Why Use Dynamic Memory Allocation?

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 12031 Views - Last Post: 10 April 2010 - 12:00 AM Rate Topic: -----

#1 Israel  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 7
  • View blog
  • Posts: 818
  • Joined: 22-November 04

Why Use Dynamic Memory Allocation?

Posted 23 March 2010 - 09:01 PM

I have no code to show here because I can use the syntax fine. I understand what things in C like malloc and free do. I know that malloc will allow me to set something in the heap instead of the stack. But I've looked around and can't really find what the advantage of this is. I've got a guess or two, but I'd rather be right. What is the advantage of this?
Is This A Good Question/Topic? 0
  • +

Replies To: Why Use Dynamic Memory Allocation?

#2 Guest_c.user*


Reputation:

Re: Why Use Dynamic Memory Allocation?

Posted 23 March 2010 - 09:39 PM

the heap is accessible from any place

you can allocate in one part of a program
and get those data from any other part

This post has been edited by c.user: 23 March 2010 - 09:41 PM

Was This Post Helpful? 0

#3 Israel  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 7
  • View blog
  • Posts: 818
  • Joined: 22-November 04

Re: Why Use Dynamic Memory Allocation?

Posted 23 March 2010 - 09:46 PM

the heap is accessible from any place


Are you saying the stack is not? Seems when I run from the stack I get the same accessibility?
Was This Post Helpful? 0
  • +
  • -

#4 muballitmitte  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 174
  • View blog
  • Posts: 470
  • Joined: 05-November 08

Re: Why Use Dynamic Memory Allocation?

Posted 23 March 2010 - 11:33 PM

It`s not that much about accessibility. If you want to debate than it should be about the trade-off between the speed of static allocation and the flexibility of the dynamic one. Basically, you *should* use static memory allocation when you know in advance how much memory is needed. In this way the memory is allocated once and released upon exit.
Now if with static allocation all the memory needed is known at compile time with dynamic allocation the process of allocation is deffered to runtime. Thus with new and malloc you get the possibility of allowing your data structures to grow in time as your needs do. Plus because everything happens during the execution when some memory used is no longer needed one can free it with delete or free.

This post has been edited by muballitmitte: 23 March 2010 - 11:41 PM

Was This Post Helpful? 1
  • +
  • -

#5 Guest_c.user*


Reputation:

Re: Why Use Dynamic Memory Allocation?

Posted 24 March 2010 - 12:42 AM

Israel said:

Are you saying the stack is not?

if you use an automatic variable it deletes oneself after the function, in which it was created, returns
and dynamic memory stays accessible while it has not freed manually by free() (delete for C++)
you can make a static variable also inside a function, it will store the last value wrote within, but you can't remove this variable till the end and use the place, where it was placed before, repeatedly (as with dynamic)
you can't create an automatic variable, then return its address to upper function and then use it by the address, because the variable has been destroed after the call to a function, where it was created (when you create a variable in a function it is automatic by default)

This post has been edited by c.user: 24 March 2010 - 12:52 AM

Was This Post Helpful? 1

#6 Israel  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 7
  • View blog
  • Posts: 818
  • Joined: 22-November 04

Re: Why Use Dynamic Memory Allocation?

Posted 25 March 2010 - 12:03 PM

Quote

Basically, you *should* use static memory allocation when you know in advance how much memory is needed.


Is that because the heap is more flexible? Or is it just better to split your needed resources over two parts of RAM instead of one?

Quote

if you use an automatic variable it deletes oneself after the function, in which it was created, returns
and dynamic memory stays accessible while it has not freed manually by free() (delete for C++)


So your saying by using "free" that the space memory will be cleared faster? That makes sense.
Was This Post Helpful? 0
  • +
  • -

#7 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Re: Why Use Dynamic Memory Allocation?

Posted 25 March 2010 - 12:27 PM

Israel, my apologies if this sounds patronising, but having been brought up in a world where dynamic memory allocation did not exist, nor was even on the horizon, I can tell you I would not like to go back there!

Dynamic memory allocation permits us to allocate the memory we need to process information. We allocate what we need to allocate, no more and no less. Memory is an expensive commodity even today. My machine has 4GB of memory. When it's all been used by programs that have allocated more memory than they actually needed through global static variables, then the machine slows down to the point of being unusable. That's how important dynamic memory allocation in terms of malloc/free and new/delete are to us. Allocate it when we need it. Delete it when we don't need it any more!

Quote

So your saying by using "free" that the space memory will be cleared faster? That makes sense


That statement is incorrect!

This post has been edited by Martyn.Rae: 25 March 2010 - 12:28 PM

Was This Post Helpful? 3
  • +
  • -

#8 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1112
  • View blog
  • Posts: 4,619
  • Joined: 09-June 09

Re: Why Use Dynamic Memory Allocation?

Posted 25 March 2010 - 01:32 PM

When a program is first compiled/ran the predefined memory is allocated on the stack. After this no more memory can be allocated or deallocated from the stack until the program ends (the stack is cleared). C++ lets you create dynamic memory which allocates memory on the heap during run time. That way if there on uncertainties on how much memory is need for a program, you can use the
new 


operator to allocate more memory for your program.

example

int x;
cin>>x;

int arr[x]; //create an array !!! DOESN'T COMPILE!!!



The code does not compile because the program will try allocate a unknown memory for the array on the stack, this is when you need to create dynamic memory.

int x;
cin>>x;

int *arr = new arr[x];

delete []arr; //dont forget to clean up



This example allocates memory for the array on the heap rather than the stack. When using dynamic memory its good practice to clean up after your self by using the delete operator. Hope that helps :)

EDIT : if your using C its the same deal but now you just use malloc and calloc and free()

This post has been edited by ImaSexy: 25 March 2010 - 01:33 PM

Was This Post Helpful? 1
  • +
  • -

#9 Guest_c.user*


Reputation:

Re: Why Use Dynamic Memory Allocation?

Posted 25 March 2010 - 10:47 PM

Israel said:

So your saying by using "free" that the space memory will be cleared faster? That makes sense.

when you use free() you just have control over the memory which you prepared for oneself
you can save something to the heap and hold it there for a long time
automatic variables are temporary, they are being while the function where they were created is working

don't use free() while the thing in the heap is useful
Was This Post Helpful? 0

#10 Israel  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 7
  • View blog
  • Posts: 818
  • Joined: 22-November 04

Re: Why Use Dynamic Memory Allocation?

Posted 25 March 2010 - 11:02 PM

Quote

Israel, my apologies if this sounds patronising, but having been brought up in a world where dynamic memory allocation did not exist, nor was even on the horizon, I can tell you I would not like to go back there!


I did not take any offense. By the way, I'm in no way bashing memory allocation, I just want to have good understanding of it :)

Quote

That statement is incorrect!


Fair enough, I must have misinterpreted what c.user said...

Quote

We allocate what we need to allocate, no more and no less.


So what normally happens? Does the compiler or program at runtime sometimes allocate too much?

ImaSexy makes a good point but wouldn't it be just the same to set that array size like:

int myArray[50];


Wouldn't setting the necessary size do the same thing too without having to dive into memory allocation?

This post has been edited by Israel: 25 March 2010 - 11:07 PM

Was This Post Helpful? 0
  • +
  • -

#11 elgose  Icon User is offline

  • D.I.C Head

Reputation: 102
  • View blog
  • Posts: 228
  • Joined: 03-December 09

Re: Why Use Dynamic Memory Allocation?

Posted 25 March 2010 - 11:30 PM

Quote

Wouldn't setting the necessary size do the same thing too without having to dive into memory allocation?

I've just recently started using dynamic memory allocation, so my understanding is very basic.

But imagine you made a program for a teacher to input the grades for all her students. You store the grades she inputs into an array... now, she might have a small class of 5, or she may input it for all her students for the last 3 semesters (say 400 students). With dynamic memory allocation your program can handle both without changing a line of code, and without setting aside memory you won't use (or, say you make an array of 400 - what if she had 500 students? Now there's a problem).

Hope that helps!
Was This Post Helpful? 1
  • +
  • -

#12 muballitmitte  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 174
  • View blog
  • Posts: 470
  • Joined: 05-November 08

Re: Why Use Dynamic Memory Allocation?

Posted 26 March 2010 - 12:00 AM

Quote

Wouldn't setting the necessary size do the same thing too without having to dive into memory allocation?


I think everyone is forgetting one last detail: the stack size of limited (not that the heap is not, but its limitation is given by the total amount of memory available (page and physical)). Allocating large (either with many components or not that many, but large components) arrays on the stack will cause an overflow.

While it might be true that memory is still an expensive commodity, in some cases it is better to use as much memory as you can in order to save some computational time. This is true in scientific computing for example. Bottom line is use dynamic allocation and don`t always be afraid of memory usage.

