Memory Allocation in Micro Controllers

Thread Starter

Allen_Lusy

Joined Dec 10, 2014
12
I know I'm asking a basic Question. But it will be very useful for me to continue my Micro-controller Experience. Consider I am using a controller with 256 KB Flash and 8 KB Ram. My Questions are

  1. I know about memory mapping like code, Data, Stack and Heap Segment, which variable will store in which segment. But are these allocated in RAM or Flash(Typically i want to know, if i am executable image of Controller, after flashing it, how the Memory will Allocate in RAM and Flash)

  2. If I am declaring a buffer, I know it is allocating in RAM. Whether it will deallocate after the function ends (if that Buffer is Locally declared)

  3. Where the Register Variable will allocate the memory.
 

Papabravo

Joined Feb 24, 2006
21,228
Can you be more specific about which microcontroller and which languages you are using? The questions require specific answers that it is impossible to give without more information.
 

Thread Starter

Allen_Lusy

Joined Dec 10, 2014
12
I am asking a common question. Not to a specific Controller. I want to know typically, if i compiled a application image and burning/flashing in to the controller. Then how the memory will allocate.
1) Where the image will sit, RAM or Flash?
2) The memory mapping Segment (Code, Data, Stack and heap) is segmented in RAM or Flash?
3) What is the use of RAM in Micro-controller? And use of Flash in Micro-controller?
 

mitko89

Joined Sep 20, 2012
127
1. RAM is for variables and data that will change over the process of execution of your program, here goes the data. So, the code goes to ROM. This is where you explain to the uC what you want him to do.
2. A locally declared buffer or any local variable will preserve its value as long as you are in the particular function (if you want your variables to preserve their values, declare them as static).
3. Register variables are in RAM, well... those are "fast" variables. Nowadays you don't really care about declaring a variable as register, because the comipler assigns it to particular variables and does the optimization for you.
 

takao21203

Joined Apr 28, 2012
3,702
Its not a good question since its different on different controllers.

1) Where the image will sit, RAM or Flash?
2) The memory mapping Segment (Code, Data, Stack and heap) is segmented in RAM or Flash?
3) What is the use of RAM in Micro-controller? And use of Flash in Micro-controller?

These questions are somehow unintelligible.

1) I havent yet heard of an image that sits in RAM. Few controllers can execute from RAM.
2) Code, Data, Stack and heap isnt known as "memory mapping segment". there isnt neccessarily any heap or stack or data.
3) The simplest PICs just have a small register file. It is used as register file. The program is in the FLASH exclusively. Techniccally, the register file is a small RAM.

You somehow demonstrate that you dont understand much about controllers, read the wikipedia article on it as a starter.
 

Papabravo

Joined Feb 24, 2006
21,228
Its not a good question since its different on different controllers.

1) Where the image will sit, RAM or Flash?
2) The memory mapping Segment (Code, Data, Stack and heap) is segmented in RAM or Flash?
3) What is the use of RAM in Micro-controller? And use of Flash in Micro-controller?

These questions are somehow unintelligible.

1) I havent yet heard of an image that sits in RAM. Few controllers can execute from RAM.
2) Code, Data, Stack and heap isnt known as "memory mapping segment". there isnt neccessarily any heap or stack or data.
3) The simplest PICs just have a small register file. It is used as register file. The program is in the FLASH exclusively. Techniccally, the register file is a small RAM.

You somehow demonstrate that you dont understand much about controllers, read the wikipedia article on it as a starter.
This is not accurate. There are many controllers that read the code from a serial device and execute from RAM. They do this because RAM is faster.
 

Papabravo

Joined Feb 24, 2006
21,228
I am asking a common question. Not to a specific Controller. I want to know typically, if i compiled a application image and burning/flashing in to the controller. Then how the memory will allocate.
1) Where the image will sit, RAM or Flash?
2) The memory mapping Segment (Code, Data, Stack and heap) is segmented in RAM or Flash?
3) What is the use of RAM in Micro-controller? And use of Flash in Micro-controller?
Then you are asking irrelevant questions because in that context every answer is correct and is therefore of no practical use.
 

MrChips

Joined Oct 2, 2009
30,824
Microcontrollers (MCU) historically are scaled down computers and have limited resources. Hence they do not by necessity use memory management techniques as employed in PCs.

Memory in an MCU consists of Registers, RAM and ROM (flash). Some MCUs may include a small amount of EEPROM and/or battery-backed RAM.

1) Where the image will sit, RAM or Flash?
MCUs do not use code image. Code is stored in Flash.

