Archived

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

RaistlinMajere

pointers

Recommended Posts

FtMonkey    122
I don''t know what books your using but if they don''t cover pointers very well you should check other sources as well...Pointers are very useful for games and applications. For example, let''s say you need to have an array of items but you have to ask the user first so you don''t know what size the array will be instead of declaring an extremely large array you can use pointers and create an array which is exactly the size you need it to be. But that''s only an example there''s a lot more you can do with them learn how to use them they can be very useful.

Share this post


Link to post
Share on other sites
Alpha_ProgDes    6929
Basically pointers are used to move around a sizable amount of data or create large data structures, such as a linked list or hash table.

for example:

int sillyFunction (int randomNUMBER)
{
int derived_octagon = randomNUMBER * 8;
return derived_octagon;
}

// now you wouldn''t normally use a pointer to pass an integer

// to a function would you? no of course not. an int (integer)

// is only 4 bytes. so the compiler can copy that variable and

// not suck up too much memory. BUT what about this??


class CCrazyClass
{

public:

long int distance_in_space; // 4 bytes

double maximum_velocity; // 8 bytes

string name_of_shuttle; // x bytes

double shuttle_weight; // 8 bytes

short number_of_tanks; // 2 bytes

float time_in_space; // 4 bytes

string liftoff_base; // x bytes

string landing_base; // x bytes


// functions go here // haha bytes!

}


// now if you wanted to pass that class to a function you''ll be

// passing around at LEAST 26 bytes of data (and that''s assuming

// the class variables are aligned correctly). now a program

// will have several classes and structs like that. you don''t

// want to be copying data like that from function to function

// so you can pass a pointer to it. just like this!


CCrazyClass FirstShuttle;
CCrazyClass *RefShuttle = &FirstShuttle;

// now RefShuttle has direct access to FirstShuttle.


void displayShuttleReadings (CCrazyClass *RefShuttle)
{
RefShuttle.distance_in_space = 28900;
RefShuttle.name_of_shuttle = "SSExplorer";
// blah blah, rest goes here

}

// now in case you''re wondering you did not just copy the whole

// 26+ bytes from the class into the function. all you did was

// copy 4 bytes. All pointers no matter what their datatype are

// 4 bytes. And they are still able to access the same data

// from the variable they point to (check for the declaration)

// (access the data [b]directly[/b]



so in conclusion pointers are normally used, as said before, to
move around large amount of data, dynamically allocate memory for data, or create well-known data structures, such as linked lists, hash tables, binary trees, etc.

Hope this helped!

Share this post


Link to post
Share on other sites
drekkar    122
pointers are just a basic concept of c++ programming, they are used extendedly any place you see fit, they''re not for anything specifically. Just like there''s no specific way to say how a class or struct must be used in games, but how you use them

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Why do people have to complicate pointers so much?

"Basically pointers are used to move around a sizable amount of data or create large data structures, such as a linked list or hash table."

First of all if the guy is asking "what are pointers" and you are going to get into hash tables then you shouldn''t start your sentence with the word "Basically".

Ok...for all of you who don''t know what a pointer is...

At the lowest level everything is 1''s and 0''s right? Well when you are programming we need to know what to do with those 1''s and 0''s. If you say "int x;" then the compiler knows that those bits that are represented by the variable x should be thought of as an integer right? Same goes for "char x" or "double x" or "float x" or any other of the built in types.

Well the creators of the C and C++ languages decided that they would provide a built in type that would represent memory address locations...and that type is a pointer. It "points" to a memory location.

When you do something like "int x" well the program needs to allocate memory for the variable x. And since it is in memory it will have a memory address right? Well if you want to store that memory address you could use a pointer like "int *y = &x;" where the & means the address of x.

Thats all pointers are, WHAT they are has nothing to do with moving around large data structures. What they are GOOD FOR partly has to do with large data structures.

Share this post


Link to post
Share on other sites
Master Mofo    122
Pointers in games are used to point to your data objects. like sprites, backgrounds, meshes, sound clips... you name it. and also they are used to manipulate buffers and memory. for exa mple when you want to load the next frame of a sprite into the buffer, you will use a pointer to copy data from your data and into memory (buffer).

Pointers are very useful in C/C++. they can save you some time and space. in other languages such as Java pointers are removed.(at least from a user point of view). you have to use built in objects and methods to load into and out of memory, or other objects.

Share this post


Link to post
Share on other sites
Peon    276
I typically use pointer to be able to dynamically change the size of my arrays. For example, I might declare a pointer to an int (int *MyVariable) Later, I might want to resize that array for some reason. I would write (MyVariable = new int[500]) And I would have an array of 500 ints. There are a lot of other uses for pointers, but that us probably my biggest use of them.

Share this post


Link to post
Share on other sites
Alpha_ProgDes    6929
quote:
Original post by Anonymous Poster
Why do people have to complicate pointers so much?

"Basically pointers are used to move around a sizable amount of data or create large data structures, such as a linked list or hash table."

First of all if the guy is asking "what are pointers" and you are going to get into hash tables then you shouldn''t start your sentence with the word "Basically".

Ok...for all of you who don''t know what a pointer is...

At the lowest level everything is 1''s and 0''s right? Well when you are programming we need to know what to do with those 1''s and 0''s. If you say "int x;" then the compiler knows that those bits that are represented by the variable x should be thought of as an integer right? Same goes for "char x" or "double x" or "float x" or any other of the built in types.

Well the creators of the C and C++ languages decided that they would provide a built in type that would represent memory address locations...and that type is a pointer. It "points" to a memory location.

When you do something like "int x" well the program needs to allocate memory for the variable x. And since it is in memory it will have a memory address right? Well if you want to store that memory address you could use a pointer like "int *y = &x;" where the & means the address of x.

Thats all pointers are, WHAT they are has nothing to do with moving around large data structures. What they are GOOD FOR partly has to do with large data structures.




actually if you read a little harder , you''ll see he asked "what are pointers used for...?" not "what are pointers?"
those are two different questions.''
so my explanation is valid. thank you.

Share this post


Link to post
Share on other sites
Aside from dynamic memory allocation, pointers are also used all the time with polymorphism. They provide a way to refer to many object types through a pointer to a common parent type allowing you to group functionality of objects which are related and share the same logic, yet whose implementations can vary. If you are referring to C and not C++, the same holds true, only you''d be working directly with vtables and vtable pointers.

Share this post


Link to post
Share on other sites