Sign in to follow this  
lukesmith123

Declaring class objects as a pointer or not

Recommended Posts

lukesmith123    153
I was reading that dynamically creating class instances can cause problems included fragmentation.

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,

Share this post


Link to post
Share on other sites
Wooh    1088
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.

Share this post


Link to post
Share on other sites
CryZe    773
[quote name='Wooh' timestamp='1312762339' post='4845970']
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.
[/quote]

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.

Share this post


Link to post
Share on other sites
Trienco    2555
[quote name='CryZe' timestamp='1312768837' post='4846010']
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.
[/quote]

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.

Share this post


Link to post
Share on other sites
SymLinked    1233
[quote name='CryZe' timestamp='1312768837' post='4846010']
[quote name='Wooh' timestamp='1312762339' post='4845970']
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.
[/quote]

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.
[/quote]

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.

Share this post


Link to post
Share on other sites
SimonForsman    7642
Don't use raw pointers unless you have a good reason to do so.
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

Share this post


Link to post
Share on other sites
Cornstalks    7030
[quote name='CryZe' timestamp='1312768837' post='4846010']
[quote name='Wooh' timestamp='1312762339' post='4845970']
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.
[/quote]

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.
[/quote]
Fixed that for you.

Share this post


Link to post
Share on other sites
CryZe    773
[quote name='SymLinked' timestamp='1312783746' post='4846070']
[quote name='CryZe' timestamp='1312768837' post='4846010']
[quote name='Wooh' timestamp='1312762339' post='4845970']
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.
[/quote]

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.
[/quote]

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.
[/quote]

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

Share this post


Link to post
Share on other sites
rip-off    10979
[quote]
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
[/quote]
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.

Share this post


Link to post
Share on other sites
lukesmith123    153
Thanks for all the info. For some reason I had got into the habit of only using pointers but I think I will stop from now on. I have in the past had problems with memory leaks which is a pain.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this