cancel
Showing results for 
Search instead for 
Did you mean: 

Heap and Stack

lgiordano2
Associate II
Posted on November 09, 2006 at 08:11

Heap and Stack

6 REPLIES 6
lgiordano2
Associate II
Posted on November 08, 2006 at 12:17

Hi, I am using CrossStudio. Microcontrollers are STR710 and STR711.

I am not an expert at C programming, but I have some idea.

Where are variables placed? heap or stack? (I mean those variables which are not explicity placed on a section)

I have been debugging, and I see some are placed in heap, while some others in stack. I am referring to 'auto' variables, those declared inside a function, and are not 'static'.

This is my real problem:

Sometimes (not very often) my app hangs up. I found out that my stack and heap size are 1024 bytes each. However, in some situation, my app declares some buffers and variables which are bigger than 1024 bytes.

By debugging, I saw those variables were in heap section, which is next to stack section. So, although variables are overlapping over stack section, and as stack is a 'stack' (it 'fills' in reverse order) while heap fills in 'normal' order, overlapping is in a free zone of stack. However I think that in some situation, that stack zone is used and if it gets overlapped app may crash. I am right?

When this happened, I increased stack size to 4096 (when I did it, I didnt know what heap exactly is). So, it started to work fine. I think that it indirectly increased the heap section, because as stack was bigger, the useful stack was more separated from heap.

That is why I need to understand where are automatic variables placed.

By the way, I think i have 2 solutions:

1) Optimize code, so not too much variables are declared.

2) Increase heap (or stack) section.

I think I will do both.

I am waiting for comments.

Regards,

sjo
Associate II
Posted on November 08, 2006 at 13:35

With a heap, you access what you are storing in any order you want and they stay there until you remove them, eg. malloc, free

With a stack, you normally access them in the reverse order you put them on the stack and they are automatically removed as you access them, eg. local variables, function parameters.

Regards

sjo

lgiordano2
Associate II
Posted on November 08, 2006 at 17:16

yeah, you are right. It seems that variables in my stack are 'invading' heap. I will increase stack section size. By the way, I DONT use malloc, calloc, etc functions, should I eliminate heap? As i will increase stack, is there any other thing to take into consideration?

Regards,

kleshov
Associate II
Posted on November 09, 2006 at 02:28

The compiler places some auto variables in registers, the rest of them in stack. So your stack MUST be big enough to accommodate auto variables in all usage patterns (consider recursion and interrupts). Stack overflows can lead to very obscure and hard-to-debug bugs.

If you are not using malloc, the C++ new operator and such, you can set heap size to 0.

Regards,

- mike

sjo
Associate II
Posted on November 09, 2006 at 06:33

Probably the easiest method to check stack useage is to prefill with a known signature (eg. 0x55555555), then after running the program check that their are still some signature bytes left.

This is not 100% as program execution can change but it is a good indication of stack useage.

Regards

sjo

lgiordano2
Associate II
Posted on November 09, 2006 at 08:11

thanks for answering. I will check my code to have an idea of how big has stack to be. I dont have recursive functions, and interrupt ones are just a few. By the way, how much stack is needed just to make a function call? I mean, variables and parameters are placed in stack, but when a function is called, some other things are placed in stack (the return address, etc.) Is it detailed somewhere? For instance, if main() has no variables, and calls a functions which has no arguments, what needs that call?

Regards,