Benoit sorry if you feel that i discuss C++ cases in a Gambas list but i
try to describe some cases of pointers and the usage of them in Gambas.

For me and as i think for any Gambas developer the subject of pointers
and reading man 3 is a new thing.

Same questions may be come also in the future from other users which
they try to use external functions so please be patient and give help,
as you can, to newbies.

On the other hand if you think that this C++ cases are not subjects
about Gambas please tell me to stop, i will understand and sorry. 

-- 
Regards,
Demosthenes Koptsis.
--- Begin Message ---
On Mon, 2011-01-17 at 11:08 +0100, Benoît Minisini wrote:
> > 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
> > 
> 
> What must be done with with a pointer returned by a C / C++ function depends 
> on the function semantic.
> 
> You must read and *understand* the function documentation to know.

ok...

> 
> Some functions return a pointer that you must free yourself with Free().
> 
> Some functions return a pointer that you must not free.
> 
> Some functions return a pointer that you must free by calling a dedicated 
> function, and not Free().

so in case of get_current_dir_name(),

i should use the free function of c++ ?

that is i understand form manual.
"get_current_dir_name() will malloc(3) an array big enough to hold the
absolute pathname of the current working directory. If the environment
variable PWD is set, and its value is correct, then that value will be
returned. The caller should free(3) the returned buffer.'


> ... Everything is possible.
> 
> Regards,
> 

-- 
Regards,
Demosthenes Koptsis.


--- End Message ---
------------------------------------------------------------------------------
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