Question:
Whats the point of pointers?
FireWire
2013-08-07 11:42:25 UTC
I'm currently reading a book on programing in C++. I'm currently in a section that talks about pointers. I know that they are variables that "point" at other variables. The way the author of the book uses them, they seem to be another way to assign a value to a variable. So what's the point of them? By the way I want to get a job in video game development. I do plan on attending college to learn this stuff, but I want to get a head start. So if anyone out there is in the video game development field, how often do you use pointers, and for what? Thanks
Four answers:
?
2013-08-07 14:21:54 UTC
Suppose you had defined two local integer variables in some subroutine and you wanted to pass them both to another function as parameters. But you wanted to allow the subroutine you are calling to change either or both of the values. In C (not in C++) there is no such thing as references, so you'd have to pass a pointer to the variable. That's the only way that another C function can change local variables that are owned by a different subroutine that is calling it. In C++, references were introduced to help clean up this kind of syntax and use and so the need for pointer variables was significantly reduced. But not eliminated.



Keep in mind that references are, in fact, just pointers under a different name. The compiler still passes pointers. The main difference is that the subroutine cannot modify the pointer that is passed, which helps the compiler know a little more and produce (sometimes) better code because of that detail. You also cannot pass NULL as a reference value, so the called subroutine doesn't have to include code to test for NULL... it can simply use the reference with safety.



It is a little more efficient (with compilers that don't support the ability to do strength reduction on array indices and construction of compiler generated pointer equivelents) to use a pointer to index through an array (and the C syntax for it may to some even look better than it does in C++.) And there may be times when you WANT to pass a NULL pointer. And there are also times when you actually NEED a pointer because you are computing the difference value between two pointers, for example, to get a count (which isn't a pointer.) You can't do that with references in C++.



Another area that crosses my mind is that in C++ references must be initialized at creation and cannot be changed later. If you are using a simple linked list of objects it is often necessary to modify pointers AFTER their creation. So here, even in C++, you will be forced to use pointers (of some kind.) C++ programmers often use "smart pointers" to help handle some constructor/destructor issues, though. But at their heart, even they have an actual pointer type and not a reference type.
2013-08-07 18:59:42 UTC
The biggest thing you can do with them is called polymorphism. I can't explain that fully because it is a very confusing concept that you will only learn after playing with the code a lot. Here is how it works:



class BaseClass {};

class SubClass : pulbic BaseClass {}

class OtherSubClass : public BaseClass {}



BaseClass * Pointer = new SubClass;

BaseClass * OtherPointer = new OtherSubClass;



This way you can hold different data types in the same array using the base class as it's type.



Another reason is to pass an array. Instead of copying the entire array through the parameter which takes up a huge amount of time and memory, you can just pass in the first element in the array as a pointer and then increment through the array with the pointer. This way you only pass in 1 element of the array.



Another reason is because some datatypes only allow you to store the variable or object as a pointer so you can't change it. When you change the pointer, it only effects the pointer no the data it is pointed to. This is true for a lot of the DirectX objects to create 3D games.



Another reason is that you want to create dynamic memory instead of just declaring every possible instance of the data type you need to store the variable. An example of this is creating dynamic arrays that you can change the size of during run time.



I use pointers very often and almost in every program. I use them because they are in most cases less memory intensive than actual data types and objects. You will use them very often in game programming when you have to make copies of 3D models/textures instead of making multiple instances of that 3D model/texture which would take up very valuable computer processes time and memory.
green meklar
2013-08-09 08:41:42 UTC
>I know that they are variables that "point" at other variables.



Not really. They point at memory locations. There's no necessity that any individual variable that you see in the source code correspond to that memory location.



>The way the author of the book uses them, they seem to be another way to assign a value to a variable.



That's misleading at best, and just plain wrong at worst.



An introduction to pointers might show some usage of that sort, but you should not get the idea that that's what they're there for.



>So what's the point of them?



They let you make data structures where one part of the data structure knows where another part is.



This might not sound important. It is EXTREMELY important.
Eamonn
2013-08-07 18:57:35 UTC
Video Game Development use mostly makers like GameMaker or Unity. They require very little code. So hope you're not wanting to get a job fully coding, because it probably wont happen. Though you DO need skills in certain languages, and you can use Java as well.



Do you know I asked the exact same question when I was starting out? Anyway, C and C++ are old languages. Back in the days of like 1980 when computers were just emerging, memory was valuable. Java eats up a lot of memory, and that's why it was said to be slow back in those days, as well as other reasons. C and C++ used pointers. They save memory. I don't want to get too into detail, so just know pointers are to save memory and they make your program run faster.



I'm 13, so I'm not in game development, but like you I want to get into game development. I have made a few programs in C and C++, and pointers are used a fair bit. You'd defiantly use them, so you might not see the point in them now, but believe me, when you get deep into C++, you'll realise why they are useful. When I was starting in C++ I didn't understand why they were useful. I thought "Yeah, I understand pointers well, but why the heck would I use them?? They seem 'point'-less.".



They aren't as important now that memory isn't in MB and is into GB, but you still want to use them to save memory and save on memory and resources.



Good luck!


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...