Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think the comments have already covered this...</p> <p>To ask for memory means you have some operating system managing memory that you are mallocing from (using a loose sense of the term operating system). First you shouldnt be mallocing memory in a microcontroller as a general rule (I may get flamed for that statement). Can be done in cases but you are in control of your memory, you own the system with your application, asking for memory means asking yourself for it.</p> <p>Unless you have reasons why you cannot statically allocate your structures or arrays or use a union if there are mutually exclusive code paths that might both want much or all of the spare memory, you can try to allocate dynamically and free but it is a harder system engineering problem to solve.</p> <p>There is a difference between runtime allocation of memory and compile time. your example has nothing to do with the rest of the question</p> <p>int i=0; int d=3;</p> <p>the compiler at compile time allocates two locations in .data one for each of those items. the linker and/or script manages where .data lives and what its limitations are on size, if .data is bigger than what is available you should get a linker warning, if not then you need to fix your linker commands or script to match your system.</p> <p>runtime allocation is managed at runtime and where and how it manages the memory is determined by that library, even if you have plenty of memory a bad or improperly written library could overlap .text, .data, .bss and/or the stack and cause a lot of problems. </p> <p>excessive use of the stack is also a pretty serious system engineering problem which coming from non-embedded systems is these days often overlooked because there is so much memory. It is a very real problem when dealing with embedded code on a microcontroller. You need to know your worst case stack usage, leave room for at least that much memory if you are going to have a heap to dynamically allocate, or even if you statically allocate. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. COLet me see if I understood: if I use compile time allocation the linker will assign a portion of memory to each variable and that's it. If on the other end I use runtime memory allocation (for example using malloc) is the malloc library that manages this heap memory and ensures it works properly. Am I right? Btw if all this works this way it makes the linker and memory management librabries dependent on which microcontroller I'm using right?Thank you
      singulars
    2. COyes, variables like your i and d are managed by the compiler in the code generated by the compiler at compile time. at link time the specific addresses for things are applied to the code, machine instructions are patched up if needed with the specific addresses to things, etc. being in control of all of this you should know how big things are and/or prevent chunks of memory being used by the compiler so that it can be left as a heap for some flavor of alloc(), then you find or create an alloc() routine if needed to manage the heap at runtime
      singulars
    3. COwhich microcontroller to some extent determines some of the code, the hardware specific code so some code is already wanting to be microcontroller specific, you might have some application code that is generic. but definitely if you change microcontrollers the linker needs to link for memory regions specific to what is available on the target microcontroller. similar products from the same company usually have similar memory maps, but you cannot rely on that, so you cannot rely on one binary (output of linker) working on more than one flavor of microcontroller
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload