Allocating memory pointer to a struct instead of calling malloc

79 views
Skip to first unread message

Dan Rossi

unread,
Jan 11, 2016, 3:49:26 AM1/11/16
to python-cffi
Hi there I am new to cffi I was wondering how it is possible to allocate memory to a struct property instead of calling malloc ? The ffi.new did not seem to work. see

my struct

struct INFO
{  

void *data ; /* Pointer to the data. */
} ;

typedef struct INFO INFO ;


On the python side

info = _ffi.new("INFO*")

Dan Rossi

unread,
Jan 11, 2016, 3:52:01 AM1/11/16
to python-cffi
This silly system posted on spacebar. 


info.data = #allocate memory to this

currently I have to do this

info.data = _lib.malloc (1024) ;

then free it up after

_lib.free(info.data)
LEt me know thanks.

Armin Rigo

unread,
Jan 12, 2016, 5:36:10 AM1/12/16
to pytho...@googlegroups.com
Hi,

On Mon, Jan 11, 2016 at 9:52 AM, Dan Rossi <electr...@gmail.com> wrote:
> currently I have to do this
>
> info.data = _lib.malloc (1024) ;
>
> then free it up after
>
> _lib.free(info.data)

This is because ffi.new() returns a Python object that keeps the
memory alive as long as it exists. If you say for example ``info.data
= ffi.new("char[]", 1024)``, then that line is *bogus* in all cases:
the pointer value is copied into ``info.data``, but the Python object
returned by ffi.new() is immediately forgotten, and so the array of
chars is immediately deallocated.

There are two ways to fix it: one is like you do, with explicit calls
to malloc() and free(); the other way is to keep alive the Python
object manually, e.g.:

my_array = ffi.new("char[]", 1024)
info.data = my_array

and then make sure that the object stored in ``my_array`` is kept
alive as long as you need ``info.data`` to be valid. This might be as
simple as keeping it in a local or global variable that survives long
enough, or it might require saving it to a ``self._my_array``
attribute, for example.


A bientôt,

Armin.

electr...@gmail.com

unread,
Jan 12, 2016, 6:56:09 AM1/12/16
to pytho...@googlegroups.com
Is calling malloc directly acceptable and then call free once the pointer has been processed ?  I don’t quite know what is going on with the pointer in the library function but I did try ffi.new("char[]", 1024) and it was segfaulting. 

So I set these up in def to be able to call them 

void* malloc(size_t);
void free(void* ptr);


--
-- python-cffi: To unsubscribe from this group, send email to python-cffi...@googlegroups.com. For more options, visit this group at https://groups.google.com/d/forum/python-cffi?hl=en
---
You received this message because you are subscribed to a topic in the Google Groups "python-cffi" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/python-cffi/ZEPRAItJGQg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to python-cffi...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Armin Rigo

unread,
Jan 12, 2016, 7:59:02 AM1/12/16
to pytho...@googlegroups.com
Hi,

On Tue, Jan 12, 2016 at 12:55 PM, <electr...@gmail.com> wrote:
> Is calling malloc directly acceptable and then call free once the pointer
> has been processed ? I don’t quite know what is going on with the pointer
> in the library function but I did try ffi.new("char[]", 1024) and it was
> segfaulting.

As I said, yes, you are allowed to call malloc/free directly. I gave
hints for a common reason for why ffi.new("char[]", 1024) can lead to
segfaults, but if you prefer, you can ignore them and use malloc/free
directly.


A bientôt,

Armin.

electr...@gmail.com

unread,
Jan 12, 2016, 8:06:17 AM1/12/16
to pytho...@googlegroups.com
my_array = ffi.new("char[]", 1024) 
info.data = my_array 

I’m sorry I will try that out and let you know. I’m new to cffi. 

Does my_array get free once the program has ended ? 

Armin Rigo

unread,
Jan 12, 2016, 8:42:51 AM1/12/16
to pytho...@googlegroups.com
Hi,

On Tue, Jan 12, 2016 at 2:05 PM, <electr...@gmail.com> wrote:
> my_array = ffi.new("char[]", 1024)
> info.data = my_array
>
> I’m sorry I will try that out and let you know. I’m new to cffi.
>
> Does my_array get free once the program has ended ?

It is freed as soon as the variable 'my_array' gets out of scope.

All memory is always freed when the program ends.


Armin

electr...@gmail.com

unread,
Jan 12, 2016, 9:52:24 AM1/12/16
to pytho...@googlegroups.com
Thanks very much that seems to work with no issues. 
Message has been deleted

Damien Ruiz

unread,
Mar 22, 2016, 11:42:13 AM3/22/16
to python-cffi
Hi,

To instantiate a structure like yours, Dan, I would go with :

pInfo = ffi.new( "INFO[]", 1 )

Then I would fill info.data with a C function like int GetInfoData( INFO* pInfo ) (Python code would be: c.GetInfoData( pInfo )), and finally access the data in Python with something like pInfo[0].data

Anything wrong with that Armin? Is it bad practice?


Thanks,

Damien

Armin Rigo

unread,
Mar 22, 2016, 12:22:57 PM3/22/16
to pytho...@googlegroups.com
Hi,

On 22 March 2016 at 16:42, Damien Ruiz <ruiz...@gmail.com> wrote:
> Anything wrong with that Armin? Is it bad practice?

I can't comment more than that because I have no idea what you're
really trying to do, but: Dan describes a situation in which,
apparently, the called function expects not only 'info' but
'info->data' to be already pointing to something. You are describing
a situation where GetInfoData() expects 'info' but will store itself
something into the 'info->data' field. If I'm correct, they are
different cases.


A bientôt,

Armin.
Reply all
Reply to author
Forward
0 new messages