Pointers? What's the point?
Back when I took my C Programming class a couple years ago, I breezed by without any trouble what-so-ever. That is, except for pointers and addresses. As I took the class, I honestly didn't see any use for these in the near future (espescially not for game development) so I basically dismissed them after the semester ended.
Well now that I'm reading over this C++ book that I got, they happen to stress heavily on the importance of the pointer. Not only that, while I was browsing the book "Role Playing Game Programming with Direct X", they specifically mentioned pointers as something you need to have a solid grasp on.
Am I missing something? Why would you ever need to know the memory address of a variable? If you're going to substitute a variable, why not just replace the entire variable altogether instead of replacing the contents of where it's located in memory? I honestly don't understand the benefits of using pointers and addresses, and I still find it to be the hardest code to understand.
Does anyone have any insight as to why they're so crucial or maybe can possibly offer a very easy to learn article about them?
Thanks.
-Kronikle
Pointers give you full control of memory. Without that you can't utilize the use of memory to its full power.
Its analogus to saying: why would a car engine engineer need to know anything about the chemical and physical reactions that take place in an engine cycle.
Pointers do make code more complicated but good use of them gives u more functionality , speed and better memory use.
Try programming any project over 10 000 lines of code and you'll see.
Its analogus to saying: why would a car engine engineer need to know anything about the chemical and physical reactions that take place in an engine cycle.
Pointers do make code more complicated but good use of them gives u more functionality , speed and better memory use.
Try programming any project over 10 000 lines of code and you'll see.
Quote:Original post by Kronikle66
I honestly didn't see any use for these in the near future (espescially not for game development) so I basically dismissed them after the semester ended.
I'm not sure how you could conclude that games in particular wouldn't need pointers. Is there a solid reason why you assumed that?
If you had a situation where you knew exactly how much memory you needed for your entire game then you could probably just about get away without using pointers but it would be messy and would be far more trouble than it's worth.
Usually in a game you will be varying things at runtime. The amount of polgygons loaded from a map, the amount of enemies in a level, the amount of players in a network game, the amount of live bullets in the world, etc.
These things will have memory associated with them. The way of refering to all the bullets isn't to have a load of different variables called bullet1, bullet2, bullet3, etc, but to have some container of bullets which you can loop through. You refer to an individual by pointer (or reference, which is similar but different too).
This makes it possible to add and remove things as and when you need to. And it means you don't have to make a copy either. Assigning a memory address to a pointer is quick as it's (likely to be) only 4 bytes, whereas an Enemy or a Bullet may have a lot of memory associated with it, position, direction, strength, badness, etc. Copying that will take longer. So there are reasons of efficiency too. However the main thing, in my book is simplicity.
Having said all that though, as you use the more advanced and useful features of c++ you may come to use pointers less. The standard library has containers which will look after all memory allocation/deallocation for you. As long as you've correctly constructed your classes you can do this kind of thing, dynamically:
#include <list>#include <string>class MyClass { std::string m_name;public: MyClass(const std::string& name) : m_name(name) { }};int main() { std::list<MyClass> container; container.push_back(MyClass("petewood")); container.push_back(MyClass("Kronikle66")); return 0;}
Pointers are necessary for dynamic memory allocation. If you know your program is going to need a block of memory, but you don't know how much you'll need until run-time, you have no choice but to dynamically allocate it:
some_type * mem = new some_type[size];
Pointers are also necessary for writing interfaces. If you're writing the renderer for your game and want to be able to pass it different objects to render, you would not want to be stuck writing a different render_object() function for each object type:
render_object(const Bullet & b);
render_object(const PlayerCharacter & pc);
...
Rather, you would want to write a single render function that accepts a pointer to and object, and then pass each object's memory address as the parameter:
render_object(Object * obj);
Object * o = new Bullet;
render_object(o);
Object * pc = new PlayerCharacter;
render_object(pc);
some_type * mem = new some_type[size];
Pointers are also necessary for writing interfaces. If you're writing the renderer for your game and want to be able to pass it different objects to render, you would not want to be stuck writing a different render_object() function for each object type:
render_object(const Bullet & b);
render_object(const PlayerCharacter & pc);
...
Rather, you would want to write a single render function that accepts a pointer to and object, and then pass each object's memory address as the parameter:
render_object(Object * obj);
Object * o = new Bullet;
render_object(o);
Object * pc = new PlayerCharacter;
render_object(pc);
Say you have a rather large structure, and you need to pass that structure to a function. What do you think is better? To create another copy of that structure on the stack, or to pass a pointer to the structure (pointers are always the same size, and rather small).
As far as understanding them, most books I've read give piss poor examples of how, why, and when to use them. For example, doing something like:
is just about pointless. Unfortunately, this is how most books/tutorials teach it. I guess the reasoning is to make pointers a bit easier to grasp. A better example, imo, is showing how passing the address of a variable to a function allows the function to manipulate the actual variable and not just a local copy.
You probably already understand how this works, however.
Another example:
Arrays in c/c++ are great, but they're static. A static array is only useful if you know _exactly_ how much data you need to store. To create a dynamic array, however, requires the use of pointers.
Here's an example of a dynamic 2-dimensional array wrapped in a templated class:
You see how I'm using pointers to store the beginning address of the memory allocated by new? Also, notice the Get method. It returns the "address of", which means you can do something like:
Array2D<int> test_array(10,10);
test_array.Get(5,5) = 10;
cout << test_array.Get(5,5) << endl; // prints out 10
There are many uses for pointers. If you're going to be using c or c++, understanding pointers is an absolute must.
As far as understanding them, most books I've read give piss poor examples of how, why, and when to use them. For example, doing something like:
int x = 5; // initialize x to store 5int* y = &x // initialize a int pointer (int *) to store the address of x (&x)*y = 4; // set the value pointed to by y (*y) to 4, making x = 4
is just about pointless. Unfortunately, this is how most books/tutorials teach it. I guess the reasoning is to make pointers a bit easier to grasp. A better example, imo, is showing how passing the address of a variable to a function allows the function to manipulate the actual variable and not just a local copy.
You probably already understand how this works, however.
Another example:
Arrays in c/c++ are great, but they're static. A static array is only useful if you know _exactly_ how much data you need to store. To create a dynamic array, however, requires the use of pointers.
Here's an example of a dynamic 2-dimensional array wrapped in a templated class:
template <class Datatype>class Array2D{ public: Array2D(int p_width, int p_height) { m_array = new Datatype[p_width * p_height]; m_width = p_width; m_height = p_height; } ~Array2D() { if(m_array != 0) delete[] m_array; m_array = 0; } Datatype& Get(int p_x, int p_y) { return m_array[p_y * m_width + p_x]; } bool Resize(int p_width, int p_height) { Datatype* newarray = new Datatype[p_width * p_height]; if(newarray == 0) return false; int x, y, t1,t2; int minx = (p_width < m_width) ? p_width : m_width; int miny = (p_height < m_height) ? p_height : m_height; for(y = 0; y < miny; y++){ t1 = y*p_width; t2 = y*m_width; for(x = 0; x < minx; x++){ newarray[t1+x] = m_array[t2 + x]; } } if(m_array != 0) delete m_array; m_array = newarray; m_width = p_width; m_height = p_height; return true; } int Width() { return m_width; } int Height(){ return m_height;} int Size() { return m_width * m_height; } private: Datatype* m_array; int m_width; int m_height;};
You see how I'm using pointers to store the beginning address of the memory allocated by new? Also, notice the Get method. It returns the "address of", which means you can do something like:
Array2D<int> test_array(10,10);
test_array.Get(5,5) = 10;
cout << test_array.Get(5,5) << endl; // prints out 10
There are many uses for pointers. If you're going to be using c or c++, understanding pointers is an absolute must.
Try creating 32mb of usable memory without using pointers. Better still, trying returning this block of memory from a function.
Other languages have other methods of dealing with these things. Garbage collection. Implicit deterministic sorts of things (I count c++'s stl as an incomplete attempt at this). C is too simple for these. It leaves it up to the programmer.
Other languages have other methods of dealing with these things. Garbage collection. Implicit deterministic sorts of things (I count c++'s stl as an incomplete attempt at this). C is too simple for these. It leaves it up to the programmer.
Pointers are almost not necessary in C++. They may be useful in some cases, but I'm still wondering if iterators, auto_ptrs or shared_ptrs and references could not be used to almost entirely replace pointers in a game. The only places where pointers would need to be used is when interfacing with pointer-using APIs, or when allocating memory, but this is in my opinion marginal.
Still, they are a necessary and very important part of C, and they are not dead yet in C++, so if you plan to actually use these languages seriously, I suggest you learn these basic parts of the language in order to have more options.
Still, they are a necessary and very important part of C, and they are not dead yet in C++, so if you plan to actually use these languages seriously, I suggest you learn these basic parts of the language in order to have more options.
Quote:Original post by C-JunkieReal World example: image editing program. Should the [usually very large (8MB+)] pixel buffer be on the stack or on the heap? Hint: heap.
Try creating 32mb of usable memory without using pointers. Better still, trying returning this block of memory from a function.
/overwhelmed
I have to thank you guys for the incredibly replies. But, as usual, leave it to this board to make me look like a complete newbie lol. I understood about 60% of what you guys said.
I guess I better learn more about them before I try to find a use for them. ontheheap's example of how the tutorials teach them is basically how I learned. Well, time to learn a little more about them. Then I'll come back and review what you guys said.
-Kronikle
I have to thank you guys for the incredibly replies. But, as usual, leave it to this board to make me look like a complete newbie lol. I understood about 60% of what you guys said.
I guess I better learn more about them before I try to find a use for them. ontheheap's example of how the tutorials teach them is basically how I learned. Well, time to learn a little more about them. Then I'll come back and review what you guys said.
-Kronikle
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement