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

06-04-2008

tabstop

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.

06-04-2008

matsp

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

06-04-2008

Daved

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.

06-04-2008

Elysia

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.

06-04-2008

dwks

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

06-04-2008

Elysia

Quote:

Originally Posted by dwks

[*Elysia points at auto pointers*]

No, actually I was hinting at a shared pointer, eg std::tr1::shared_ptr or boost::shared_ptr.

06-04-2008

Daved

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

06-04-2008

Elysia

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.

06-04-2008

Daved

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