Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Dynamic memory allocation in standard c language

Status
Not open for further replies.

jab99407

Member
Hi

I think I have been tried my best to understand Dynamic memory allocation in standard c language. I found that dynamic memory allocation is not useful for small device such as micro-controller but its useful for large system such as PC

I have question When we have PC then why should we use dynamic memory instead of static memory ?
 
Static and Dynamic memory are different types of physical memory and shouldn't be confused with the term used in programming.

In programming,
Dynamic memory is memory that is allocated when needed and released when finished with.
Static memory is all the variables that are allocated at run time and only released when the program exits.

For more information, see malloc();

Mike.
Edit, didn't differentiate between local and global variables so as not to confuse the situation.
 
For microcontrollers or embedded systems, you can pretty much forget that dynamic memory allocation exists.

That's needed when the size of some data (or the quantity of line / values in it) is unknown or very variable & you also need to avoid claiming all memory as other programs are running via an operating system.
eg. If you were creating something like a word processor or database program to run on Windows/Linux/OSX/IOS/Android etc..

For a microcontroller setup where you define everything in advance of it running and nothing can ever be added, just use normal arrays and structures with appropriate sizes.
 
When we have PC then why should we use dynamic memory instead of static memory ?
Static and dynamic RAM are two types of physical memory storage in a computer.
Volatile Static RAM maintains its information indefinitely as long as power is applied.
Dynamic RAM only maintains the information for a few ms since each bit is stored on a small, leaky capacitor, so must be constantly refreshed to avoid losing data.
SRAM is faster and more expensive than DRAM; it is typically used for CPU cache while DRAM is used for a computer's main memory.
The reason DRAM is cheaper is that it has significantly fewer parts per stored bit compared to SRAM so can store much more information in a given chip area.

That has nothing to do with dynamic or static memory allocation in a programming language.
Either type of memory can be statically or dynamically allocated.
 
I have question When we have PC then why should we use dynamic memory instead of static memory ?
You can not judge between PC memory and microcontroller memory.. Two completely different beasts.

Virtually every chunk of memory in a PC is managed via a chip called the MMU.. Variables can be stored on virtual or physical memory, even the hard disk has paged memory...

Microcontrollers ie ARM 32bit and Pic32 are more likely to have dynamic memory usage.. But an 8 bit with max 4k, whats the point.
 
For microcontrollers or embedded systems, you can pretty much forget that dynamic memory allocation exists.

That's needed when the size of some data (or the quantity of line / values in it) is unknown or very variable & you also need to avoid claiming all memory as other programs are running via an operating system.
eg. If you were creating something like a word processor or database program to run on Windows/Linux/OSX/IOS/Android etc..

For a microcontroller setup where you define everything in advance of it running and nothing can ever be added, just use normal arrays and structures with appropriate sizes.

Microcontroller setups are not always static.

There are times during the initial boot up when control structures like numbers of buffers/control variables using a significant part of ram memory can be programmed to dynamic as an optimization of program code and structure even in a microcontroller. (size and number that fit into X sized total config space) like when the controller communicates with different types of devices it discovers (config switches) at bootup and only runs the needed functions that need memory for that configuration. The possible structure configuration pointers for each device/function are defined in software and then memory for the discovered device structures only are allocated once, manipulated with pointers and never deallocated. This is pretty common in control systems that need several types of I/O interfaces as it saves memory over purely static allocation of valuable ram resources.
 
Last edited:
Dynamic memory is allocated at run time where as static is allocated at compile time. when do you choose dynamic memory instead of static memory ? really don't understand reason when to select dynamic instead of static memory. perhaps explanation with one example may be describe better
 
Dynamic memory is used for items whose size is unknown at compile time such as the size of a file or document etc. It's not really used in embedded programs as usually all the memory is yours to do as you please. On bigger systems resources have to be shared and so dynamically allocating memory makes more sense.

Mike.
 
Dynamic memory is used for items whose size is unknown at compile time such as the size of a file or document etc.

Mike.
Hi Mike, Can you give an example Because i don't understand it ?

For example if we want to store salary of employee in a company because we do not how many employees working in company
 
See this page for examples of dynamic allocation. However, small microcontrollers don't normally support it as the overhead outweighs the gain. Just checked and XC8 (microchip C compiler) doesn't support malloc type functions.

Mike.
 
