Why is this person using pointers?

Apr 14, 2013 at 10:05pm
I am just getting back into C++, but I have been programming for quite some time. I decided to jump into using DirectX 10 for the fun of it.

Anyway, I came across these tutorials:

http://www.rastertek.com/dx10tut02.html

What this guy does throughout all of his classes is use pointers where I don't think he needs them. Such as his use in WinMain:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
SystemClass* System;
bool result;
	
// Create the system object.
System = new SystemClass;
if(!System)
{
	return 0;
}

// Initialize and run the system object.
result = System->Initialize();
if(result)
{
	System->Run();
}

// Shutdown and release the system object.
System->Shutdown();
delete System;
System = 0;

return 0;


Why do you think he is making System a pointer, and would you normally do it this way as well? I don't see him copying or reusing the variable anywhere.

Thanks for the help!
Last edited on Apr 14, 2013 at 10:08pm
Apr 14, 2013 at 10:48pm
Without spending time going through the whole code there are few reasons I could immediately think of.

1. He specifically wants the instance to be stored on the heap instead of the stack. This could be to ensure he doesn't exhaust the available memory on the stack.

2. By using allocation and de-allocation he exactly controls when the constructor/destructor is called. The order of destruction and construction is often important when using third-party libraries or developing complex systems.

The snippet code you have provided only really leaves the possibility of #1, unless he is working up towards a more complex structure.
Apr 14, 2013 at 11:17pm
Thanks for the reply. I do have some follow up questions then.

In the code I posted above, would you make System a pointer as well, assuming that System is the container and controller for the rest of the program? How about using a smart pointer instead?
Apr 14, 2013 at 11:21pm
In this example because it's in the main method there is very little risk of forgetting to free the memory.

Personally I'd definitely use a smart pointer and reset it where he is doing the delete. Again this is to ensure order of destruction is maintained. (something like boost::scoped_ptr<> would be ideal)
Apr 14, 2013 at 11:29pm
Zaita's points are right on.... but unless SystemClass is huge, I don't see any reason why you'd use a pointer here. The likelihood of exhausting stack space with just a single object is very, very low.

This guy also does this:
1
2
3
4
5
System = new SystemClass;
if(!System)  // <- System will never be null... so this check is pointless
{
	return 0;
}


Which leads me to believe this guy's tutorials probably aren't the best.

That's not to say you shouldn't read the tutorial. I'm sure there's still merit to what he's doing. But he's definitely doing other things that are questionable.
Apr 14, 2013 at 11:32pm
Using a pointer still allows you to control the destruction time of the object. Again this example is so simple it's not likely an issue, but it still should be considered for future growth of the project :)

But yea @Disch is also spot on.
Apr 14, 2013 at 11:46pm
Sweet! Thanks guys!

I notice the guy does that for most of the pointers. Not sure why he does that, but his tutorials are easy to follow and help set up DirectX pretty well, though I have changed the organization considerably. I will see if I can find other tutorials to double check my work to make sure I am not picking up bad habits.
Topic archived. No new replies allowed.