Also, Israel it is not a good idea to be talking about RAM here. For example, in windows there is no real guarantee that what you allocate will end up in the main memory. you might often find yourself using the pagefile.

This post has been edited by muballitmitte: 26 March 2010 - 12:11 AM

Was This Post Helpful? 1
  • +
  • -

#13 Israel  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 7
  • View blog
  • Posts: 818
  • Joined: 22-November 04

Re: Why Use Dynamic Memory Allocation?

Posted 26 March 2010 - 12:33 PM

Quote

Allocating large (either with many components or not that many, but large components) arrays on the stack will cause an overflow.


Ok. But I know there are Heap Overflows too.


Quote

Also, Israel it is not a good idea to be talking about RAM here. For example, in windows there is no real guarantee that what you allocate will end up in the main memory. you might often find yourself using the pagefile.


True.

Quote

Allocating large (either with many components or not that many, but large components) arrays on the stack will cause an overflow.


Ok. But I know there are Heap Overflows too.


Quote

Also, Israel it is not a good idea to be talking about RAM here. For example, in windows there is no real guarantee that what you allocate will end up in the main memory. you might often find yourself using the pagefile.


True.
Was This Post Helpful? 0
  • +
  • -

#14 Israel  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 7
  • View blog
  • Posts: 818
  • Joined: 22-November 04

Re: Why Use Dynamic Memory Allocation?

Posted 08 April 2010 - 10:11 AM

No takers?
Was This Post Helpful? 0
  • +
  • -

#15 kingfeanor  Icon User is offline

  • D.I.C Head

Reputation: 44
  • View blog
  • Posts: 60
  • Joined: 18-April 09

Re: Why Use Dynamic Memory Allocation?

Posted 08 April 2010 - 10:25 AM

Israel,
If the statements above haven't helped you understand, then nothing I write will either.

As a student learning how to program I thought pointers and memory allocation and deallocation was confusing and perhaps pointless. Then I hit my data structures class and had to program red-black trees, linked lists, graphs, and so on. I had to write a program that could output statistics on a given text (which was over 15mb). I quickly learned the value of heap memory allocation doing these things. And the performance value became crystal clear when my text analysis program could run in 5 min using all pointers and dynamic memory allocation but other student's programs which didn't use pointers took hours to complete. You will understand the need for dynamic memory management when you are faced with a problem you have to solve that would be difficult or impossible without it. Until you face and solve that problem, all this discussion about the value is quite nebulous.

My advice? Go read a good C or C++ data structures book or take a data structures class (in C or C++). Afterward, I doubt you will have any questions.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2