[Gambas-user] Gambas3 Pointers example - signal #6

Demosthenes Koptsis demosthenesk at ...626...
Tue Jan 18 09:08:32 CET 2011


On Mon, 2011-01-17 at 13:59 -0800, John Spikowski wrote:
> On Mon, 2011-01-17 at 21:36 +0200, Demosthenes Koptsis wrote:
> > On Mon, 2011-01-17 at 13:33 +0100, Benoît Minisini wrote:
> 
> > Public Sub Main()
> > 
> >   Dim pFunc As Pointer
> >   Dim sWorkingDirectory As String
> > 
> > 'get_current_dir_name
> >   pFunc = get_current_dir_name()
> >   Print pFunc
> >   
> >   sWorkingDirectory = Str@(pFunc)
> >   Print sWorkingDirectory
> > 
> >   free_ptr(pFunc)
> >   pFunc = 0
> > 
> > End
> 
> Isn't pFunc a Gambas variable (memory location) which is maintained by
> the Basic? Why would you free it externally? I would think Gambas would
> complain about that.

Yes pFunc is a pointer. A pointer is a variable that holds a memory
address.

At line 
Dim pFunc As Pointer
i declare the variable.

But i do not use Alloc() to alocate a memory address with Gambas3.

i do this because the external function get_current_dir_name() allocates
a memory address internal.

This is described in manual
http://manpages.ubuntu.com/manpages/dapper/en/man3/get_current_dir_name.3.html

"get_current_dir_name, which is only prototyped if _GNU_SOURCE is
defined, will malloc(3) an array"

to free a pointer in such a case i have to use free() function of C.

Next is the same example but with an error.
The error is that i allocate memory with Gambas3 and try to Free() the
memory later. But when i call get_current_dir_name() this function set
again a new pointer and the address from Gambas3 Alloc() is not
accesible any more. So when i try to Free() the pointer in fact i try to
free an unlocated memory!

' Gambas module file
'char *get_current_dir_name(void);
Extern get_current_dir_name() As Pointer In "libc:6"

Public Sub Main()

  Dim pFunc As Pointer
  Dim sWorkingDirectory As String

'get_current_dir_name
  pFunc = Alloc(4096)
  Print pFunc '(Pointer 0x185b918)
  
  pFunc = get_current_dir_name()
  Print pFunc '(Pointer 0x185d780)
  
  'notice that address was changed
  ' from now on the address 0x185b918
  'is unreachable
  'and the new address 0x185d780
  'is not allocated by Gambas3 !
  'so we cannot free both of them
  'with Free() function of Gambas3!
   
  sWorkingDirectory = Str@(pFunc)
  Print sWorkingDirectory

  Free(pFunc)
  pFunc = 0

End



> 
> 
> ------------------------------------------------------------------------------
> Protect Your Site and Customers from Malware Attacks
> Learn about various malware tactics and how to avoid them. Understand 
> malware threats, the impact they can have on your business, and how you 
> can protect your company and customers by using code signing.
> http://p.sf.net/sfu/oracle-sfdevnl
> _______________________________________________
> Gambas-user mailing list
> Gambas-user at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/gambas-user

-- 
Regards,
Demosthenes Koptsis.





More information about the User mailing list