1

I was looking at one of the questions in SO, and looked at the question.

Why id function behaves differently with integer and float?

I tried that in python to get the same id for both a&b. But when i tried the same on c,

main()
{
    int a=4,b;
    b=2+2;
    printf("%p\n",&a);
    printf("%p",&b);
}

But this printed different values. Why is the difference ?

Community
  • 1
  • 1
Aswin Murugesh
  • 10,831
  • 10
  • 40
  • 69
  • Why should `&a == &b`? After all, you're dealing with variables. Maybe if you use optimization, but after all both variables are indicating available memory space which are completely independent from each other. – Zeta Apr 14 '13 at 08:21
  • http://stackoverflow.com/questions/3402679/identifying-objects-why-does-the-returned-value-from-id-change – ndpu Apr 14 '13 at 08:22
  • `&a` is the address of `a`. Their values are the same however their memory addresses are different. – 1337holiday Apr 14 '13 at 08:23
  • @Zeta: but why do they point to the same id in python? – Aswin Murugesh Apr 14 '13 at 08:23
  • 4
    Because python caches small integeres; C/C++ doesn't – unddoch Apr 14 '13 at 08:24
  • 5
    @AswinMurugesh: *"That is because the result of id in numeric constants is implementation defined."* Did you even read the answer in the other question? Also, those are completely different languages. You're not going to look at your cat and ask it "Why don't you bark"? – Zeta Apr 14 '13 at 08:25
  • oh. Do you mean that python references are based on the number, but c references are based on the address of that variable? – Aswin Murugesh Apr 14 '13 at 08:26
  • To make memory use smaller, Python saves very small, frequently used integers and strings in the memory, so when you use them, the addresses are equal because they point to the cached values. C/C++ just don't do any memory manipulations when not explicitly told to. – unddoch Apr 14 '13 at 08:28
  • oh.. ya tried with large numbers in python, and got different ids – Aswin Murugesh Apr 14 '13 at 08:30

3 Answers3

5

The equivalent Python code works for the reason given in several answers in the question you link - Python (or, at least, CPython) happens to cache some small integer values - 4 is among them, and so every Python integer object that equates to 4 will be the same object, and hence have the same id.

In C, integers are mutable, so a compiler can't perform this kind of optimisation - if you increment a, you would not expect b to change If Python integers were mutable, the ids of a and b in the equivalent code would likewise be different.

At the implementation level, int means a different thing to C than it does to Python - even if they happen to serve the same purpose in a program.

lvc
  • 34,233
  • 10
  • 73
  • 98
2

Python caches some small integers. From python documentation http://docs.python.org/2/c-api/int.html

The current implementation keeps an array of integer objects for all integers between -5 and 256, when you create an int in that range you actually just get back a reference to the existing object. So it should be possible to change the value of 1. I suspect the behaviour of Python in this case is undefined. :-)

ndpu
  • 22,225
  • 6
  • 54
  • 69
1

In C two variables will always have different addresses,

The only exception is when you use union,

Consider the code given below,

union x
{
 int a;
 int b;

}y;

main()
{
    y.a=4;
    y.b=2+2;
    printf("%p\n",&y.a);
    printf("%p",&y.b);
}

Here both y.a and y.b will point to the same address.

Deepu
  • 7,592
  • 4
  • 25
  • 47