Idiosyncratic python

Ian Kelly ian.g.kelly at gmail.com
Thu Sep 24 10:26:11 EDT 2015


On Thu, Sep 24, 2015 at 8:07 AM, jmp <jeanmichel at sequans.com> wrote:
> result = getResult()
>
> For the later, the original weird form come from a C habit to allocate
> returned structures within the caller and provide a pointer to it so the
> function can fill the data in, otherwise the structure is lost as the stack
> is popped out and the structure content is garbage. None of this make any
> sense in python.

Only if the structure is allocated on the stack and returned by
pointer. If it's returned by value, then the content remains intact,
but at the expense of copying it. The other option of course would be
to allocate it on the heap and return the pointer, but this needlessly
incurs malloc overhead and creates an opportunity for a memory leak if
the variable is naturally stack-scoped. Python effectively takes this
option, as everything is allocated on the heap. Leaving it up to the
caller to provide a pointer also gives the caller the option of
allocating on the stack or the heap as best fits the context.



More information about the Python-list mailing list