By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,034 Members | 1,777 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,034 IT Pros & Developers. It's quick & easy.

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

P: n/a

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

Oct 27 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
code 2 is faster than code 1.

Oct 27 '05 #2

P: n/a

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

Oct 27 '05 #3

P: n/a

peter koch wrote:
chris skrev:

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).


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

Oct 27 '05 #4

P: n/a
chris wrote:
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
Oct 27 '05 #5

P: n/a
Ayoung Chueng wrote:
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.
Oct 27 '05 #6

P: n/a

peter koch wrote:
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 ?

Oct 28 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.