What's benefit of dynamic memory allocation over static memory

Go To Last Post
19 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi

 

I am reading chapter "dynamic memory allocation in c language" in text book. I do not understand meaning of statement " When we don't know how much memory we actually need then we use dynamic memory allocation"

 

Can somebody clarify meaning of statement with example? What's benefit of dynamic memory allocation over static memory?

 

Last Edited: Sun. Apr 5, 2020 - 05:40 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Dynamic memory is allocated at run time vs static being allocated at compile time.

Dynamic allocation of memory has to be managed. This requires code and memory.

Google heap fragmentation

Last Edited: Sun. Apr 5, 2020 - 05:49 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

As I understand it, C language dynamic memory allocation essentially assumes that there will always be enough memory for what ever you want to do. For small micros, that is rarely true.

 

Also, I would argue that in small systems, it is rarely true that you don't know how much memory is needed for various tasks. Often, the biggest memory user is text buffering for serial input and output processes. Usually, you can put an upper bound on the needed memory. Even if you don't always need that much memory, it is also often the case that you won't know WHEN that upper bound is  going to be tested. So, you just create a memory space in the form of a string that is big enough for the maximum-size case, and use it, and use, and use it, with the comfort that it will always be big enough. Then, while developing, you periodically check that memory space you provided has not been over-run. 

 

Jim

 

Until Black Lives Matter, we do not have "All Lives Matter"!

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

On, say, a multi-user or multi-processing machine like a common PC, dynamically allocating memory means that that memory that you don't allocate can be used by some other user or program.   You don't want a web browser limited to 10 tabs, and you don't want it to use up enough memory for 40 tabs even if you're only using 5.

 

On a large embedded system like a network router, dynamic memory lets you respond to actual configuration (hardware or software) dynamically.  If you have 7 slots and stick 4-port ethernets in them, your memory needs are different than if you have 8port serial cards in each slot, but you probably won't need that X.25 info.  Design things "right" and you'll run out of performance just about the same time you run out of memory, both of which will be somewhat beyond "typical" configurations.

 

On a small microcontroller, dynamic memory allocation is frequently more trouble than it's worth.  A common usage is for strings.   A common problematic usage is also strings - it turns out that if you keep appending characters to a string (as they're received from a serial port, for example) using dynamic allocation, you frequently end up with a 10byte memory chunk (free), an 11byte memory chunk (also free), ... and eventually your current 20byte string.  A GOOD string library and memory allocation will figure out how to collapse all of those free-but-short chunks of memory into one big chunk, but it seems that it frequently doesn't work out that way.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ka7ehk wrote:
you don't know how much memory is needed for various tasks.

consider we want to store the age of family members but we don't know how many members in the family so there may be three, four or don't know exactly how many 

 

This is what the first post statement say if we don't know how much memory actually need we use dynamic memory so How dynamic memory is useful for this task ?

Last Edited: Sun. Apr 5, 2020 - 06:21 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

You're reading  book on 'C', doesn't that tell you?

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Three family member, or four, or 5, make it 10 and be done with it. Or 20 or what ever is likely to be the upper bound among all your customers for your gizmo. 

 

You have to have enough memory for the largest possible case, so why not just set that aside at the start and be done with it?

 

Of course, if this is a big program (say, a tax compiling program, which I should be using right now), its a whole different case. But if this thing remembers what each family member likes for the temperature of their tea, then its no big deal.

 

So, one needs to add: It All Depends.

 

Jim

 

Until Black Lives Matter, we do not have "All Lives Matter"!

 

 

Last Edited: Sun. Apr 5, 2020 - 06:31 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

There are two option to store information, static memory and dynamic memory when do you choose dynamic memory instead of static memory ?

 

I found conclusion that when we know the exact size then we use static memory and when we don't know how much memory actually need to store the information then we use dynamic memory

 

