Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can Python functions return locally declared arrays but C can't? [duplicate]

I was reading this question: Cannot return int array because I ran into the same problem.

It seems that data structures (because C can obviously return a locally declared variable) declared locally within a function cannot be returned, in this case an array.

However Python doesn't suffer from the same problem; as far as I can remember, it's possible to declare an array within a function and to return that array without having to pass it as an argument.

What is the difference "under the hood"? Is Python using pointers implicitly (using malloc within the function)?

like image 297
jeremy radcliff Avatar asked Oct 23 '25 15:10

jeremy radcliff


2 Answers

For the record, Python's built-in mutable sequence type is called a list, not an array, but it behaves similarly (it's just dynamically resizable, like C++'s std::vector).

In any event, you're correct that all Python objects are implicitly dynamically allocated; only the references (roughly, pointers) to them are on the "stack" (that said, the Python interpreter stack and the C level stack are not the same thing to start with). Comparable C code would dynamically allocate the array and return a pointer to it (with the caller freeing it when done; different Python interpreters handle this differently, but the list would be garbage collected when no longer referenced in one way or another).

Python has no real concept of "stack arrays" (it always returns a single object, though that object could be a tuple to simulate multiple return values), so returns are always ultimately a single "pointer" value (the reference to the returned object).

like image 165
ShadowRanger Avatar answered Oct 26 '25 06:10

ShadowRanger


It seems that data structures (because C can obviously return a locally declared variable) declared locally within a function cannot be returned, in this case an array.

You already have a good Python answer; I wanted to look at the C side a little more closely.

Yes, a C function returns a value. That value may be primitive C type, or a struct or union type. Or, it may be a pointer type.

The C language syntax makes arrays and pointers seem very similar, which makes arrays special. Because the name of the array is the same as the address of the first element, it can't be something else. In particular, an array name does not refer to the whole array (except in the case of the sizeof operator). Because any other use of an array name refers to the address of the first element, attempting to return an array results in returning only that address.

Because it's a C function, that address is returned by value: namely, a value of a pointer type. So, when we say,

char *s = strdup("hello");

s is a pointer type whose value is not "hello", but the value of address of the first element of the array that strdup allocates.

Python doesn't suffer from the same problem

When Y is a property of X, Y is a problem only if that property is, in the eyes of the beholder, undesirable. You can be sure the way C treats arrays is not accidental, and is often convenient.

like image 34
James K. Lowden Avatar answered Oct 26 '25 05:10

James K. Lowden



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!