472,779 Members | 2,541 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,779 software developers and data experts.

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


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
6 8097
code 2 is faster than code 1.

Oct 27 '05 #2

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

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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
by: wwj | last post by:
Hi ,all I want to know the difference between char a and char *p=new char and the difference between the heap and the stack ,and if the char a is corresponding to the stack in MEMORY,and char...
5
by: bull | last post by:
hi could any one explain with example the following in a better way to understand 1. what is stack in memory, how the programs are stored in stack , what is the use of stack 2. What is heap in...
2
by: Ma Xiaoming | last post by:
Dear ladies and gentlemen, I don't understand what the difference between the Heap and the Stack is. Could you please explain the difference between the both for me? Thank you very much. ...
28
by: Dennis | last post by:
I have a function which is called from a loop many times. In that function, I use three variables as counters and for other purposes. I can either use DIM for declaring the variables or Static. ...
61
by: academic | last post by:
When I declare a reference variable I initialize it to Nothing. Now I'm wondering if that best for String variables - is "" better? With Nothing I assume no memory is set aside nor GC'ed But...
7
by: Michael | last post by:
Hi, What's the benefit to dynamically allocate memory? using namespace std; int main() { char* ptr; ptr="abc";
11
by: toton | last post by:
Hi, I have little confusion about static memory allocation & dynamic allocation for a cluss member. I have class like class Bar{ public: explicit Bar(){ cout<<"bar default"<<endl; }
12
by: contactmayankjain | last post by:
Hi, Its said that one should avoid dynamic allocation of memory, as it de- fragment the memory into small chunks and allocation of memory is a costly process in terms of efficiency. So what is...
14
by: vivek | last post by:
i have some doubts on dynamic memory allocation and stacks and heaps where is the dynamic memory allocation used? in function calls there are some counters like "i" in the below function. Is...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.