2) The memory mapping Segment (Code, Data, Stack and heap) is segmented in RAM or Flash?
Code and constant literals are stored in flash.
Variable data is stored in RAM. Heap and stack are allocated from RAM.

3) What is the use of RAM in Micro-controller? And use of Flash in Micro-controller?
See above.

If I am declaring a buffer, I know it is allocating in RAM. Whether it will deallocate after the function ends (if that Buffer is Locally declared)
Local variables are allocated from the heap/stack. The space is deallocated when the function ends.

Where the Register Variable will allocate the memory.
Registers are created in hardware registers in the ALU.
 

ErnieM

Joined Apr 24, 2011
8,377
Again, this is all very compiler and processor dependent.

Temporary variables such as local variables inside functions may be created and released on the stack IF a stack exists. For times when there is no stack they are placed in normal RAM like any other variable, you just can’t refer to it by name in other routines but you could possibly read/write it. You can tell by making some dummy locals and watch the memory size rise on the next compile.

Register variables are suggestions to the compiler to use a register inside the CPU. That assumes there are such registers and they are free. C will ignore the suggestion if it has no room.
 

takao21203

Joined Apr 28, 2012
3,702
This is not accurate. There are many controllers that read the code from a serial device and execute from RAM. They do this because RAM is faster.
not so many controllers do that except high end one's.

Remember a controller with on-purpose external memory isnt a controller- its a CPU.
Or you may call a tablet CPU a controller because so much is integrated.

None of the smaller 8bit MCU's does what you describe. Neither is RAM "faster". Typically its very scare, there is no DRAM just SRAM. Even if possible, you can only run a tiny code slice.

Anyway, its not making sense to argue without to agree on some product line- then consider its merits.

A typical controller doesnt execute from RAM, even if some high-end one's can. If you have exernal memories, its rather a CPU.
 

Papabravo

Joined Feb 24, 2006
21,228
not so many controllers do that except high end one's.

Remember a controller with on-purpose external memory isnt a controller- its a CPU.
Or you may call a tablet CPU a controller because so much is integrated.

None of the smaller 8bit MCU's does what you describe. Neither is RAM "faster". Typically its very scare, there is no DRAM just SRAM. Even if possible, you can only run a tiny code slice.

Anyway, its not making sense to argue without to agree on some product line- then consider its merits.

A typical controller doesnt execute from RAM, even if some high-end one's can. If you have exernal memories, its rather a CPU.
What is low end and high end is getting very blurry. Many of the DSPs from Analog Devices have 3 Meg or more of on chip RAM and their prices are certainly competitive with what we used to call low end. I agree that the original questions are impossible to answer unless we have a specific example. I was just trying to make the point that all of the supposed answers are possible if not probable with the current state of technology. If we're talking about soft controllers inside an FPGA things get even murkier because they are almost always downloaded from serial memory at RESET and they have a ton of onboard RAM. Your world view of technology is sounding a bit dated, but it is the way it used to be.
 

takao21203

Joined Apr 28, 2012
3,702
What is low end and high end is getting very blurry. Many of the DSPs from Analog Devices have 3 Meg or more of on chip RAM and their prices are certainly competitive with what we used to call low end. I agree that the original questions are impossible to answer unless we have a specific example. I was just trying to make the point that all of the supposed answers are possible if not probable with the current state of technology. If we're talking about soft controllers inside an FPGA things get even murkier because they are almost always downloaded from serial memory at RESET and they have a ton of onboard RAM. Your world view of technology is sounding a bit dated, but it is the way it used to be.
I was guessing you had FPGA in mind, they arent however $2 devices with low pin count.

Not all 8bit controllers are dated there are new developements, with USB, logic cell, DA converters and so on.

Sure DSP with 3 MB just static RAM...guess what one chip costs.

A Microcontroller as such for costs reasons has it all integrated into a single IC, sure DSPs and FPGA and so on exist, but they arent so common and you cant buy them easily, the softwares are expensive...

The OP raised topics such as "heap" and "stack", which most Controllers dont have, besides a small hardware stack.

There is no good answer possible.

Memory mapping? Only some ICs use it, and its totally specific, no sense making answer here either.
 

Papabravo

Joined Feb 24, 2006
21,228
OK but thats pricing for a serious computation core, not a small controller.
I don't think the TS/OP was suggesting a small controller with his original question. Come to think of it we don't really know what he was thinking about. Another thread as useless as a bag of hammers.
 
Top