Archived

This topic is now archived and is closed to further replies.

Billy Lee

Pointers: what's the point?

Recommended Posts

I''ve begun programming a 3D game like Breakout but so far, I haven''t used any pointers. Now I''m wondering what the point (excuse the pun ) of pointers is. All I do is use variables like you do in Java and change their values when necessary. What would you use pointers for in a game like Breakout?

Share this post


Link to post
Share on other sites
A question like this *really* belongs in the beginners forum.

Okay, what do we need pointers for...
Well, to access dynamicly allocated variables for example.
Consider creating an array of a size given by the user or loaded from a file etc.
You cant use a normal array for this because you dont know
at compile time how big it will be.
You are forced to use
int* a = new int[users_choice];

Also consider the case where you need to pass a variable to a function, and letting it change it. If you pass the variable by value, and not by reference or pointer, you cannot change the variable in the function, since you are working on a copy that is local the function.

In java, all variables except the integral types(int, double etc) are passed by reference(essentially pointers with automatic dereferencing).
So in Java when you create an object:
Object a = new Object(); you are actually creating a reference to the object, and when you pass ''a'' to a function you are only passing the reference, not the object itself.

Share this post


Link to post
Share on other sites
Well for example you can sometimes change a variable with it easier without making them global

How do you like this swap function:


  
swap(&a, &b);

void swap(int *a, int *b)
{
int c;
c = *a; *a = *b; *b = c;
}


Look ma, no return

Share this post


Link to post
Share on other sites
A pointer contains the the address of a variable (or function, or whatever), whereas a reference is an alias.

Pointers are a lot more flexible than references for this reason. For example, you can allocate memory for a pointer or you can set it to point to an arbitrary address. A reference, however, can only ever refer to one thing, and that thing must be specified when you initialise the reference.

This makes references safer than pointers, so it's best to use references over pointers when you have the choice.

____________________________________________________________
www.elf-stone.com

[edited by - benjamin bunny on August 12, 2002 11:29:56 AM]

Share this post


Link to post
Share on other sites
Pointers have nothing to do with whether a variable is global or not. Global simply means that the variable is declared outside of a function or class. A pointer is a type of variable that stores the address of another variable.

Another good aspect of pointers is that they let you pass large structures/classes/variables around quickly. Imagine that you have a large data structure that stores a bitmap, a game map, or something similarly large (multiple megabytes). Instead of passing the entire block of memory back and forth between functions, you can simply pass a pointer (only 4 bytes on a 32-bit machine).

Why not simply declare the massive structure global? Well, technically, that would be fine. But once you start writing larger programs, you will find that having too many globals will really start to clutter up your code and make it more difficult to read/write/maintain/extend. Basically, though, its purely up to you if you want to use lots of globals or not.

-Mike

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Yes it''s possible to use global variables for just about everything. However this is often not a great idea. Why? Well there are a number of reasons, the way memory is allocated, the possability that you could modify a global variable with out realizing it and cause a bug in your game, and the difficulty that global variables add should you want to copy and paste some of your code into another project. Not only do you copy and paste the functions, you need to go find all the global variables you created for use with that function. Needless to say it gets messy.

Now onto the pointers question. Pointers have a number of uses in programming. You can make a "Function pointer" that can be pointed at any function you wish. This allows you to call multiple functions based on user input perhaps such as which video card they might have.

Another use is dynamic memory allocation. With global variables as you have right now, every variable grabs some memory at the start of the application, and is not freed until the program ends. With pointers you can allocate memory dynamically. What this means is that you can allocate/and deallocate memory on the fly. So that your program isn''t taking up all the memory all of the time. So lets say you have 4 characters, each character takes up 1 byte. Now lets say these are global characters. This means 4 bytes are allocated at the start of your application and aren''t freed until you hit exit. Now lets say you only use 1 character at a time, but you want 4 different variable names. You can create 4 character pointers, and let your program start. When your ready to use one of the characters you allocate 1 byte of memory for it. When your done using it you delete it. Now you can create another character using the same memory the first one used.

There are a number of other uses but the top two are the most popular uses for pointers I can think of.

Share this post


Link to post
Share on other sites
Never ever think that a pointer always comes with a variable.
int *p, i;

i=0;
p=&i;
*p=10;

What you read in most pointer tutorials is just to tell you how it works, but people rarely use it that way. In some cases, maybe they do, but in most cases, they don''t.

Ziphnor told you this:
dynamic allocation memory.

It means allocating a block of data in the memory during the runtime. This block of data is not referenced by a variable. Thus, you need a pointer to point this block of data, and use it appropriately (otherwise, you get some weird runtime error).


My compiler generates one error message: "does not compile."

Share this post


Link to post
Share on other sites
Yeah, I should have addressed globals too.

In short: if you use globals, your head will fall off.

A longer (and slightly more accurate) answer is:
If you use globals, your code will become very difficult to understand very quickly. If you can avoid using globals then it''s transparent to the user (and you, when you come back to it 6 months later) which variables affect a function, because they''re all listed in the function''s parameter list. If you''re using global variables, without checking the function''s entire source, you have no way of knowing which variables affect it, which makes your code much harder to understand and debug.

____________________________________________________________
www.elf-stone.com

Share this post


Link to post
Share on other sites
I understand better now. Maybe someone could help me get started using pointers. As I said, I''m writing a game like Breakout. There is a difference though in that there is an arrow stuck on the bat, which initially points upwards. When you move the bat left or right, the angle of the arrow (arrow_angle) changes up to a maximum of 45 degrees either way. At the moment, arrow_angle is a global variable but I want to change this so that it makes use of pointers. This is what I have attempted:



// In init.cpp
int InitGL(GLvoid)
{
.
.
BatInit();
.
.
}

// In bat.cpp
float arrow_angle;

void BatInit()
{

arrow_angle = 0.0f;

}

// In input.cpp
void InputKeyPressed()
{
int *p = &arrow_angle;

if (keys[VK_LEFT])
{
bat_x_pos -= 0.5f;
arrow_angle -= 10.0f; // This needs to change to use the pointer
}
}


At the moment, I get error C2065: ''arrow_angle'' : undeclared identifier on the first line of the InputKeyPressed method.

Share this post


Link to post
Share on other sites
Thats because input.cpp there is no declaration that arrow_angle exists, try adding this...
extern float arrow_angle;
above the declaration of InputKeyPresses(), or better still put it in bat.h and include bat.h in input.cpp.

Hope thats helpful
X2K

btw: the extern means that it is declared...somewhere (in this case in bat.cpp)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
arrow_angle is defined as a float... so why are you trying to point an integer pointer at it ???

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
arrow_angle is probably not visible in the file input.cpp

Declare it like this in input.cpp
extern float arrow_angle;
Or use header files.
Look up variable scope in a basic C++ book for more information.

And since arrow_angle is still global you don''t need to declare a pointer to it!?!

Make a Bat class instead and put the variable there.

Good Luck,
Patrik

Share this post


Link to post
Share on other sites
This just goes back to my main argument about what the point of pointers is. I now know its advantages but in this case, it seems like you are suggesting I keep it as a global variable (which is how I had it before and it worked fine). I have declared it in a header file called global_vars.h, which I use for all global variables. Should I keep it as a global variable?

Share this post


Link to post
Share on other sites