Declaring class objects as a pointer or not
#1 Members - Reputation: 153
Posted 07 August 2011 - 05:56 PM
I was jsut wondering if it was reccomended to people with less experience to declare instances like this:
MYCLASS myClass;
rather than:
MYCLASS* myClass;
?
thanks,
#3 Members - Reputation: 724
Posted 07 August 2011 - 08:00 PM
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.
#4 Members - Reputation: 1338
Posted 07 August 2011 - 10:42 PM
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.
#6 Members - Reputation: 479
Posted 08 August 2011 - 12:09 AM
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.
#7 Members - Reputation: 3830
Posted 08 August 2011 - 12:35 AM
Don't allocate on the heap unless you have a good reason to do so.
You can allocate on the heap without dealing with raw pointers
instead of doing:
MyClass *instance = new MyClass();
you can do
shared_ptr<MyClass> instance(new MyClass());
shared pointers can track the number of shared pointers referencing the same object and free the memory automatically once all shared pointers to the same object go out of scope
The voices in my head may not be real, but they have some good ideas!
#8 GDNet+ - Reputation: 5614
Posted 08 August 2011 - 01:03 AM
Fixed that for you.
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.
#9 Members - Reputation: 724
Posted 08 August 2011 - 03:22 AM
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. >.<
#10 Moderators - Reputation: 5310
Posted 08 August 2011 - 04:50 AM
Even still, no.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
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.






