Developer101
Developer101

Reputation: 217

What is the difference between the address stored and displayed in C and C++?

In C, if I make a variable and print its address like this:

int a;
int main (void) {
    printf ("%p", &a);
    return 0;
}

The output was: 00AA

The same program in C++ using the line:

cout << &a << endl;

The output was: 0x8f970aa

What is the difference between these two?

I compiled both the programs using Turbo C.

Upvotes: 11

Views: 1753

Answers (4)

Lundin
Lundin

Reputation: 214740

Unless you are using some special, system-specific linker file, there are no guarantees of which address your variables will end up at in memory. It might even end up in different places from compilation to compilation on the same compiler. And of course different compilers will behave differently. There's no standard stating how they should allocate variables.

This has nothing to do with C versus C++. Both standards state that your variable should be allocated with static storage duration. All static storage duration variables that are not explicitly initialized are guaranteed by the C and C++ standards to get initialized to zero (see for example C11 6.7.9/10).

This means that both standards indirectly guarantee that the variable is allocated in .bss. Exactly where in the .bss is not specified anywhere.

Related question.

Upvotes: 17

haccks
haccks

Reputation: 106102

This comparison makes no sense because C and C++ are different languages. Also you may get different output on each run of your code for same language on same machine using same compiler. It is not necessary that memory for variable a will be allocated at same location. The result is implementation defined.

C11: 7.21.6 (p8):

p The argument shall be a pointer to void. The value of the pointer is converted to a sequence of printing characters, in an implementation-defined manner.

Upvotes: 5

AnthonyLambert
AnthonyLambert

Reputation: 8830

Trying to answer in the spirit of the question - If you change your C++ program to be like this they will be the same.

int a;

int main (void) {

printf ("%p\n", &a);
cout << &a << endl;

return 0;
}

The address will be the same which is after all, is all that matters!

The C++ code will pull in more libraries and start up code and C++ default static data (cout,cerr,cin,etc..) than the C code. So the address may get pushed higher into memory. Also the starting address of the application maybe set differently for C and C++ or indeed random. In Visual C++ preferences you can have a "Randomised Base Address" or a "Fixed Base Address" Setting these will move the int a address.

Upvotes: 2

Yasir Majeed
Yasir Majeed

Reputation: 741

The problem is not in the statements but rather both statements should have been in the same program. Because in different programs both have different addrress. In same program both would print the same address. I know one is C statment while the other in C++. But you can use both in same program.

Upvotes: 0

Related Questions