0

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?

5
Contributors
13
Replies
14
Views
11 Years
Discussion Span
Last Post by vicky_dev
0

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.

0

This call is missing parameters.

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

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

0

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?

0

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.

0

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?

0

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.

0

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

0

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.

0

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

0

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?

0

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.

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.