This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

variable memory allocation



i have micro which has around 2KB of ram 16KB FRAM

what would happen if i declare something like this 

static uint8_t mydata[1500];        // it simply goes to bss , and consume 1500 bytes in ram 

void main()

{

uint8_t data2[5000];                   // it goes where ? compiler don't seems to be giving me any error or warning at all 

}

another confusion is 

static uint8_t mydata[1500];        // it simply goes to bss , and consume 1500 bytes in ram 

void main()

{

------

uint8_t data2[250];                   // i can see from map file of my microcontroller, stack is of only 160 byte , what happen if i declare variable of size 250 byte?

-------

}

  • The mydata declarations would be placed in the .bss section of RAM.
    The data2 declarations would be allocated on the stack, which is not checked at compile time. It's purely on the you make sure that you don't allocate too much on the stack. Once you overflow the stack unpredictable things may happen.
  • this sound fair to me. we can keep track of the stack size.

    but if i have stack size of 160 then i can not create array size of 161? will stack expand if i create one?

    i tried this a little although i don't know if i tested it correctly but i was able to create a 256 byte local array when having stack size of only 160 byte .
    does anyone have some theoretical details about this ? some compiler manual or something like that? 

    i will really very kind of help me little. 

  • Hi Gaurav,

    The linker only allocates the specified stack size (i.e. 160B) to avoid placing variables inside this area.
    But the stack can grow and overflow indefinitely (limited by the RAM size of course).

    If the stack grows and doesn't overwrites any variables, then you'll be OK; but if the stack overflows and re-writes some variables, then you will find problems.
    Some details here: processors.wiki.ti.com/.../Stack_and_Heap_size_requirements

    Regards,
    Luis R
  • Luis, the stack can grow beyond ram size. Sure, nothing can be stored there then, but the CPU doesn't are as long as it doesn't try to read something back (e.g. a return address).
    It may even happen that you define a large local array and the stack pointer now points into peripheral register area, where a return address can be stored and fetched from (of course with disastrous effects on the hardware configuration of the affected module).
    And if you have a function that calls itself (recursion), the memory (ram or whatever) will be quickly written with the return address form top of ram down until finally the return address is written to a register that is password protected and triggers a reset.

    "The stack grows as the program flows." :)

    To clarify what's going on: the compiler assumes a limitless stack when generating code. It doesn't know how much of the available ram is already required on the stack by other modules that are later linked, or by recursions. It doesn't even know how much ram the target may have. Only the linker knows this, but the linker doesn't know how much stack the code might need during program execution. It can only ensure that at least as much as configured will be available.

**Attention** This is a public forum