stack heap and dynamic memory allocation

Nov 16, 2007 7 Replies

iam new to C and experienced with assembly language programming.



Typically in embedded C, which data is stored in heap?



If there are various threads/process like ADC, communication etc..when switching between different process where is the data being stored (stack or heap).



From where is this space for stack or heap allocated. Is it in RAM or elsewhere?


-VIVEK


Stack and heap space are both allocated in RAM.

Data that is local to functions is stored on the stack, and each thread (i.e. task) has it's own stack space.

The heap is there for applications programs that need to allocate objects at run-time whose lifetime isn't synchronized to any particular sequence of function calls, or even objects whose 'ownership' passes from one task to another.

Having said that, using the heap is usually frowned upon in an embedded system that has any pretense at all of being mission-critical.

Do a web search on stack, heap and heap fragmentation. You should find _something_ out there.

Tim Wescott Control systems and communications consulting http://www.wescottdesign.com Need to learn how to apply control theory in your embedded system? "Applied Control Theory for Embedded Systems" by Tim Wescott Elsevier/Newnes, http://www.wescottdesign.com/actfes/actfes.html

Stack and heap are implementation specific.

You may want to look it up from your embedded platform's C reference manual. Which CPU and OS are you using?

Allocation at runtime. But, not preferred in embedded.

Tasks have their own stacks.

Normally in RAM.

Karthik Balaguru

None at all. The heap is generally too unorganized to be trusted.

Neither. It's stored in static storage, because that's the only way it'll be found again, reliably.

What do you mean by "elsewhere"?

Nonsense. The heap user may be untrustworthy, though. For example, using the initialization routine to allocate a set of buffers, etc. from the heap, via malloc, is perfectly safe. The system just ceases allocation when enough buffers are present, or the malloc refuses. From thereon the malloc/free/realloc system can go unused.

Alternatively the programmer simply provides a reliable way to handle malloc refusal. This may involve releasing previously allocated memory. It is up to the programmer to ensure that this is always feasible.

Chuck F (cbfalconer at maineline dot net) Try the download section.

Iam using COLDFIRE processor from Freescale and using CODE WARRIOR compiler. Still not decided on the OS.

Also there are different segments like

text- code section- stored in FLASH

data section

bss-uninitialised global variables, constant data etc..

where are these stored?

Are constant data stored in EEPROM?

Join the Discussion

Have something to add? Share your thoughts — no account required.

Didn't find your answer?

Ask the community — no account required