C-API question: Memory returned by PyString_AsString

Tim Peters tim.one at comcast.net
Fri Jul 19 04:00:41 EDT 2002


[Christoph Bennemann]
> functions like PyString_AsString return a pointer to a character
> vector. Is this memory specially allocated when I call this function

Sorry, I don't know what that means.  Luckily, it's irrelvant <wink>.

> (which implies I have to take care of it in my program) or is this
> merely a pointer to a character vector stored by the Python
> interpreter (implying that I should not free the memory). I assume
> that the later is correct, but would like to have a definitive answer.

No Python API function returns a pointer you should free() or realloc().  In
the case of PyString_AsString, you're getting a (shared with Python) pointer
into the guts of a pre-existing Python string object, and trying to free()
that would lead to disaster.






More information about the Python-list mailing list