c - How can I align stack to the end of SRAM? -


i have stm32f103vct6 microcontroller 48kb of sram, , i've got memory collision:

i have static variable (lets call a) located in heap size of 0x7000 , wrote simple function info stack , heap:

void check(int depth) {     char c;     char *ptr = malloc(1);     printf("stack @ %p, heap @ %p\n", &c, ptr);     if (depth <= 0) return;     check(depth-1); } 

so got this:

stack @ 2000939b, heap @ 20008fd0 stack @ 20009383, heap @ 20008fe0 stack @ 2000936b, heap @ 20008ff0 stack @ 20009353, heap @ 20009000 stack @ 2000933b, heap @ 20009010 stack @ 20009323, heap @ 20009020 stack @ 2000930b, heap @ 20009030 stack @ 200092f3, heap @ 20009040 stack @ 200092db, heap @ 20009050 stack @ 200092c3, heap @ 20009060 stack @ 200092ab, heap @ 20009070 

all static variables (incliding a) got heap, heap located @ 0x8fd0. , seems like, originally, stack pointer located @ 0x939b, far away 48kb (0xc000)

and when changed a variable size 0x4000 i've got picture:

stack @ 2000639b, heap @ 20005fd0 stack @ 20006383, heap @ 20005fe0 stack @ 2000636b, heap @ 20005ff0 stack @ 20006353, heap @ 20006000 stack @ 2000633b, heap @ 20006010 stack @ 20006323, heap @ 20006020 stack @ 2000630b, heap @ 20006030 stack @ 200062f3, heap @ 20006040 stack @ 200062db, heap @ 20006050 stack @ 200062c3, heap @ 20006060 stack @ 200062ab, heap @ 20006070 

so, seems stack location not located @ end of sram but, how, rely on user defined variables.

how can align stack @ end of sram (at 48kb)?

i using coocox ide gnu tools arm embedded toolchain.

thank you!

edit:

sorry misunderstanding here, a isn't const, i've called static because of keyword:

static uint8_t a[a_size];     printf("a @ %p\n", &a); 

this shows a located @ begginning of memory:

a @ 20000c08 

the gnu toolchain uses target specific linker script (conventionally .ld file name extension). describe memory layout of target , can customised suit.

the manner in have deduced stack , heap location non-deterministic , over-complicated. far simpler , entirely accurate @ map file output generated linker (ld command line option -map <mapfile>).

the heap definition dynamic allocation, implicitly not use allocate static data; misconception on part. linker allocate static data locations @ build time. linker script allocate fixed size stack, , allocate remains , not reserved other purposes heap. customised script may allocate areas other purposes such dma buffers or battery-backed domains example.

either way, placement of stack hardly solution actual problem; moves things around; won't increase available memory; stack overflow collide else.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -