Re: Implementing an interface



bcpkh <vanheerden.braam@xxxxxxxxx> wrote:
Think I found my problem though, even though the function's return
type is void one of the parameters passed to the function is a char**,
pointer to pointer, which is supposed to contain the version number of
my plug-in, a character string. I am not correctly allocating memory
in my function and I think, as you suggested, the calling process
probably does not check that I allocated the memory correctly and give
the segmentation error.

If the caller just expects a string with the version then you
may not have to allocate memory. Perhaps something like

void give_me_your_version_please( char ** version_string ) {
*version_string = "0.0.42"
}

will do since you set what 'version_string' points to to a
string literal that won't vanish until the library gets
unloaded. But, of course, you have to read the complete
documentation about what kind of memory the caller expects
to get returned via 'version_string', if there's any indi-
cation that the caller is going to modify that string or
that it will try to call free() on the pointer you gave it
then this won't do and you have to resort to using "real"
allocated memory.

I need to experiment a bit but feel confident that I am on the right
track.

Much luck and hopefully at least a bit of fun;-)

Regards, Jens
--
\ Jens Thoms Toerring ___ jt@xxxxxxxxxxx
\__________________________ http://toerring.de
.



Relevant Pages

  • Re: should I free mem in function after alloc and returning pointer... ?
    ... Should I always free memory previously allocated in a ... > function even if I returned the pointer to that mem previously? ... Returning a string to a caller is usually done in 3 ways: ...
    (comp.lang.c)
  • Re: How is string::c_str() usually implemented?
    ... > would change the dynamics of the pointer returned. ... > associated string) pretty much tells the tale. ... I think a new buffer can be returned without transferring ... ownership to the caller. ...
    (comp.lang.cpp)
  • Re: Strings
    ... When allocating memory, there is *always* the possibility that the allocation will fail ... and you will get a NULL pointer back. ... APpending data to a string is a very expensive operation. ...
    (microsoft.public.vc.mfc)
  • Re: marshaling strings - ArrayTypeMismatchException
    ... I know - however the DLL I am using is doing the mmgmt, every string ... > Your callee is allocating memory, then passing that reference back to the ... First, the caller doesn't own it, second, who's going to free that ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: Replace escapeable characters with escape sequence?
    ... returning a string from a function can be tricky. ... I said it was tricky, ... requires the caller to free the allocated string. ... pre-allocate a buffer and pass a pointer to it, ...
    (comp.lang.c)