开发者

SWIG: String not being fully copied or displayed?

开发者 https://www.devze.com 2023-03-18 20:57 出处:网络
I need to send a pointer to a character array to one of my functions. To produce this char*, I use this function in one of my c files such that is called like this

I need to send a pointer to a character array to one of my functions. To produce this char*, I use this function in one of my c files such that is called like this

charPtr = myProj.strAll ( 8 );

where strAll is:

char * strAll ( int size ) {
    return malloc( sizeof( char ) * size );
}
开发者_JAVA技巧

I then pass charPtr into a function like this:

myProj.Populate ( char* dataIn, char* dataOut, maxLen );

Populate copies dataIn into dataOut, using maxLen as the size restriction. It uses memcpy to copy it over through something like this:

memcpy ( dataOut, dataIn, maxLen);

Usage:

myProj.Populate ( "ABCD1234", charPtr, 8 ); //maxLen is the # of bytes I've allocated for charPtr.

However, when I tell python to print charPtr out, it will only print ABC.

Expected:

>>charPtr
'ABCD1234'
>>print charPTr
ABCD1234

Actual:

>>charPtr
'ABC'
>>print charPTr
ABC

Does anyone know what is happening?


ABCD1234 has 8 characters. But you should also provide an extra byte for the termination character \0. Every C string should be terminated by \0.

myProj.Populate ( "ABCD1234", charPtr, 8 );

charPtr should be pointing to a location which can accommodate 9 characters out of which the last is to place the termination character.

And I amn't sure why are you just allocating 5 locations where you are trying to copy a string of 8 characters long.

charPtr = myProj.strAll ( 5 );
0

精彩评论

暂无评论...
验证码 换一张
取 消