 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
chris Guest
|
Posted: Thu Oct 27, 2005 8:53 am Post subject: What is the difference between dynamic memory allocation,and |
|
|
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
|
|
| Back to top |
|
 |
Ayoung Chueng Guest
|
Posted: Thu Oct 27, 2005 9:20 am Post subject: Re: What is the difference between dynamic memory allocation |
|
|
code 2 is faster than code 1.
|
|
| Back to top |
|
 |
peter koch Guest
|
Posted: Thu Oct 27, 2005 9:26 am Post subject: Re: What is the difference between dynamic memory allocation |
|
|
chris skrev:
| Quote: | 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.
| Quote: | 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.
| Quote: |
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).
| Quote: |
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).
/Peter
|
|
| Back to top |
|
 |
deane_gavin@hotmail.com Guest
|
Posted: Thu Oct 27, 2005 9:42 am Post subject: Re: What is the difference between dynamic memory allocation |
|
|
peter koch wrote:
| Quote: | 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
|
|
| Back to top |
|
 |
John Harrison Guest
|
Posted: Thu Oct 27, 2005 6:51 pm Post subject: Re: What is the difference between dynamic memory allocation |
|
|
chris wrote:
| Quote: | 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.
| Quote: | 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.
| Quote: | 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.
| Quote: |
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.
| Quote: |
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.
john
|
|
| Back to top |
|
 |
Ron Natalie Guest
|
Posted: Thu Oct 27, 2005 7:10 pm Post subject: Re: What is the difference between dynamic memory allocation |
|
|
Ayoung Chueng wrote:
| Quote: | 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.
|
|
| Back to top |
|
 |
chris Guest
|
Posted: Fri Oct 28, 2005 5:27 am Post subject: Re: What is the difference between dynamic memory allocation |
|
|
peter koch wrote:
| Quote: | Dynamic allocation allows you to create an object and return it. It
|
Peter, what did you mean by "return it" above ?
| Quote: | 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;
| Quote: | 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 ?
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|