Trouble understanding C++ pointers

Started by
8 comments, last by DevFred 14 years, 4 months ago
OK so I've been trying to learn about pointers the past few days. Now, I get how they work (referencing the point in memory, etc etc). The question I have though, is why are they used? How do you know when you should use a pointer or not? It seems like you can still do the same thing without pointers, but I can't grasp why you would use them and when. It's really eating away at me lol.
Advertisement
Quote:OK so I've been trying to learn about pointers the past few days. Now, I get how they work (referencing the point in memory, etc etc). The question I have though, is why are they used? How do you know when you should use a pointer or not? It seems like you can still do the same thing without pointers, but I can't grasp why you would use them and when. It's really eating away at me lol.
Try searching the forum archives for phrases such as 'C++ pointers', 'why use pointers', etc. This question has been answered at least a few times, and I know that in some previous threads on the subject there were some very good summaries of why and when to use pointers in C++.

Meanwhile, here's an exercise to try. Write a C++ program that asks the user to specify an array size, then creates an array of that size, fills the array with consecutive integers starting and 0, and prints out the array. See if you can do it without using pointers :)

(Note that for the purposes of this exercise at least, we won't consider existing RAII containers such as std::vector.)
1) To pass an instance of an object around without copying it. Save copying the whole thing, then passing it to a function. References can be a better option here. Good example is also when you want to use the same texture for many models in a game - the models would have a pointer to the same texture, rather than copy it each time.

2) Iterating over an array (or arbitrary block of memory) of objects.

3) Messing around with memory at a low level (casting blocks of memory to objects, serialisation, etc)

There's just a few uses of them. Languages without pointers tend to use references - examples being C#, Java and Python.

I wouldn't worry about understanding where and when to use them - just get used to programming C++ and you'll learn when to use them. I have found myself when I learn something new, I'll either think "Wow, how did I live without this?" or "When the hell would I use this?" - you just figure it out in time.


Quote:Original post by jyk
Meanwhile, here's an exercise to try. Write a C++ program that asks the user to specify an array size, then creates an array of that size, fills the array with consecutive integers starting and 0, and prints out the array. See if you can do it without using pointers :)


I've seen people write some code without an important feature...and not in a very nice way either (don't know for-loops? just write every permutation of the code sort of solution). You could do that without pointers, and simply use a lot of code, a big ass switch statement ((1-whatever you specify as max) and some templating if you really don't want to copy the same function over and over. The result is far from a 'solution', but it works. Shame mind, whatever backwards way you done it there would still technically be a pointer there somewhere ( int myarray[123]; )

Adventures of a Pro & Hobby Games Programmer - http://neilo-gd.blogspot.com/Twitter - http://twitter.com/neilogd
Pointers are also essential for data structures and algorithms used in lists, queues, trees etc.

A node in a list for example needs to be able to reference its neighbor nodes
Quote:You could do that without pointers, and simply use a lot of code, a big ass switch statement ((1-whatever you specify as max) and some templating if you really don't want to copy the same function over and over. The result is far from a 'solution', but it works. Shame mind, whatever backwards way you done it there would still technically be a pointer there somewhere ( int myarray[123]; )
Ok, let's add the stipulation that the only upper limit that can be imposed on the array size is that of available memory - that should effectively eliminate the 'massive switch statement option' :) It should also eliminate the 'create a huge static array and just use as much of it as is needed' option, since that would require some a priori knowledge of the environment in which the program was to run.

Maybe there are other hacky ways you could do it, but I can't think of any at the moment.
Why would you ever link to a website instead of copying the site and putting it in a forum post, or emailing it to a friend?
Check out this post for a problem that can't be solved without pointers.
Quote:Original post by applejacks
It seems like you can still do the same thing without pointers, but I can't grasp why you would use them and when. It's really eating away at me lol.
This is like thinking that you don't need Dihydrogen monoxide. Little do you know that your world is absolutely full of it and it's vital to your survival.

Just program in C for a few months and pretty soon you'll realise how you really can't do without them.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
Quote:Original post by jyk
Quote:OK so I've been trying to learn about pointers the past few days. Now, I get how they work (referencing the point in memory, etc etc). The question I have though, is why are they used? How do you know when you should use a pointer or not? It seems like you can still do the same thing without pointers, but I can't grasp why you would use them and when. It's really eating away at me lol.
Try searching the forum archives for phrases such as 'C++ pointers', 'why use pointers', etc. This question has been answered at least a few times, and I know that in some previous threads on the subject there were some very good summaries of why and when to use pointers in C++.

Meanwhile, here's an exercise to try. Write a C++ program that asks the user to specify an array size, then creates an array of that size, fills the array with consecutive integers starting and 0, and prints out the array. See if you can do it without using pointers :)

(Note that for the purposes of this exercise at least, we won't consider existing RAII containers such as std::vector.)

yES search the forums this has been covered numerous times. To quicky summarize if you are still fuzzy on them as you seem to be you don't need them. Practice learning to write programs first and when you start to write games that use C++ and DirectX using pointers then ask this question again if you still having problems.
Remember millions of programmers, I'm thinking VB6, got by for many years without having to deal with pointers. Hence, one of the reasons they were probably looked down upon by other programmers. See #3 below.
If you read the new Stroustroup Swan book he doesn't even cover pointers until PartIII and even then only because of the following reasons:
#1you would be unable to implement a new container (should you need one, and that's not uncommon).
#2 You would be unable to read huge amounts of C and C++ code that directly uses memory.
#3 More philosophically, I am among the large group of computer professionals
who are of the opinion that if you lack a basic and practical understanding of
how a program maps onto a computer's memory and operations, you will have
problems getting a solid grasp of higher-level topics, such as data structures , algorithms,and operating systems
[size="2"]Don't talk about writing games, don't write design docs, don't spend your time on web boards. Sit in your house write 20 games when you complete them you will either want to do it the rest of your life or not * Andre Lamothe
Quote:Original post by Telastyn
Why would you ever link to a website

I like that analogy a lot. And the existence of dead links means that the internet isn't garbage collected :)

[Edited by - DevFred on December 6, 2009 4:28:42 AM]

This topic is closed to new replies.

Advertisement