On 09/24/2015 04:26 PM, Ian Kelly wrote:
On Thu, Sep 24, 2015 at 8:07 AM, jmp <jeanmic...@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.


I'm not an expert but I think this "return by value thing" is only for C++.
In vintage C, you can only return something that fits within a register.

Anyway, there's a lot of legit C code in which functions are plagued by 'out parameters' and somehow it has transpired in some python code for no reason :o)

jm

--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to