stack heap and dynamic memory allocation

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

Reply to
vivek
Loading thread data ...

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
Reply to
Tim Wescott

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?

Reply to
Laguna

Allocation at runtime. But, not preferred in embedded.

Tasks have their own stacks.

Normally in RAM.

Karthik Balaguru

Reply to
karthikbalaguru

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"?

Reply to
Hans-Bernhard Bröker

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.
Reply to
CBFalconer

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

Reply to
vivek

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?

Reply to
vivek

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.