Question

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.

Was it helpful?

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.

OTHER TIPS

1)Yes. over writing old variables happens due to lack of memory 2)To avoid this Try minimizing array size or you can increase the stack size. In Keil uVision there are options to increase stack size . This may solve your problem

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top