Here is a last case about pointers i found.

CASE 1
-------
When a C++ function does not malloc any memory it may return a pointer.
In this case we can do

DIM pPointer as Pointer
pPointer = Alloc(4096)' alloc 4096 bytes

pPointer = getwd(pBuf)

'use pPointer with Str@() or other xxx@()

Free(pPointer)
pPointer=0

OK!

CASE 2
-------
When a c++ function itself allocate memory with malloc() what can we do?

Example.
--------
' 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 '0xc12938
  
  pFunc = get_current_dir_name()
  Print pFunc '0xc147a0
  
  'now we cannot Free(pFunc) it is not 0xc12938 anymore
  'get_current_dir_name() malloc itself memory and return a new pointer
   
  sWorkingDirectory = Str@(pFunc)
  Print sWorkingDirectory

' Free(pFunc)
'if i unrem Free i get a crash, because i try to free the 0xc147a0
  pFunc = 0

End

What can we do in such cases when a function itself allocates memory?
Should we free it? Can we?

May be we can use pointers without Alloc()?

Example, no Alloc(), no Free, just DIM pPointer
---------
' 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 = get_current_dir_name()
  Print pFunc '0xc147a0
  
  sWorkingDirectory = Str@(pFunc)
  Print sWorkingDirectory

  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@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/gambas-user

Reply via email to