19 Replies - 6060 Views - Last Post: 10 April 2010 - 12:00 AM
#1
Why Use Dynamic Memory Allocation?
Posted 23 March 2010 - 09:01 PM
Replies To: Why Use Dynamic Memory Allocation?
#2 Guest_c.user*
Re: Why Use Dynamic Memory Allocation?
Posted 23 March 2010 - 09:39 PM
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
#3
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?
#4
Re: Why Use Dynamic Memory Allocation?
Posted 23 March 2010 - 11:33 PM
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
#5 Guest_c.user*
Re: Why Use Dynamic Memory Allocation?
Posted 24 March 2010 - 12:42 AM
Israel said:
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
#6
Re: Why Use Dynamic Memory Allocation?
Posted 25 March 2010 - 12:03 PM
Quote
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
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.
#7
Re: Why Use Dynamic Memory Allocation?
Posted 25 March 2010 - 12:27 PM
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
That statement is incorrect!
This post has been edited by Martyn.Rae: 25 March 2010 - 12:28 PM
#8
Re: Why Use Dynamic Memory Allocation?
Posted 25 March 2010 - 01:32 PM
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
#9 Guest_c.user*
Re: Why Use Dynamic Memory Allocation?
Posted 25 March 2010 - 10:47 PM
Israel said:
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
#10
Re: Why Use Dynamic Memory Allocation?
Posted 25 March 2010 - 11:02 PM
Quote
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
Fair enough, I must have misinterpreted what c.user said...
Quote
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
#11
Re: Why Use Dynamic Memory Allocation?
Posted 25 March 2010 - 11:30 PM
Quote
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!
#12
Re: Why Use Dynamic Memory Allocation?
Posted 26 March 2010 - 12:00 AM
Quote
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
#13
Re: Why Use Dynamic Memory Allocation?
Posted 26 March 2010 - 12:33 PM
Quote
Ok. But I know there are Heap Overflows too.
Quote
True.
Quote
Ok. But I know there are Heap Overflows too.
Quote
True.
#15
Re: Why Use Dynamic Memory Allocation?
Posted 08 April 2010 - 10:25 AM
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.
|
|

New Topic/Question
Reply




MultiQuote






|