Thread: Difference between straight and dynamic allocation?

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    57

    Difference between straight and dynamic allocation?

    I am curious about the difference between straight allocation:

    class_name object_name(arguements);

    and dynamic allocation:

    class_name *pointer_name = new class_name(arguements);

    Is it because straight allocation gives you a 'this' pointer and dynamic gives you a regular pointer? Or is that explanation too simple?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The "straight" allocation, as you call it, means that you are passing the job of dealing with the variable off to the compiler. The compiler allocates it itself when you declare it, and the compiler makes it go away by itself when it goes out of scope.

    Something that is dynamically allocated is your responsibility -- you have to decide when you're done with it and delete it yourself.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The difference is mainly in the lifetime of the object. A local object (inside a function) will only live for the duration of that function. A dynamically allocated object will exist until you free it (with delete). And of course, it is possible (in most environments) to have global objects, which exist from "before main" until "after main".

    There is a "this" pointer for ALL objects. It points to the object itself, no matter when and how it was created.

    For many things, you do not need dynamic allocation, but there are certainly cases where it is needed.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The general rule is to use "straight allocation" or local variables and declare them in the smallest scope in which they need to be used.

    Then you use dynamic allocation only in certain other circumstances. The most common of those circumstances is when you want to create an object in a specific scope but you want it to live longer than that scope.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Another common use is to avoid unnecessary copying of data (if you have two structs pointing to the same data, it's more efficient to keep a pointer to it).
    Of course, in C++, it's also relative easy and safe to do that with the help of smart pointers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    "Straight" allocation, as you call it, uses memory from the stack. Dynamic memory allocation uses memory from the heap.

    The stack is more convenient, being automatically freed and so on. [*Elysia points at auto pointers*] The heap is usually much larger, however, so you might also want to use dynamic memory allocation for very large objects.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by dwks View Post
    [*Elysia points at auto pointers*]
    No, actually I was hinting at a shared pointer, eg std::tr1::shared_ptr or boost::shared_ptr.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Another common use is to avoid unnecessary copying of data (if you have two structs pointing to the same data, it's more efficient to keep a pointer to it).

    That doesn't seem to be a use for dynamic allocation over statically allocated objects. You can have pointers to local objects as well.

    Using dynamic allocation to avoid copying is basically just a specific case of using it to change the lifetime of an object.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I suppose your theory is more plausible. It doesn't need to be dynamically allocated to use pointers, after all (which you do point out).
    But another very common use is (which I remember now) to allocate a lot of data. The stack is limited to about 1 MB. The heap is limited to the computer RAM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    That is another case, yes. I don't consider it quite as common because in most cases you will be using another class that does that for you (e.g. vector).

  11. #11
    Registered User
    Join Date
    May 2006
    Posts
    57
    Thank you for very clear explanations!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 05-06-2002, 11:40 AM
  2. Static Binding & Dynamic Binding :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 12-31-2001, 08:51 PM