0

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?

y J.
  • 131
  • 4
  • 2
    What makes you think it's not an address on the stack? Do you know where the stack pointer starts at from the beginning of your program? – Lundin Oct 18 '21 at 06:23
  • You should [edit] and show a commented [mcve], that's easier to understand than a description of your code. – Jabberwocky Oct 18 '21 at 06:46
  • _"What is the approximate address of the stack?"_: it totally depends on your platform. – Jabberwocky Oct 18 '21 at 06:47
  • @Lundin Idk But, Considering that the highest address in memory was 0xFFFFFFFF, the address {0x00bef6fc} seemed relatively small, so it did not appear to be the address in the stack. – y J. Oct 18 '21 at 07:04
  • @yJ. the operating system can place the stack anywhere. You cannot say that some address is in the stack space or not just by looking at the value of the address. BTW look at my anser below. It seems that on my platform (Windows 10) the stack is in a similar place than on your platform. – Jabberwocky Oct 18 '21 at 07:07
  • @yJ. Most systems don't place the stack on the highest available address. Where did you get that idea from? – Lundin Oct 18 '21 at 07:32
  • 2
    You can run the code from the answer here https://stackoverflow.com/questions/52855674/a-program-uses-different-regions-of-memory-for-static-objects-automatic-objects and see the rough memory layout of the various RAM sections. Addresses beyond 0x80000000 may be reserved for kernel purposes, depending on OS. – Lundin Oct 18 '21 at 07:33

1 Answers1

1

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
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115