[Tutor] Variable Modification in a class

Alan Gauld alan.gauld@blueyonder.co.uk
Tue Jun 3 17:51:03 2003


> when i do ' c = b' it makes the memory locations same.
> 
> C example which is creating the confusion:
> 
> main()
> {
>   AB b;
>   AB c;

Lets change the C example so it is more like 
the Python one, that might help.

/*gosh I'd forgotten how horrible malloc is! */
AB* b = (AB*)malloc(AB); 
AB* c = (AB*)malloc(AB);

>   b->a = 5;
>   b->b = 10;
> 
>   c = b;

Now, c points to the same original object as B and 
the one that C pointed to is lost(a memory leak in 
C, garbage colected in Python)

>   c->a = 30;
>   c->b = 40;
> 
>   printf("AB values %d %d\n", b.a, b.b);
>   printf("New values %d %d\n", c.a, c.b);
> }

Now both lines will print the same values, just 
like Python.

HTH,

Alan G.