Pointers

I just read a large section of a book on pointers and I have a question regarding them.

When should I use them and why would I use them? The seem sort of pointless.

I also have another smaller question.

What happens if you do not delete the objects you create from classes?

Thanks in advance and if you have any advice for a newbie I would love to hear it!
This thread (about half way down) contains many good reasons to use pointers:
http://www.cplusplus.com/forum/beginner/60951/
I could summarise that thread as pointers are needed to make big arrays (or indeed, huge objects), to interact with hardware, to make arrays of size that you don't know at compile time, polymorphism (this is a big one), and efficient algorithms (think linked-lists etc).

What happens if you do not delete the objects you create from classes?

If you made it using new, and you do not delete it, the object stays in memory forever (well, until the program ends). If your program does this a lot, eventually it will run out of memory and crash. This commonly happens with badly written software that is designed to stay running for very long periods of time.
Last edited on
and you do not delete it, the object stays in memory forever


FOREVER!

All jokes aside, pointers are nice because they are much lighter weight than copying entire objects. For example, if you have an image editing program that deals with high res images, copying that image could be a costly operation. Instead, just pass around a pointer to the image and all of a sudden the cost of passing this image to a different function just got down 100 fold (don't take this as an exact measurement).
really.. forever?

a restart should suffice.. :/
It was a joke lol. I'm sure on older systems it would have remained until a system reboot though.
The seem sort of pointless.

:D
Thanks blacksheep, I came here to...point that out.
Topic archived. No new replies allowed.