What is the difference between dynamic memory allocation,and stack allocation ?

C

chris

Hi all,

I need to know, what is the difference between dynamic memory
allocation, and stack allocation ?
1. If I have a class named DestinationAddress, when should I use
dynamic memory allocation to create object of that class ?
2. If it says "dynamic memory allocation", is it mean the
following code :
DestinationAddress* dest = new DestinationAddress(); // code 1

doesn't allocate address when compile time, but in run time ?

So, the following code :
DestinationAddress dest; // code 2

does allocate memory when compile time ?

3. In Java programming language, all object created on heap,
just like dynamic memory allocation on C++. So, why should
I use non-dynamic memory allocation just like on code 2 ?

Thanks in advance
 
P

peter koch

chris skrev:
Hi all,

I need to know, what is the difference between dynamic memory
allocation, and stack allocation ?
1. If I have a class named DestinationAddress, when should I use
dynamic memory allocation to create object of that class ?
Dynamic allocation allows you to create an object and return it. It
also allows you to not have any object at all. Finally, dynamic
allocation allows you to determine the type of the object at run-time.
If you need to use any of these features, you should use dynamic
allocation.
Stack allocation is faster and ensures automatic destruction when you
leave scope. You should normally prefer this if you can. An exception
could be if you use huge C-style arrays or otherwise have an object
that requires vast amounts of storage, but that would be an extreme
case in a normal system.
2. If it says "dynamic memory allocation", is it mean the
following code :
DestinationAddress* dest = new DestinationAddress(); // code 1

doesn't allocate address when compile time, but in run time ?
Correct.

So, the following code :
DestinationAddress dest; // code 2

does allocate memory when compile time ?
Well, not actually. But the "allocation" is made for all local object
at entry to the function and the prize for this allocation is so low
that you can ignore the cost (at least on all architectures I'm aware
of).
3. In Java programming language, all object created on heap,
just like dynamic memory allocation on C++. So, why should
I use non-dynamic memory allocation just like on code 2 ?

Because you care about perfomance and ease of programming. Java is an
entirely different language where a garbage collecter takes care of
releasing your memory. In C++ there is (by default) no garbage
collection so you're on your own. In return C++ gives you deterministic
destruction on all objects and this is in my opinion far more valuable
(it is also one reason why C++ does not need finally).
Thanks in advance

/Peter
 
D

deane_gavin

peter said:
chris skrev:

Because you care about perfomance and ease of programming. Java is an
entirely different language where a garbage collecter takes care of
releasing your memory. In C++ there is (by default) no garbage
collection so you're on your own. In return C++ gives you deterministic
destruction on all objects and this is in my opinion far more valuable
(it is also one reason why C++ does not need finally).

Just to add to this for the OP, on those occasions where you need
dynamic storage duration, you are indeed on your own. By which I mean
that _you_ are responsible for paring every new with a delete and every
new[] with a delete[] to avoid leaking memory.

Wrap ALL you dynamic allocations in smart pointers (e.g. std::auto_ptr
or something from the boost smart pointers library) or other class
constructors and you will find this responsibility much less onerous.

Gavin Deane
 
J

John Harrison

chris said:
Hi all,

I need to know, what is the difference between dynamic memory
allocation, and stack allocation ?

The main difference is that with dynamic allocation you get to choose
when the object is destroyed. Dynamically allocated objects are
destroyed only when you explicitly delete them.
1. If I have a class named DestinationAddress, when should I use
dynamic memory allocation to create object of that class ?

The main thing to consider is whether you want to control when the
object is destroyed.
2. If it says "dynamic memory allocation", is it mean the
following code :
DestinationAddress* dest = new DestinationAddress(); // code 1

doesn't allocate address when compile time, but in run time ?

Run time.
So, the following code :
DestinationAddress dest; // code 2

does allocate memory when compile time ?

Run time also.

As already said the main difference is not when the memory is allocated
but when the object is destroyed.
3. In Java programming language, all object created on heap,
just like dynamic memory allocation on C++. So, why should
I use non-dynamic memory allocation just like on code 2 ?

Because you don't have to remember to delete the object. Because you are
happy with the idea that the object will be destroyed when the variable
goes out of scope. Most of the time that is what you want and that is
what stack allocation gives you.
Thanks in advance

john
 
R

Ron Natalie

Ayoung said:
code 2 is faster than code 1.
That isn't necessarily true.

The big difference, is that you know the lifetime of the object will be
automatically ended wherever that variable in #2 exits. In the case
of a dynamic allocation you must make sure that you specifically delete
the object.
 
C

chris

peter said:
Dynamic allocation allows you to create an object and return it. It

Peter, what did you mean by "return it" above ?
also allows you to not have any object at all. Finally, dynamic

and what did you mean by "to not have any object at all" ? Did you
mean, I just create unitialized pointer, like this :

DestinationAddress *pDestAddress;

allocation allows you to determine the type of the object at run-time.
If you need to use any of these features, you should use dynamic
allocation.
Stack allocation is faster and ensures automatic destruction when you
leave scope. You should normally prefer this if you can. An exception

and what did you mean by "leave scope" ? When is stack allocation
destructed automatically ? If I pass an stack-allocated object by
reference to other class, how do I know when it will be destroyed ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top