[Tutor] Python and memory allocation

Dave Angel davea at davea.name
Fri Oct 25 18:34:04 CEST 2013


On 25/10/2013 08:20, #PATHANGI JANARDHANAN JATINSHRAVAN# wrote:

> Hello All,
>
>>Similarly, if you call sorted() on a list of large strings,
>  you get a new list, but the strings are not duplicated, so it's not
>  nearly the duplication it might look like.
>
> 1. Sorry, but I did not understand the above point.
>

For example, if you have a list of 100 strings, each 1000  bytes long,
the total size of the list is 100,400+ bytes.

If you make a new list, by using
    sorted_list = sorted(mylist)

The new one is roughly 400+ bytes, since it doesn't make copies of any
of the strings.

(The + is intended to represent the overhead, which in some cases is
substantial.  And the factor of 4 is assuming a 32bit Python
implementation)

>
> 2. My interpretation of your answer is that the stack memory in Python holds only pointers that hold references to data on the heap. Is this correct? 
>
> 3. Also, when you spoke about the second type of variable lifetime in C, did you mean the local scope of a variable like:
>
> int func(){
>   int a = 5;
>   printf("a");
>   return 0;
> }
>  Here were you talking about 'a' which is removed from the stack when it goes out of scope?
>

Variable a is an auto variable, created on the stack when the function
is entered, and removed when the function returns.

-- 
Signature file not found



More information about the Tutor mailing list