Page 1 of 1

Pointers...What is the point?

Posted: Sun Sep 06, 2009 6:00 pm
by Big Grizzle
Firstly, sorry for the really bad subject title.

Anyway what I would like to know is what are pointers used for in C/C++. I just started learning C++ and I understand that they point to a memory address, but having only programmed in higher level languages I have no idea what they are primarily used for? What uses do they hold?

Now I am fairly sure some of the C++ whizzes on here are at this point thinking FACE PALM, but bear with me. I just need a simple example to ignite my imagination a little bit.

Re: Pointers...What is the point?

Posted: Sun Sep 06, 2009 7:00 pm
by XianForce
Well firstly you can allocate memory dynamically with pointers, meaning instead of it all being on the stack in which it's all deleted when it goes out of scope, you can choose when it is allocated, and deallocated.

so with C++ memory allocation it'd be like:

Code: Select all

someClass* myObjectOfSomeClass;

//Then we allocate the memory...

myObjectOfSomeClass = new someClass();

//then delete it when needed

delete myObjectOfSomeClass;
myObjectOfSomeClass = NULL;

 /*I always set my pointers to NULL after I delete the memory, because the pointer is still pointing to that portion of memory, but what was there isn't there anymore, so you can get some funky results if you attempt to try any operations with it.*/
A small note, just in case it isn't apparent to you; In this example, the pointer itself is on the stack, so when it goes out of scope, the POINTER is deallocated, not the memory it is pointing to. So if you only have one pointer pointing to something, and you don't deallocate the memory, when the pointer goes out of scope, you have just earned yourself a memory leak, meaning that memory was allocated, yet never deallocated.

Re: Pointers...What is the point?

Posted: Sun Sep 06, 2009 8:43 pm
by Spikey
XianForce is correct. But there's also more! Since most of us programming games here, speed is an important issue. Passing pointers to a function is faster than passing objects, just imagine for a second: Your game character takes up 10000 bytes of memory while a (32 bit) pointer takes up 4 bytes. Obviously passing 4 bytes is going to be faster than passing 10000 bytes for each call.

Pointers still have more uses... Such as being used for linked lists or you can also do some funky stuff like pointers pointing to pointers ;)

Re: Pointers...What is the point?

Posted: Sun Sep 06, 2009 9:11 pm
by JaxDragon
What really made pointers useful for me, is the fact that with them you can modify the variable you are pointing to. For example, suppose you have a addition function that takes two numbers, adds them, and puts the result in a variable not defined inside the function. If you pass a normal variable to the function(as the result), you will have problems. Whereas if you use pointers, it should work flawlessly.

Re: Pointers...What is the point?

Posted: Mon Sep 07, 2009 1:12 am
by XianForce
Spikey wrote:But there's also more!
Yep, I think it's one of those things, where it's uses are only as big as your imagination type of things. There's a bunch of witty little ways you can use them to improve some aspect of something.

Re: Pointers...What is the point?

Posted: Mon Sep 07, 2009 5:04 am
by programmerinprogress
Since everyone has already mentioned all of the usual uses for pointers (dynamic allocation, passing by reference instead of value) my justification is that they help implement a vital component of polymorphism.

When you want to define a generic interface for your classes, you will declare methods(member functions) in a class, but you will implement them in derived classes, in C++, derived classes have close relationships with their base types, and you can actually use a base pointer to hold the address of a derived type (but you can only use the functions which were declared in the base class), however, you are able to call the specific version of the method in the derived class, because polymorphism allows you to override the base class method and replace it with the derived class method.

This is useful if you needed to perform an operation on different classes, with the same function name, but each object has a different definition for that function, rather than calling each individual method, using wither local objects or by loads of different pointers, you can simply use a collection of base class pointers instead.

so for example, if I was implementing my own menu system, and it had lots of different controls, such as a textbox, some buttons, a label etc, in my solution i'm going to push them into a container within the menu object, the menu object needs to be able to handle the drawing and the updating of each control, without polymorphism and pointers, the menu would have to know exactly what controls need to be drawn and updated, but because of polymorphism and base class pointers, all you have to do is push base class pointers which contain addresses to the specific control into the container and let the menu class perform Draw() and Update() methods, for each control, a unique version of Draw() and Update() are called, thus making pointers incredibly useful ;)

And i'm sure one of the pros on this forum will explain the usefulness of pointers to functions, although I must admit this is a grey area in my knowledge, and the guys on here have a lot more fun explaining them than me :lol:

Re: Pointers...What is the point?

Posted: Mon Sep 07, 2009 8:19 am
by Big Grizzle
Thanks for all of the helpful responses.

