Jump to content

  • Log In with Google      Sign In   
  • Create Account


Declaring class objects as a pointer or not


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
10 replies to this topic

#1 lukesmith123   Members   -  Reputation: 153

Like
0Likes
Like

Posted 07 August 2011 - 05:56 PM

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,

Sponsor:

#2 Wooh   Members   -  Reputation: 608

Like
0Likes
Like

Posted 07 August 2011 - 06:12 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.

#3 CryZe   Members   -  Reputation: 768

Like
-2Likes
Like

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 Trienco   Crossbones+   -  Reputation: 2117

Like
0Likes
Like

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.
f@dzhttp://festini.device-zero.de

#5 Hodgman   Moderators   -  Reputation: 29358

Like
0Likes
Like

Posted 07 August 2011 - 10:45 PM

To declare class objects as a pointer or not?

It depends.


#6 SymLinked   Members   -  Reputation: 846

Like
0Likes
Like

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 SimonForsman   Crossbones+   -  Reputation: 6035

Like
0Likes
Like

Posted 08 August 2011 - 12:35 AM

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
I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!

#8 Cornstalks   Crossbones+   -  Reputation: 6974

Like
0Likes
Like

Posted 08 August 2011 - 01:03 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 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.

Fixed that for you.
[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

#9 CryZe   Members   -  Reputation: 768

Like
-1Likes
Like

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 rip-off   Moderators   -  Reputation: 8111

Like
2Likes
Like

Posted 08 August 2011 - 04:50 AM

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.

#11 lukesmith123   Members   -  Reputation: 153

Like
0Likes
Like

Posted 08 August 2011 - 06:43 AM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS