This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Configuring stack size and direction to avoid stack overflow

Hello,

I'm using a Cortex M-4 processor (STM32F4) and I recently encountered strange behavior of my code after minor modifications that were not directly related to the problems I was seeing.

After a bit of digging, I noticed that some of my global variables were modified without being accessed through the code. Looking at the memory mapping, it turned out these variables were located just before the stack. Increasing the stack size in the startup file solved the issue, so obviously this was a stack overflow.

But the thing is, I want to be sure that when a stack overflow occurs, a fault happens, not some random behavior. I thought I was safe because my stack was at the end of the RAM space. But as it turns out, it actually grows backwards.

So the question is: How to configure the stack direction ? And are there other ways to prevent this kind of problem?

Thank you

Parents
  • "I thought I was safe because my stack was at the end of the RAM space. But as it turns out, it actually grows backwards."

    Yes - that is normal and, as already noted, I don't think there's any way to change the direction it grows.

    But something doesn't make sense here:

    1. The stack is placed at the "end" (top) of RAM, and grows backwards.

    2. Your program variables start at the "bottom" of RAM, and grow upwards.

    So, if your stack was colliding with your program variables, you had filled-up all the available RAM anyhow - so increasing the Stack size would not have helped!

Reply
  • "I thought I was safe because my stack was at the end of the RAM space. But as it turns out, it actually grows backwards."

    Yes - that is normal and, as already noted, I don't think there's any way to change the direction it grows.

    But something doesn't make sense here:

    1. The stack is placed at the "end" (top) of RAM, and grows backwards.

    2. Your program variables start at the "bottom" of RAM, and grow upwards.

    So, if your stack was colliding with your program variables, you had filled-up all the available RAM anyhow - so increasing the Stack size would not have helped!

Children