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

Discussion in 'C++' started by chris, Oct 27, 2005.

  1. chris

    chris Guest

    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
    chris, Oct 27, 2005
    #1
    1. Advertising

  2. code 2 is faster than code 1.
    Ayoung Chueng, Oct 27, 2005
    #2
    1. Advertising

  3. chris

    peter koch Guest

    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
    peter koch, Oct 27, 2005
    #3
  4. chris

    Guest

    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, 2005
    #4
  5. Re: What is the difference between dynamic memory allocation,andstack allocation ?

    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
    John Harrison, Oct 27, 2005
    #5
  6. chris

    Ron Natalie Guest

    Re: What is the difference between dynamic memory allocation,andstack allocation ?

    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.
    Ron Natalie, Oct 27, 2005
    #6
  7. chris

    chris Guest

    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 ?
    chris, Oct 28, 2005
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jakk
    Replies:
    4
    Views:
    12,118
  2. Ken
    Replies:
    24
    Views:
    3,857
    Ben Bacarisse
    Nov 30, 2006
  3. vivek

    Dynamic memory allocation stack heaps

    vivek, Nov 15, 2007, in forum: C Programming
    Replies:
    21
    Views:
    1,070
    James Kuyper
    Nov 17, 2007
  4. Bjarke Hammersholt Roune
    Replies:
    14
    Views:
    1,181
    Bjarke Hammersholt Roune
    Mar 6, 2011
  5. PerlFAQ Server
    Replies:
    0
    Views:
    354
    PerlFAQ Server
    Jan 6, 2011
Loading...

Share This Page