so really don't understand reason why to select dynamic instead of static memory. perhaps one example may be describe so I gave my example What will your example ?        

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Say you're writing an invoicing program - how many line items in an order? It could be one, or it could be a million or anything in between. You can statically allocate memory for 1 million items which would be wasteful if there is only actually 5 items, or you dynamically allocate memory to suit.  Memory is a resource, you don't want to waste it.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

How many invoicing programs run on an 8 bit micro with 2K of RAM? Or 8K? Or even a 16 bit micro? 

 

So, it really depends on the context. 

 

If this whole question is an abstract one, then Kartman's comments are as good as any. BUT, this is an AVR forum, and that implies a certain range of contexts. The immense bulk of those point to static allocation, whether or not you absolutely know the required memory size.

 

Jim

 

Until Black Lives Matter, we do not have "All Lives Matter"!

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

muke12 wrote:
I found conclusion that when we know the exact size then we use static memory and when we don't know how much memory actually need to store the information then we use dynamic memory

That conclusion is too simplistic.

In practise you might allocate static memory to cover a reasonable majority of your use cases:

So in your family members example you don't know exactly how many members a user may enter ages for, but on a limited memory system you might choose a limit of 8 for a single family unit. For a PC where memory is almost unlimited you might choose a silly limit of 128 members and be done with it.

 

So if you chose a limit of 8 members; when the user tries to enter the age for a ninth family member you could either:

  1. Throw an error "This program handles only a maximum of 8 family members"
  2. Dynamically allocate memory for another 8 members. (With checking for "Out-of-Memory" of course)

 

Again, in practise, on a small embedded micro-controller system (not Wi-Fi routers etc.) you would do everything within your power to NOT use dynamic memory. But this could be a topic for your next question.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Sheikh Mohammed Al Maktoum of Dubai: six wives, 30 children and a 14-year reign of control

You will need a bigger limit.

 

Mind you,   he must get a lot of ear-ache.

 

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

A recent thread showed a practical application of dynamic allocation...

 

https://www.avrfreaks.net/commen...

 

The dynamic allocation here is creating "nodes" in a linked list to record what numbers (and how many of each) are seen in arbitrary set of data.

 

In a later post I explained how that works..

 

https://www.avrfreaks.net/commen...

 

The point in that example is that until you start to analyze the data you can't really know how many different numbers you might encounter so it's not like you can preallocate an array or something as you don't know, until you start, how many different numbers there are going to be.

 

Of course this example just creates and grows the list (with malloc()), what it doesn't show is how free() might be used to then release memory that has been allocated once it's no longer needed.

Last Edited: Sun. Apr 5, 2020 - 12:03 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

"Dare to be naïve." - Buckminster Fuller

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

There was a long thread recently all about Dynamic memory allocation - by someone else who seems to be just picking random programming topics and throwing them on the forum:

 

https://www.avrfreaks.net/forum/dynanamic-memory-allocation-what-malloc-does

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

muke12 wrote:
"When we don't know how much memory we actually need then we use dynamic memory allocation"

There's a very important detail missing from that premise!

 

Dynamic allocation is most commonly used in cases where not only do you not know how much memory you need - but you also don't know how much memory you have.

 

And, as has already been noted, the latter is very rarely the case on embedded microcontrollers.

 

See #9 in the thread I mentioned above.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:

There was a long thread recently all about Dynamic memory allocation - by someone else who seems to be just picking random programming topics and throwing them on the forum:

 

https://www.avrfreaks.net/forum/dynanamic-memory-allocation-what-malloc-does

Thank you for being so gracious in the manner in which you pointed out my thread

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Seems it's not "someone else":

 

https://www.avrfreaks.net/commen...

 

frown

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Sorry to put this bluntly but we're either being deliberately trolled or we are clearly wasting our time as OP (all three of them!) seem unable to take on board anything explained to him/them.

 

Having said that, playing Devil's advocate I *suppose* it could be three different students on the same course in the same location maybe?