Search code examples
cmemoryarmkeil

What happens when we run out of memory on an Cortex M3


I'm developing a program with C for an ARM processor, in my program when my memory usage grows(when the program flows through functions which have defined arrays in them) the program behaves unexpectedly and I think some variables are overwritten, that is when it runs out of memory it starts allocating memory from the beginning.

Now my question is : Is overwriting older variables the behavior it takes when there's lack of memory? If it is, how can I avoid this?

I'm using Keil uVision and lpc 1768.

Thanks for your helps.


Solution

  • Yes, You are right. When the stack memory is full it starts to fill it from bottom address(0x00..0).This causes older variables to be overwritten and program behaves unexpectedly. solution: The easiest way to solve this problem is never define array directly instead use malloc or calloc to allocate memory and free the memory as soon as possible. This can prevent memory from overwriting. It is also good practice.