Using pointers is more work, more code and increased risk for mistakes so I always avoid pointers unless there is a good reason to use pointers.

If you aren't using pointers the object itself is getting saved on the stack. So you should always use pointers. This way only the pointer variable itself is located on the stack and the object is located on the heap. That's it.

If you aren't using pointers the object itself is getting saved on the stack. So you should always use pointers. This way only the pointer variable itself is located on the stack and the object is located on the heap. That's it.

And how is that a good thing, unless your object is really huge (as in "size expressed in kb - mb")? "Allocating" stack just does something like stack_ptr -= sizeof(Object)

In comparison new will deal with the OS, take a ton more time for allocation, add the potential for bugs and memory leaks and unless the object is needed beyond that function serves no practical or performance oriented purpose I could see.

And in regard to the original question: either you need your object on the heap or not. If you do, then declaring a pointer is useless, because you _still_ need to create the actual object somewhere. Avoiding fragmentation requires a little more work and design than just adding a *. If you're talking about temporary objects in a function: the stack doesn't fragment.

Using pointers is more work, more code and increased risk for mistakes so I always avoid pointers unless there is a good reason to use pointers.

If you aren't using pointers the object itself is getting saved on the stack. So you should always use pointers. This way only the pointer variable itself is located on the stack and the object is located on the heap. That's it.

That's not true. If the variable is declared inside a heap-allocated class instance as a non-pointer, it will still be allocated on the heap. If you're inside function scope then sure, the variable will be allocated on the stack. Recommending someone to always use pointers is quite a blanket statement however.

Using pointers is more work, more code and increased risk for mistakes so I always avoid pointers unless there is a good reason to use pointers.

If you aren't using pointers the object itself is getting saved on the stack. So you should sometimes use pointers. This way only the pointer variable itself is located on the stack and the object is located on the heap. You should only do this if you really need to, as dealing with pointers is a pain in the butt, and seeing as you're a beginner, there probably isn't a lot of need to do that.

Using pointers is more work, more code and increased risk for mistakes so I always avoid pointers unless there is a good reason to use pointers.

If you aren't using pointers the object itself is getting saved on the stack. So you should always use pointers. This way only the pointer variable itself is located on the stack and the object is located on the heap. That's it.

That's not true. If the variable is declared inside a heap-allocated class instance as a non-pointer, it will still be allocated on the heap. If you're inside function scope then sure, the variable will be allocated on the stack. Recommending someone to always use pointers is quite a blanket statement however.

Next time I shouldn't try to write my posts in like 10 seconds. I already knew all that stuff, and yes you shouldn't always allocate all of your objects on the heap. Sorry for the misunderstanding... I thought, that he as a beginner should just allocate all of them on the heap, so that he doesn't get problems like for example stack overflows. I simply should've said that. >.<

I thought, that he as a beginner should just allocate all of them on the heap, so that he doesn't get problems like for example stack overflows. I simply should've said that

Even still, no.

Stack overflow is a rare problem, mostly caused by excessive recursion or placing large arrays on the stack (or objects that contain large arrays). It is also generally easy to fix.

Allocating data on the heap can lead to lots of common problems, including memory leaks and bad pointers. These problems can be difficult to track down and fix. It also is a bad idea to do dynamic allocation when it is unnecessary from a performance standpoint, because each allocation/deallocation involves traversing some data structure. Stack allocation is virtually free in comparison.