Memory Allocation in Micro Controllers

Discussion in 'Embedded Systems and Microcontrollers' started by Allen_Lusy, Dec 10, 2014.

  1. Allen_Lusy

    Thread Starter New Member

    Dec 10, 2014
    10
    0
    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.
     
  2. Papabravo

    Expert

    Feb 24, 2006
    10,137
    1,786
    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.
     
  3. Allen_Lusy

    Thread Starter New Member

    Dec 10, 2014
    10
    0
    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?
     
  4. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    2,802
    832
    And a common answer is, "It depends on a specific Controller." So I guess there is no answer to your question.
     
  5. mitko89

    Member

    Sep 20, 2012
    123
    19
    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.
     
  6. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    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.
     
  7. Papabravo

    Expert

    Feb 24, 2006
    10,137
    1,786
    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.
     
  8. Papabravo

    Expert

    Feb 24, 2006
    10,137
    1,786
    Then you are asking irrelevant questions because in that context every answer is correct and is therefore of no practical use.
     
  9. MrChips

    Moderator

    Oct 2, 2009
    12,431
    3,360
    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.

    MCUs do not use code image. Code is stored in Flash.

    Code and constant literals are stored in flash.
    Variable data is stored in RAM. Heap and stack are allocated from RAM.

    See above.

    Local variables are allocated from the heap/stack. The space is deallocated when the function ends.

    Registers are created in hardware registers in the ALU.
     
    Allen_Lusy likes this.
  10. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    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.
     
  11. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    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.
     
  12. Papabravo

    Expert

    Feb 24, 2006
    10,137
    1,786
    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.
     
  13. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    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.
     
  14. Papabravo

    Expert

    Feb 24, 2006
    10,137
    1,786
  15. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    OK but thats pricing for a serious computation core, not a small controller.
     
  16. Papabravo

    Expert

    Feb 24, 2006
    10,137
    1,786
    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.
     
  17. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    Not if you have a barrel of nails.
     
  18. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    or if you are a big spider with 8 hands- 8 nails a time
     
Loading...