There is a libx.so which export 2 functions, and a struct,
typedef struct Tag {
int num;
char *name;
}Tag;
Tag *create(int n, char *n)
{
Tag *t = malloc(sizeof(Tag));
t->num = n;
t->name = n;
return t;
}
void use(Tag *t)
{
printf("%d, %s\n", t->num, t->name);
}
I want to call create in Python and then save the Tag *res returned by create, later I will call use and pass the Tag *res saved before to use, here is it (just to demonstrate):
>>>libx = ctypes.CDLL("./libx.so")
>>>res = libx.create(c_int(1), c_char_p("a"))
>>>libx.use(res)
The above code might be wrong, just to demonstrate what I want to do.
And my problem is that, how could I save the result returned by create? Because it returns a pointer to a user-defined struct, and I don’t want to construct struct Tag‘s counterpart in Python, would c_void_p do the trick?
UPDATE
From @David’s answer, I still don’t quite understand one thing:
the pointer (
c_char_p("a")) is only valid for the duration of the
call tocreate. As soon as create returns then that pointer is no
longer valid.
And I assign c_char_p("a") to t->name in create, when the call to create finishes, is t->name a dangling pointer? Because according to the quoted words, that pointer is no longer valid after create. Why c_char_p("a") is no longer valid?
The C code that you present is simply not going to work. You need to be much more precise about which party allocates and is responsible for the heap memory.
In your current example you pass
c_char_p("a")to the C code. However, the pointer to that ctypes memory is only valid for the duration of the call tocreate. As soon ascreatereturns then that pointer is no longer valid. But you took a copy of the pointer insidecreate. Thus the subsequent call touseis liable to fail.You are going to need to take a copy of the contents of that string and store it in the struct. If you do that then you can use
libx.create.restype = c_void_psafely.But if you want the memory you allocated to be deallocated you will have to provide a
destroyfunction to match thecreatefunction. With these changes the C code would look like this:The Python code would look like this: