What are some of the technical differences between memory that is allocated with the new operator and memory that is allocated via a simple variable declaration, such as int var? Does c++ have any form of automatic memory management?
In particular, I have a couple questions. First, since with dynamic memory you have to declare a pointer to store the address of the actual memory you work with, doesn’t dynamic memory use more memory? I don’t see why the pointer is necessary at all unless you’re declaring an array.
Secondly, if I were to make a simple function such as this:
int myfunc() { int x = 2; int y = 3; return x+y; }
…And call it, would the memory allocated by the function be freed as soon as it’s scope of existence has ended? What about with dynamic memory?
Note: This answer is way too long. I’ll pare it down sometime. Meanwhile, comment if you can think of useful edits.
To answer your questions, we first need to define two areas of memory called the stack and the heap.
The stack
Imagine the stack as a stack of boxes. Each box represents the execution of a function. At the beginning, when
mainis called, there is one box sitting on the floor. Any local variables you define are in that box.A simple example
In this case, you have one box on the floor with the variables
argc(an integer),argv(a pointer to a char array),a(an integer), andb(an integer).More than one box
Now, you have a box on the floor (for
main) withargc,argv,a, andb. On top of that box, you have another box (fordo_stuff) witha,b, andc.This example illustrates two interesting effects.
As you probably know,
aandbwere passed-by-value. That’s why there is a copy of those variables in the box fordo_stuff.Notice that you don’t have to
freeordeleteor anything for these variables. When your function returns, the box for that function is destroyed.Box overflow
Here, you have a box on the floor (for
main, as before). Then, you have a box (fordo_stuff) withaandb. Then, you have another box (fordo_stuffcalling itself), again withaandb. And then another. And soon, you have a stack overflow.Summary of the stack
Think of the stack as a stack of boxes. Each box represents a function executing, and that box contains the local variables defined in that function. When the function returns, that box is destroyed.
More technical stuff
The heap
This is where dynamic memory allocation comes into play.
Imagine the heap as an endless green meadow of memory. When you call
mallocornew, a block of memory is allocated in the heap. You are given a pointer to access this block of memory.Here, a new integer’s worth of memory is allocated on the heap. You get a pointer named
athat points to that memory.ais a local variable, and so it is inmain‘s “box”Rationale for dynamic memory allocation
Sure, using dynamically allocated memory seems to waste a few bytes here and there for pointers. However, there are things that you just can’t (easily) do without dynamic memory allocation.
Returning an array
What happens here? You “return an array” in
create_array. In actuality, you return a pointer, which just points to the part of thecreate_array“box” that contains the array. What happens whencreate_arrayreturns? Its box is destroyed, and you can expect your array to become corrupt at any moment.Instead, use dynamically allocated memory.
Because function returning does not modify the heap, your precious
intarrayescapes unscathed. Remember todelete[]it after you’re done though.