At this point my knowledge of C++ and memory management is lacking somewhat. I am too used to Python and it's built-in memory management. Hopefully through the preceding months my understanding will gradually increase. One eureka moment at a time. :)

Re: Pointers...What is the point?

Posted: Mon Sep 07, 2009 9:38 am
by programmerinprogress
Indeed, that's what happened to me when I learnt pointers, I went from "pointers? yeah, I get what they are, but why would I use them" to "ooh, I could use a pointer for that, brilliant" :lol:

Good luck to you!

Re: Pointers...What is the point?

Posted: Mon Sep 07, 2009 10:59 am
by Pickzell
Meh, you'll catch on, I didn't get pointers when I first started either. :p

Re: Pointers...What is the point?

Posted: Mon Sep 07, 2009 12:57 pm
by gordon
Yeah what everyone said above. I'll just chip in and agree that it does take a bit of time to understand pointers and their uses but eventually it will click.

Re: Pointers...What is the point?

Posted: Mon Sep 07, 2009 2:01 pm
by Sliver
Yeah, it took me a while to "get" pointers. The memory management stuff is a big pain in the arse.

A big reason is virtual functions:

Lets say that you're coding an RPG. In the RPG you have a character class, and inside it the virtual function Attack:

Code: Select all

class Character
{
public:
//usual stuff like constructors etc here
virtual void Attack(Character target)
{
//choose an attack automatically
}
};
This is good, but what if you wanted the player to select an attack, and only the player, not NPCs? The answer is to use polymorphism, and therefore pointers. You can extend the attack function like this:

Code: Select all

class Player
{
void Attack(Character target)
{
//bring up a menu of attacks
}
}
Ok, so now lets try it out:

Code: Select all

Player Sonic;
Character Mario;
Sonic.Attack(Mario);
The above code would NOT bring up the menu, because the Sonic Character is not a pointer. It would call the base Attack function defined in Character.

You'd have to do this:

Code: Select all

Player* Sonic = new Player();
Character* Mario = new Character();
Sonic->Attack(Mario);

//cleanup, further down somewhere in your code when the objects aren't needed
delete [] Sonic;
delete [] Mario;
And another reason - you will always get a handle to the same object with pointers. Every time you pass a class that isn't a pointer to a function, you'll just create a new object. For example:

Code: Select all

void SetObjectPos(Object obj, int x, int y)
{
obj.x = x;
obj.y = y;
}
That would not change the x and y values of the object that you pass to the function, it would just create a new object and change that. So you have to pass it the address of the variable, AKA a pointer.

These are just the reasons why I use them. There's loads more...

Re: Pointers...What is the point?

Posted: Mon Sep 07, 2009 3:14 pm
by Spikey

Code: Select all

Player* Sonic = new Player();
Character* Mario = new Character();
Sonic->Attack(Mario);

//cleanup, further down somewhere in your code when the objects aren't needed
delete [] Sonic;
delete [] Mario;
There's an exploit here. Drop the []'s, delete [] is trying to delete an array of objects while you did not allocate an array. Resulting behaviors may be unnoticeable but should be avoided. http://taossa.com/index.php/2007/01/03/ ... lete-in-c/

Re: Pointers...What is the point?

Posted: Mon Sep 07, 2009 3:43 pm
by Sliver
Spikey wrote:

Code: Select all

Player* Sonic = new Player();
Character* Mario = new Character();
Sonic->Attack(Mario);

//cleanup, further down somewhere in your code when the objects aren't needed
delete [] Sonic;
delete [] Mario;
There's an exploit here. Drop the []'s, delete [] is trying to delete an array while you did not allocate one. Resulting behaviors may be unnoticeable but should be avoided. http://taossa.com/index.php/2007/01/03/ ... lete-in-c/
Oops ^^

I agree with you 100%, thanks for correcting me. I was working with some char* arrays just before answering that post, I guess I've been working on them for too long ;)

Re: Pointers...What is the point?

Posted: Mon Sep 21, 2009 2:48 pm
by captjack
Spikey wrote:... funky stuff like pointers pointing to pointers ;)
Oh, you tricksy devil! :)

Code: Select all

int (*(*kill_me_now)(char *,double))[9][20]
A pointer to a function that takes (char *, double) as parameters, returning a pointer to an array of 9 of an array of 20 of int.

Code: Select all

int *(*crappage())();
crappage is a function that returns a pointer to a function which returns a pointer to an int.

<curls in corner, sucking thumb> No... make it stop!

-capt jack

Re: Pointers...What is the point?

Posted: Thu Sep 24, 2009 10:47 am
by MarauderIIC
On the topic of pointers that are (initially) confusing,

mytype* const mypointer
const mytype* mypointer
const mytype* const mypointer

Hooray! Const correctness!