🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

C++ Workshop - Pointers (Ch. 8)

Started by
87 comments, last by mihaimoldovan 12 years, 3 months ago
Just wanted to say thanks to all the tutors and jwalsh for starting this workshop. Reading over the other questions and the tutor responses has helped me a lot to improve my understanding of C++.

Ok, I have two questions.

1. The book said do not delete a pointer twice. I assume they mean something like this.

int * pWhatever = new int;
delete pWhatever;
delete pWhatever;

What happens when you delete it the second time that is bad?

Also what happens if you do something like this:

int * pWhatever = new int;
delete pWhatever;
* pWhatever = new int;
delete pWhatever;

Are you able to do this?

2. I would like to know when you should create on objects on the free store and when you should create objects on the stack. Perhaps some real life examples might help me clarify this.

For example if you were making a game and have the class Sprite() when would it be better to:

Sprite Plane;

or

Sprite *pPlane = new Sprite;


Advertisement
<not official student or tutor> <intermediate to advanced>
question: new and delete allocates memory to the free store and malloc and free allocates memory to the heap. i'm wondering what is the difference in regards to memory/data representation between the two (aside from the fact that the former calls constructor and destructors)?
</intermediate to advanced> </not official student or tutor>

Beginner in Game Development?  Read here. And read here.

 

The difference is usually abstract, not literal. It's important to always release memory the same way you aquired it, because the standard explicitly states that not doing so is undefined behavior. Standard library implementators are not required to implement operator new in terms of malloc, but it's been my experience that they usually do. In general, the 'memory structure' of things in C++ are never defined explicitly by the standard.
Well I hate to bump but its been a week and I haven't gotten any answers from my questions at the top of this page. Should I be putting the post in a different thread?
Quote:
1. The book said do not delete a pointer twice. I assume they mean something like this.

int * pWhatever = new int;
delete pWhatever;
delete pWhatever;

What happens when you delete it the second time that is bad?

You've already returned this memory to the system. It can't tell that you're returning it twice, so you just totally trashed the data structure it uses to keep track of this stuff.

Quote:
Also what happens if you do something like this:

int * pWhatever = new int;
delete pWhatever;
* pWhatever = new int;
delete pWhatever;

Are you able to do this?

Besides the extraneous *, and it being bad style, it's perfectly legal.

Quote:

2. I would like to know when you should create on objects on the free store and when you should create objects on the stack. Perhaps some real life examples might help me clarify this.

For example if you were making a game and have the class Sprite() when would it be better to:

Sprite Plane;

or

Sprite *pPlane = new Sprite;

You stick things on the heap/free store when you need them to survive beyond the scope they are declared in. And/or because they are very large and would overflow the stack otherwise.
1. When you trash the data structure, what happens? Would there be a memory leak or maybe a runtime error?

2. Ok so if you can contain the object in a particular scope, then you should create them on the stack instead of the free store?

Also, how large is large?
Quote: 1. When you trash the data structure, what happens? Would there be a memory leak or maybe a runtime error?

If you're lucky. If not, it could work fine... until one day, some other system supposably totally unrelated goes boom.

Quote:
2. Ok so if you can contain the object in a particular scope, then you should create them on the stack instead of the free store?

Image you have a function called 'load_image'. This function creates a brand new image and reads data from disc into it. If it created that image on the stack, it would be destroyed as soon as the function returned. That's clearly no good. The image has to still be around after the function call; it has to survive the end of the function's scope - so you allocate it on the heap instead.

Quote:
Also, how large is large?

That would depend on the size of the stack, wouldn't it? There's no hard 'more than this is big' line. But, generally, if the class takes up more than a full page, it will probably be better on the heap...
Thanks a lot Deyja.

I think I got the idea now. Just one quick question. If there is any doubt should I just create the object on the free store?

Usually, I try to keep things on the stack as much as possible, because it makes things less complicated and your code easier to manage. Only use the free store if it is really necessary.
Mike Popoloski | Journal | SlimDX
ussnewjersey4 has it right. You will find, though, that any dynamic data structure is going to require you use the heap, because the stack is static. Also keep in mind that using a pointer does not neccessarily mean you are using the heap. A pointer can point to an object on the stack just the same as it points to the heap. (Of course, deleting an object that's on the stack has undefined behavior)

This topic is closed to new replies.

Advertisement