Dynamic Memory

Sep 19, 2011 at 9:05pm
I have been reading a book and it is talking about the efficiency in using dynamic memory allocation because memory can be used and freed up as needed. When you use a standard data type (i.e. int, double, float, etc.) does this same principle not happen when a variable passes in and out of scope? If not, why wouldn't you want to use dynamic memory all of the time so that memory is only being used on an as-needed basis? Thanks for your input.
Sep 19, 2011 at 9:14pm
Dynamic memory is more about creating things when you don't know at compile time how many you'll be creating.

Say for example you want to keep track of all the test scores of some students. You might use a simple array:

 
int testscores[50];


Simple enough. but what if there are more than 50 students? The above code won't work because it only makes space for 50 scores.

What if there's no way to know how many students there are? For example, let's say the user provides that info. For that, we'd need to allocate the memory dynamically:

1
2
3
4
5
6
7
int numberofstudents = GetNumOfStudentsFromUser();

int* testscores = new int[numberofstudents];

//...

delete[] testscores;


No matter how many students the user provides, this code will allocate enough memory to hold all their test scores.
Sep 19, 2011 at 9:25pm
When a variable passes out of scope is the memory freed up in the same way that the dynamic memory would be with delete (My inclination is that this is not the case)? I have also read that when a dynamically allocated variable passes from scope its memory is released without having to use the keyword delete (although to me it is much safer and clearer to manually free up this memory regardless).

I guess to provide an example of my orginal question, suppose my program uses 4 integer variables all at different times. So I would declare these variables say at the beginning of main(). This would use up 16 bytes of memory (with most compilers). Since none of these variables are ever used for the same time and they are only used for a very limited period of time lets say, would it not be more efficient to use dynamic memory, use a pointer, and then free the memory up with delete immediately when and after I need the memory location? I know 16 bytes of data isn't anything at all anymore but as a program gets larger this sort of thing could play a substantial role.
Sep 19, 2011 at 9:41pm
When a variable passes out of scope is the memory freed up in the same way that the dynamic memory would be with delete


This is true. Sorta. See info about the stack at the end of this post.

I have also read that when a dynamically allocated variable passes from scope its memory is released without having to use the keyword delete


This is false. One of the entire reasons you'd want to use dynamic memory is the ability to control it's lifetime. It does not automatically delete itself. You must do it or it stays allocated forever (or really until the program ends).

Other languages (C#, Java), have a garbage collector which gets rid of dynamically allocated memory that is no longer in use, but C and C++ do not.

would it not be more efficient to use dynamic memory, use a pointer, and then free the memory up with delete immediately when and after I need the memory location?


No that would not be more efficient because memory allocation is a more complex task than that.


How it works with locally allocated variables is there's a stack. Your program has a bunch of stack space allocated for it when it starts up. And whenever you enter a function, all the local vars that function uses go on the stack.

So as you get deeper and deeper in function calls, you use more and more of the stack. But as you exit those functions, you get that stack space back.

Note that all this memory is allocated up-front. So putting it on/off the stack doesn't really allocate new space, it just uses the space that's already there.

Therefore, as long as you don't exceed your stack space (which is actually hard to do unless you put really, really large objects/arrays on the stack) -- then putting things on the stack is harmless.
Last edited on Sep 19, 2011 at 9:41pm
Sep 19, 2011 at 9:48pm
closed account (zb0S216C)
ddwinters45 wrote:
When a variable passes out of scope is the memory freed up in the same way that the dynamic memory would be with delete (My inclination is that this is not the case)? (sic)

It's true that a variable allocates n bytes of memory, but automatic variables are pushed onto the programs stack. As a result the stack grows by n bytes. While your variable remains in scope, the variable stays within the scopes stack frame. Automatic variables are automatically destroyed when they leave the scope in which they are declared.

I would recommend that you read up on the differences of automatic variables and DMA.

Wazzak
Sep 20, 2011 at 1:26pm
Thank you everyone for the explanations. I knew there had to be a logical reason or we would all be using dynamic memory all the time. Thanks again.
Topic archived. No new replies allowed.