Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Dynamic Memory Allocation

Status
Not open for further replies.

alexz

Full Member level 5
Joined
Nov 19, 2004
Messages
283
Helped
6
Reputation
12
Reaction score
3
Trophy points
1,298
Location
UK
Activity points
2,246
Can anybody please explain and give some examples of why would we use the Dynamic Memory Allocation ?
I know what malloc(), heapInit(), and mfree() functions do.
I know that "heap" is the region defined for these purposes and has number of pre-defined segments.
But where would I need to use it?
 

I have an example:
When you need to stuff data in an array , and you are not sure of the array size because you are not sure of the number of data that you will recieve (From user or another program ). So you dont have to assign large amount of memory in a static array. Instead of that you can create a dynamic array (a linked list) , where you can allocate the memory dynamically for each new element of the array (on the fly). This way, you will only reserve the amount of memory you just need.
 

In most of the data-structure(stack,queue,trees,graphs) implementations we use dynmic memory allocation.
Becoz data grows at run time.
 

you can use it to allocate space for a interrupt driven message
 

ahmedsalah00 said:
I have an example:
Instead of that you can create a dynamic array (a linked list) , where you can allocate the memory dynamically for each new element of the array (on the fly). This way, you will only reserve the amount of memory you just need.

As precisely explained by ahmedsalah00 most of the usage of malloc falls in this category. You can find some more information on https://en.wikipedia.org/wiki/Malloc#Rationale.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top