C API and memory allocation

Floris Bruynooghe floris.bruynooghe at gmail.com
Wed Dec 17 18:06:41 EST 2008


Hi

I'm slightly confused about some memory allocations in the C API.
Take the first example in the documentation:

static PyObject *
spam_system(PyObject *self, PyObject *args)
{
    const char *command;
    int sts;

    if (!PyArg_ParseTuple(args, "s", &command))
        return NULL;
    sts = system(command);
    return Py_BuildValue("i", sts);
}

What I'm confused about is the memory usage of "command".  As far as I
understand the compiler provides space for the size of the pointer, as
sizeof(command) would indicate.  So I'm assuming PyArg_ParseTuple()
must allocate new memory for the returned string.  However there is
nothing in the API that provides for freeing that allocated memory
again.  So does this application leak memory then?  Or am I
misunderstanding something fundamental?


Regards
Floris



More information about the Python-list mailing list