0
Dynamic memory
Anybody have a simpler explanation for Dynamic memory allocation and the term 'allocate' what does this means here?
2 Answers
+ 11
I can try....
You're program is chugging along, and suddenly it's asked to work on this chunk of data, say 5000 doubles. .... So, you ask the operating system: Hey, can I have that much memory? And assuming that there is enough spare space, then the OS dedicates the memory, and you get your array of doubles to calculate upon.
.... I hope you wrote your loops to be flexible enough to run through all those numbers!
I hope this helps, I figured the light/flippant take might suit, but if it doesn't help, we can make another go at it.
+ 2
A loose definition of memory allocation is when your program sets a chunk of memory aside of the predetermined size for that type to ensure that there will be memory to hold that variable. So let's say we declare an int variable and on that machine an int was 4 bytes. At least 4 bytes of memory is set aside to hold that variable.
So now in order to understand Dynamic allocation let's go over static allocation.
Static allocation can roughly be determined by the compiler. When your program is compiled the compiler can see the different types of variables that were declared in your program and calculate how much space in memory they will take up based on their types and the size those types are. So let's say you have 4 int variables declared at the top of your main function and each int takes up 4 bytes on your machine. The compiler can see this and knows that at least 16 bytes of memory will be needed to run your program. It can also see that they need to be in 4 sections of memory that are at least 4 bytes each. So when you run your program this memory is immediately allocated for your program.
Dynamic is on the fly. It is memory that the compiler can't completely determine its size prior to running your program so it is determined at run time. Let's say you had an object that was created every time the user clicked a button. There is no way for the compiler to determine how much memory (beyond maybe the first one) it will need for each addition object even though it may know the size of the object itself. So it is allocated as needed (Dynamic).
These aren't exact terms, but hopefully it helps with your understanding of what it means.