9 years, 2 months ago.

variables and heap/stack area

I tried the following code on Nucleo F072, with off-line gcc compile exported from online compiler. The linker script declares that the RAM starts from 0x200000c0, and lts length is 16KB-0xc0. This results that the initial stack pointer wil be 0x20004000, at the end of RAM region.

  1. include "mbed.h" UART declaration here uint8_t a; main() { register unsigned char *stack_ptr asm ("sp"); printf("sp=%x\r\n", stack_ptr);

uint8_t b; uint8_t c; printf("@a=%x\r\n", &a); printf("@b=%x\r\n", &b); printf("@c=%x\r\n", &c); }

The results are (The UART inisitalization will use some heap and stack before allocating b and c.)

sp=20003ee8 a=20000300 b=20003eee c=20003eef

In my understanding, stack is used from the address of sp to the top of RAM region. The variable a seems to be allocated at the heap region, however, b and c seems to be inside the used stack, the larger address than sp. Is the variables of b and c will be allocated in the heap area? Can I know where they are actually allocated?

This is my mis-understanding. The variables are allocated in stack region; this gives the results above. This is correct.

posted by Junichi Akita 10 Sep 2015
Be the first to answer this question.