Using a pointer returned from a C library in C ++

I am using a library developed in C (specifically, HTK). I changed the source code a bit and tried to get a pointer (from the beginning of the linked list) from the function. Do not go into details; I have a structure called OutType. In my C ++ code, I declare: OutType * Out ; and pass it to some LName function (....., OutType * Out) Now in the C library, LName takes an Out parameter and calls a function called SaveH, where Out is the return value ( Out = SaveH (...) ), and in SaveH, Out - malloc'ed as OutType returnOut = (OutType *) malloc (1, sizeof (OutType)); As far as I can see, Out is perfectly malloc'ed, and in the LName function I can get the address of the allocated memory area. But when I return to my C ++ code, where I call LName and pass Out as a parameter, this parameter always has 0 as the address. If I leave everything the same, but just change SaveH so that Out is not the return value, but the SaveH parameter (...., OutType * Out) and selects this value in C ++ code before going through Everything is fine. This is normal? Is there some kind of problem with the pointer allocated in the C library using C ++ code? Thanks

+3
source share
2 answers

You are passing a copy of the pointer, so the change in the C library is not visible in your C ++ code.

, C, .

LName(....., OutType** Out)

*Out=SaveH(...);

++, C- .

+7

:

void Foo( int * p ) {
   p = malloc( sizeof(int) );
}

, :

int * x;
Foo( x );

x , . :

void Foo( int ** p ) {
   *p = malloc( sizeof(int) );
}

int * x;
Foo( & x );

, - , , .

+2

Source: https://habr.com/ru/post/1709489/


All Articles