See this page for examples of dynamic allocation. However, small microcontrollers don't normally support it as the overhead outweighs the gain. Just checked and XC8 (microchip C compiler) doesn't support malloc type functions.

Mike.
I know dynamic memory allocation is not useful for small micro-controllers. Dynamic memory is used for array whose size is not fixed at compile time.

I understand that, but I am not able to make an real world example of a dynamic allocation. Can you give one real world example that can give solid understanding
 
One that comes to mind is such as a photo editor.

An image file can be anything from a few kb to many megabytes - and it is possible to open several images at once, in many programs.

For something like that, dynamic memory allocation to store the data array for each image opened would be pretty essential.
Each time an image is closed, the memory area used for that would be released again.

Another would be a "slippy map" program such as Google Maps.
The map is made of graphical "tiles", each covering a fixed area.
As you pan around the displayed map, every time you get near the edge of the display, new tiles are fetched from the map server - and each has a data structure giving such as its geographic position, zoom level and the visible image.

As you pan and zoom the map, possible hundreds or thousands of tiles may be loaded, each needing a new memory allocation for its data.
 
Let's say we have 40 bytes of memory and we are storing record of student marks so we can only store marks of 10 students only because one float variable takes 4 bytes to store one value. compiler will allocate memory for 10 students. we can't store the marks of student more then 10 students. but if we want to store marks of 15 students. Can we use dynamic memory to store marks for 15 students?
 
Let's say we have 40 bytes of memory and we are storing record of student marks so we can only store marks of 10 students only because one float variable takes 4 bytes to store one value. compiler will allocate memory for 10 students. we can't store the marks of student more then 10 students. but if we want to store marks of 15 students. Can we use dynamic memory to store marks for 15 students?

You 'could' I suppose, but it would be poor practice - in all cases (including less than 10) you keep the records in files on 'disc' storage (of whatever type).

But if you don't understand it, then don't use it - if you ever 'need' to it will become apparent.

A big problem with dynamic memory usage is 'losing' it, if everything isn't released correctly your available memory gets less and less, which is one reason that you need to reboot Windows occasionally.
 
You 'could' I suppose, but it would be poor practice - in all cases (including less than 10) you keep the records in files on 'disc' storage (of whatever type).

But if you don't understand it, then don't use it - if you ever 'need' to it will become apparent.

A big problem with dynamic memory usage is 'losing' it, if everything isn't released correctly your available memory gets less and less, which is one reason that you need to reboot Windows occasionally.
Can you explain why you used Dynamic by giving an example ? here is one but can you give simple example repleted to daily work
 
Don't use dynamic memory allocation in a controller to solve typical programming problems. There are specific narrow applications where it might be to some advantage when you're stuck with X size memory on X type chips and need to extra memory functionality beyond what purely static allocations can provide. A simple allocate only malloc() library is what most people use in those cases.

https://barrgroup.com/embedded-systems/how-to/malloc-free-dynamic-memory-allocation
Static memory preallocation
Projects that either do not need the complexity of a full heap or can't afford the risk of fragmentation, can use a technique that allows allocation, but not freeing. This means that after a program has completed its initialization code, the main loop of the program (or the loop of each of its tasks) will not allocate any further memory. This technique can be implemented with the normal malloc() routine, but I've always found it useful to write a custom version. My custom version has the following advantages over using the normal malloc() routine:

  • The overhead of the headers on each block is avoided.
  • The routine can be disabled once initialization is complete.
This technique also has the following advantages over declaring all memory globally.

  • Different start-up sequences can allocate memory to different purposes, without the programmer having to explicitly consider which items can be active simultaneously.
  • The namespace does not get polluted as much. In many cases, a pointer to an item may exist, but there is no need for a global or file static to exist for the item itself. Creating the global or file static allows access to the item from inappropriate parts of the code.
  • It is easy to transition to using a free() function later.
 
An array of unknown length is a good example.

let's say you have an I2c device collecting data and it has a buffer that allows one to read the device to see how many bytes are ready to transfer (TRANS_BYTES) before making the transfer (like a MAX30102 oximeter) , it would be nice to dynamically create an array of variable length [TRANS_BYTES] before transferring bytes. The ability to read the I2c when ready, makes it easier to get work done without constant interrupts.

You can dynamically assign array length in Java (and python?) interpreted languages but but forbidden in c++. (Compiled languages).
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top