y J.
y J.

Reputation: 131

What is the approximate address of the stack?

I recently studied about stack, and for learning, I wrote a simple programming example in C language. And I tried debugging. Apparently, the stack stores data such as local variables or parameters and it accumulates from high to low addresses, but when I print out the address of the local variable num1, An address value that seems to be a code area {0x00bef6fc} came out. The local variable is stored in the stack, so I thought the address of the local variable should be the location of the stack, that is the high address. Did I think wrong? If I did something wrong can you please guide me?

Upvotes: 0

Views: 763

Answers (1)

Jabberwocky
Jabberwocky

Reputation: 50775

This small program prints the address of a local variable and the address of a parameter.

#include <stdio.h>

int bar(int parameter)
{
  int foo = 0;
  printf("Address of foo:       %p\n", (void*)&foo);
  printf("Address of parameter: %p\n", (void*)&parameter);
}

int main()
{
  bar(42);
}

On most platforms function parameters and local variables are stored on the stack. So the two adresses printed by the program above will most likely be close to each other, and therefore this will also be the approximate address of the stack.

OTOH you cannot determine the start address of the stack neither its length in a portable manner.

Possible output:

Address of foo:       006FFCD4
Address of parameter: 006FFCE8

Upvotes: 1

Related Questions