This is really simple code that prints the address of a variable :

int main()
{
  int x;
  scanf("%d", &x );
  printf("Address of x : %X, value : %d" );
  return 0;
}

<< moderator edit: added [co[u][/u]de][/co[u][/u]de] tags >>

Simple as that.
But the thing is this program gives the same output for the address no matter when I run it - before system startup, or even with my RAM full with apps. Even if I run multiple instances of the program, the address remains the same, leading me to believe that many different values are stored at the same address.
Can somebody please explain why this happens? Is it that the variable isn't really stored in the RAM at all?

Recommended Answers

All 13 Replies

Try this program and see the output.

int main()
{
  int y;
  int x;
  scanf("%d", &x );
  printf("Address of x : %X, value : %d" );
  return 0;
}

<< moderator edit: fixed [co[u][/u]de][/co[u][/u]de] tags >>

I think the allocation starts from a specific location and every next allocation starts from the memory.

Regards,
Paramesh.

This call is missing parameters.

printf("Address of x : %X, value : %d" );

"If there are insufficient arguments for the format, the behavior is undefined."

Sorry about that, here's the correct code :
int main()
{
int x;
scanf("%d", &x );
printf("Value of x : %d, address : %x", x , &x );
return 0;
}
Can you explain the output?

First, it should be this:

printf("Value of x : %d, address : %p", x , (void*)&x);

But the thing is this program gives the same output for the address no matter when I run it - before system startup, or even with my RAM full with apps. Even if I run multiple instances of the program, the address remains the same, leading me to believe that many different values are stored at the same address.
Can somebody please explain why this happens? Is it that the variable isn't really stored in the RAM at all?

You are inquiring about OS internals, which has nothing to do with the code you posted.

The change in printf() doesn't really make a difference. The memory allocated is always the same. It does concern the OS, but how can the same memory location be allocated to two variables at the same time?

The change in printf() doesn't really make a difference. The memory allocated is always the same. It does concern the OS, but how can the same memory location be allocated to two variables at the same time?

Not certain, but the address you see is probably the offset from the beginning of the program in memory, not the real address of the variable in your computer's memory.

Thanks, that probably explains it. But do you know how to get the absolute address?

no, I don't know how to get the physical address of the stack segment. there is probably a win32 api function somewhere that will give it do you.

The change in printf() doesn't really make a difference.

Perhaps. What I posted is correct. What you had was undefined behavior. Attempting to draw any conclusions from undefined behavior is ludicrous.

The memory allocated is always the same. It does concern the OS, but how can the same memory location be allocated to two variables at the same time?

Again, it has to do with OS internals.

[edit]http://en.wikipedia.org/wiki/Virtual_memory

Again, why is it an undefined behaviour?

Edit printf statement to correct format.
The argument you are passing for the printfstatement is not correct. So it is taking some junk value. :lol:

This is really simple code that prints the address of a variable :

int main()
{
  int x;
  scanf("%d", &x );
  printf("Address of x : %X, value : %d" );
  return 0;
}

<< moderator edit: added [co[u][/u]de][/co[u][/u]de] tags >>

Simple as that.
But the thing is this program gives the same output for the address no matter when I run it - before system startup, or even with my RAM full with apps. Even if I run multiple instances of the program, the address remains the same, leading me to believe that many different values are stored at the same address.
Can somebody please explain why this happens? Is it that the variable isn't really stored in the RAM at all?

Again, why is it an undefined behaviour?

any time you use os-specific os-internals, it becomes undefined behavior because c and c++ standards do not specifiy what should happen. It might not have that behavior on another operating system.

Really really sorry. But that mistake in printf() is totally unrelated because that was not what I wrote in the real code, just a mistake in wriring it here :). Please ignore that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.