Why the hell does anyone need to know pointers?
What the hell is a pointer for. Finding the address of a variable, I know, but for what damn reason. Can someon:
Give me acouple answers as to how a pointer can help my programming.
And if I will ever need them for games.
~ from the depths of the ocean
You can allocate memory at run time:
int* p = new int[50];
Commonly, you don''t know how much memory you will need in an application, therefore you will need to be able to allocate more memory if needed.
Another use:
struct hog{
double[5000];
};
void f(hog h);
void f(hog* h);
...
hog aHog;
f(aHog); /* slow, must copy 5000 doubles */
f(&aHog); /* fast, only must copy a 32-bit int */
ECKILLER
int* p = new int[50];
Commonly, you don''t know how much memory you will need in an application, therefore you will need to be able to allocate more memory if needed.
Another use:
struct hog{
double[5000];
};
void f(hog h);
void f(hog* h);
...
hog aHog;
f(aHog); /* slow, must copy 5000 doubles */
f(&aHog); /* fast, only must copy a 32-bit int */
ECKILLER
Pointers also allow you to perform arithmetic on the actual variable's address to a much greater extent than available to automatic and static variables. Also, one pointer variable could be used as an array if enough memory has been allocated.
Math.
Edited by - Mathematix on August 11, 2001 6:44:38 PM
Math.
Edited by - Mathematix on August 11, 2001 6:44:38 PM
Yes, you will need pointers to program a game if you use C or C++. If you use soemthing like VB or Java, the pointers are still there but the language wraps them up to "protect" the code.
Hmmm... Pointers, eh? The word "Virus" comes to mind immediatly... But seriously, Pointers are one of the things that make C such a powerful language. Besides, even if all your doing is a bunch of printf()/scanf() statements, you still ned a basic knowledge of poiter usage. (thats what the & symbol is there for!)
-----------------------------
Vash the Stampede
"Love & Peace!"
-----------------------------
Vash the Stampede
"Love & Peace!"
Pointers are also great to pass to a function that requires a lot of parameters. Er, I mean, you pass pointer to your params, instead of the params themselves.
Here is some code to ''splain:
// here is my parameter structure:struct ParamStruct{ int x; int x2; int y; int y2; char stuff; char other; }; // now here is a function that uses the above paramsint NeatFunction(ParamStruct* ps){ // code that uses params}// to call my function, I''d do this:ParamStruct ps; ps.x = 10;ps.x2 = 20;ps.y = 30;ps.y2 = 40;ps.stuff = 50;ps.other = 60;int result = NeatFunction(&ps);
Now instead of passing six params, I just pass one. I use this specifically in a multi-purpose blitter function I wrote, where the majority of the blitter params stay the same from call to call, and only one or two params change at a time. Seems more efficient.
Hi,
Here are some things pointers are good for:
Dynamic memory allocation
int *p = new int[256];
Linked lists ( basically dynamic memory allocation ^-^ )
Binary trees
Changing values in a function ( C++ uses reference parameters for this ).
...But mostly, dynamic memory allocation :-D It''s fun. You''ll love it.
--Nairb
Here are some things pointers are good for:
Dynamic memory allocation
int *p = new int[256];
Linked lists ( basically dynamic memory allocation ^-^ )
Binary trees
Changing values in a function ( C++ uses reference parameters for this ).
...But mostly, dynamic memory allocation :-D It''s fun. You''ll love it.
--Nairb
Lists and trees are not good examples as they don''t have to be implemented using pointers.
ECKILLER
ECKILLER
people always forget to list the most important reason: pointers create relationships.
Let''s say you have a Monster class/struct. It has an x and y coordinate, hit points, and a current target. For x,y and hp we use ints. What do we use for current target? We use a pointer to whatever it can target. We can''t use a variable and set it equal to whatever we want to target. That would make a copy. By using a pointer our monster "knows about" a target, he doesn''t "have" a target. There are lots of different kinds of relationships and pointers are used for many of them. Some other relationships:
"has": this means that the data is part of a greater whole, the x variable in a point struct is part of the point. We use normal instance variables for the has relationship
"knows about" here we usually use pointers, though I think references would work for static relationships
"owns" this one is also done with pointers, but here there is a responsibilty for creation and destruction
"is a" where one type is a subtype of another, use inheritance
those are the basic ones, when deciding how model something think about what kind of relationship you are making.
Let''s say you have a Monster class/struct. It has an x and y coordinate, hit points, and a current target. For x,y and hp we use ints. What do we use for current target? We use a pointer to whatever it can target. We can''t use a variable and set it equal to whatever we want to target. That would make a copy. By using a pointer our monster "knows about" a target, he doesn''t "have" a target. There are lots of different kinds of relationships and pointers are used for many of them. Some other relationships:
"has": this means that the data is part of a greater whole, the x variable in a point struct is part of the point. We use normal instance variables for the has relationship
"knows about" here we usually use pointers, though I think references would work for static relationships
"owns" this one is also done with pointers, but here there is a responsibilty for creation and destruction
"is a" where one type is a subtype of another, use inheritance
those are the basic ones, when deciding how model something think about what kind of relationship you are making.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement