ctypes pointers

Diez B. Roggisch deets at nospam.web.de
Wed Nov 14 07:00:16 EST 2007


Giorgos Giagas wrote:

> Hi all,
> I have started using ctypes and I am having some questions I can't
> seem to find the answers to.
> 
> There is a structure referenced by a pointer in an other structure and
> I want to see if it is one of the structures I have pointers to.
> So what I think I need is a way to compare 2 pointer's memory
> location. I can't find a way to do this. Pointer comparisons don't
> work. Is there something else I should do?

Use addressof on the contents of your pointers.
 
> The other thing is more of a curiosity... The documentation says that
> I need to keep references to structures so they don't get freed is
> this also true for structures returned as pointers? What about
> structures that I cast to something else? I guess the largest of the
> two casts gets freed like it should... can someone confirm this?

The one has nothing to do with the other.

If you create a struct and pass e.g. a pointer to it into a DLL, you need to
keep a reference, otherwise the struct will be deallocated and the pointer
is dangling.

If you have a pointer and cast it to some structure, you get a structure
object (in python) that points to a certain memory portion. But GCing that
object doesn't do anything to the memory location. 

Diez



More information about the Python-list mailing list