Sign in to follow this  

What's the point in references and pointers?

This topic is 4106 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So I Understand everything about them but what their purpose is? Do they make things compile faster? Do they make things easier to read for coders? Do they make things that are out of scope come into scope? I have no clue what their purpose is.

Share this post


Link to post
Share on other sites
Evidently the main point of the article slipped past you. As far as the things you mentioned, the only one that really applys is the scope purpose. Having a reference to Something means you can tell something else about this particular Something. I guess you could say thats a way to pass the variables through the scope but you shouldn't look at it as such individual "purposes" but what the pointer actually is, like understanding what a variable is and how to use it.

I suggest re-reading or reading another tutorial.

Share this post


Link to post
Share on other sites
Consider this bit of code.


void WriteVectors(std::vector<int> v)
{
// Write the contents of the vector to the disk
//
// .....
}

void WriteVectorsRef(const std::vector<int>& v)
{
// Write the contents of the vector to the disk
//
// .....

}

void SomeFunction()
{
std::vector<int> vec;

for (int i=0; i<100000; i++)
{
vec.push_back(i);
}

// 1st version
WriteVectors(vec);

// 2nd version
WriteVectorsRef(vec);
}



In the first version vec is passed by value which means that a temporary vector is to be created and then 100000 items have to be copied into it. The function then works on the temporary vector

In the second version vec is passed by reference which means that the address of vec is passed to WriteVectorsRef and so the function uses the original vector rather than a temporary. The const is there to indicate that the function will not modify the vector's contents (if it does then you would get a compile time error)

Passing by reference is what you would do to increase performance at runtime.

Share this post


Link to post
Share on other sites
Also, dynamic memory allocation is only possible with pointers. This memory is allocated on the heap rather than the stack. At first I didn't understand the point of pointers either, but eventually ( hopefully ) you'll come to love them. They are especially useful when dealing with classes.. you won't use pointers very often with primitive types like int or float.

Share this post


Link to post
Share on other sites
I used to wonder the same things. Someone already mentioned a good use for refrences, here is one for pointers.

Say you have a Model object. Inside of it, you have 1 megabyte of data loaded from the hard disk.

Now, say that the model is a character. You want 5 different people walking around in your world, but they all use the same model. You can have a list of all your characters in the world, and instead of each of them holding on to 1 megabyte of Model data, they all have pointers to the same model data. Now, you only load that megabyte once but are able to reuse it multiple times.

Also, like refrences, passing around a pointer that is only 4 bytes is a lot faster than passing copys of 1 megabyte model objects :)

Share this post


Link to post
Share on other sites
I'm not sure exactly how much you understand of pointers, but I can admit that they don't seem to offer much when a description without example is given. You probably know that they store the address of another variable. The importance of that fact is in that a single value, the value of the regular variable, can be accessed from multiple places. A more vivid example:

Imagine you have two structures in your code. One is a graphics object containing all the bitmaps needed to display a sprite. Another object is the state info of a sprite, i.e. where a sprite is to be rendered on the screen and with what frame of animation. Now, let us further imagine that you want to have several of these little critters running around the screen all in dependant of each other. That means you will need to keep track of each of these sprite state using the latter object mentioned above. It might seem plausible that for each of these sprite state objects you would need one those sprite images objects to accompany it. Kind of a waste, eh? ;) After all, if each of these critters is sharing the same set of images, why load multiple copies into memory. Duh-du-duh duh! Pointers to the rescue! Each sprite state object can use a pointer to reference a single pre-loaded sprite images object.

There are other uses as well, but they seem to come more naturally when you understand this general principle. Of course this all introduces you to a whole new world of headaches involving resource management. Making sure pointers are valid, making sure they are released, ensuring nothing is still access the data when it is released, etc... But like I said, that's a whole 'nother story.

Share this post


Link to post
Share on other sites

This topic is 4106 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this