Stack size for 2D array in KDS

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Stack size for 2D array in KDS

Jump to solution
1,200 Views
McLuhan
Contributor II

Hi,

 

I'm trying to declare, define and use a 2x68 int array in KDS with Processor Expert.

Unfortunately I get a m_data segment overflow error (16 byte) when I build the project.

I tried to increase the stack size in Processor Expert, but situation is getting worse (bigger overflow).

Then I tried to decrease the stack size. The compiler now says that everything is ok.

This is counterintuitive for me.

Do you confirm that is ok?

 

Thank you!

 

0 Kudos
Reply
1 Solution
1,193 Views
ErichStyger
Specialist I

Hi @McLuhan ,

if you get a m_data overflow, it means that your are running out of RAM. The linker has not enough RAM space available to fit all your variables, heap memory and stack space.

So if you increase the stack size, you will need more RAM for the stack, thus having an even bigger overflow. Not sure why this would be counter intuitive?

Giving an example: if you have a bus with 30 seats, and if you try to squeeze in 35 people, you have an overflow of 5. If you now try to increase one group of people from 5 to 8 to fit into that bus, you will have now an even bigger overflow 8 (5+3 = 8).

I hope that now makes sense,

Erich

View solution in original post

3 Replies
1,194 Views
ErichStyger
Specialist I

Hi @McLuhan ,

if you get a m_data overflow, it means that your are running out of RAM. The linker has not enough RAM space available to fit all your variables, heap memory and stack space.

So if you increase the stack size, you will need more RAM for the stack, thus having an even bigger overflow. Not sure why this would be counter intuitive?

Giving an example: if you have a bus with 30 seats, and if you try to squeeze in 35 people, you have an overflow of 5. If you now try to increase one group of people from 5 to 8 to fit into that bus, you will have now an even bigger overflow 8 (5+3 = 8).

I hope that now makes sense,

Erich

1,159 Views
myke_predko
Senior Contributor III

@McLuhan 

To explain the situation, you must realize that when you declare something outside a method, you are creating a global variable.  The RAM for global variables is coming from what is left over after the stack and other memory allocations take place.  So, if you decrease your stack size, you're increasing the amount of RAM that's "left over" leaving you with more space for your array.  

I should point out that an "int" array of 2x68 is not of insignificant size for a microcontroller.  It's 544 bytes and that must be contiguous - so, you could find that adding additonal (smaller) variables didn't have the out of memory problem which might have added to your confusion.  

Traditional systems have essentially unlimited memory (I'm being a bit facious and overly simplifying the memory management functions of the operating system but for all intents and purposes it's a true statement) and issues like this simply won't happen.  In microcontrollers, like the Kinetis, you must have a reasonabley good understanding of how memory is organized into different buckets as well as how things like variables are allocated memory within the overall scheme as well as the different buckets (like stacks).  

0 Kudos
Reply
1,170 Views
McLuhan
Contributor II

Thank you Erich!

I was incorrectly thinking that a higher number of variables would mandatory require a bigger stack size.

0 Kudos
Reply