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

Demosthenes Koptsis demosthenesk at ...626...
Mon Jan 17 20:36:38 CET 2011


On Mon, 2011-01-17 at 13:33 +0100, Benoît Minisini wrote:
> > sorry my wrong expression.
> > 
> 
> No problem.
> 
> Another point: at the moment, Alloc() and Free() are not the equivalent of 
> malloc() and free() in C. So you cannot use Free() to free a memory allocation 
> returned as a pointer by a C function. You must use the free() function 
> located in the C library directly.
> 
> Regards,
> 

About that,

The next example uses free() function of C to free a pointer.

Example1
------------
' Gambas module file
'char *get_current_dir_name(void);
Extern get_current_dir_name() As Pointer In "libc:6"
'void free(void *ptr);
Extern free_ptr(ptr As Pointer) In "libc:6" Exec "free"


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
------------
free_ptr(pFunc)
is the free() function of C and here frees a pointer which was allocated
internal by get_current_dir_name().

But free() function of C, cannot free a pointer allocated by Alloc() of
Gambas. May be this is obvious or expected but is good to say it.

Example2
--------------
' Gambas module file
'void free(void *ptr);
Extern free_ptr(ptr As Pointer) In "libc:6" Exec "free"

Public Sub Main()

  Dim pPointer As Pointer

  pPointer = Alloc(4)

  free_ptr(pPointer)

End

example2 gives signal #6.

 

-- 
Regards,
Demosthenes





More information about the User mailing list