Jump to content
  • Advertisement
Sign in to follow this  
thugkilla

Pointers whats the point?

This topic is 4851 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I always say pointers in programs and I always tried to avoid them,but it has come to my thoughts that I need to know what they are for.I don't understand when to use them,why to use them,and what the hell they are.

Share this post


Link to post
Share on other sites
Advertisement
In lamens terms, when you don't use pointers and you pass a variable through a parameter, it makes a copy of that variable. When you use pointers, it passes the memory address that variable is stored at, thus using much less memory than without. Using pointers on variables such as ints and bools aren't needed as much but using pointers to pass objects and structures as parameters saves a lot of memory if they are big.

Share this post


Link to post
Share on other sites
Well, when programming in pure C there is just no way you can do anything non trivial without using pointers. C++ covers a lot of that up with classes and such. Like the containers vector, list, stack, etc. These are all made using pointers so if you use those and ever want to understand them you're gonna need to take a look at pointers. Many languages don't have pointers so really if you want to remain ignorant go ahead. But you implied you did want to know what they were for so read on:


There are a few uses of pointers in C++. One is reserving memory using new. You must have a pointer to that memory to use it. If you wanted to load a map for a game you'd use new to put all the map data in an array or something. Or you could use a container. Your choice, but the container also uses a pointer.

Another is passing a large data structure to a function. Rather than copying, say, 50 bytes, you just copy a few for the pointer and then access the data structure through it's address.

Polymorphism uses pointers. You have a base class Quadriped for instance with some methods "walk" "run" or "die" which all quadriped's do in their own unique way. Then you create a derived class Dog with it's own way of doing stuff, then another derived class Cat with another way to do stuff. And you have a list of these objects and you just want to treat all dogs and cats as quadripeds. You do this by accessing pointers. In code it'll look something like this(I hope--I haven't done polymorphism in awhile):
Quadriped * animals[2];
animals[0] = new Dog;
animals[1] = new Cat;
animals[0]->run();
animals[1]->run();
delete animals[0];
delete animals[1];


So in a game you can treat all characters exactly the same just by having a list of characters and running through them. Hope that helps.

Share this post


Link to post
Share on other sites
A pointer is a memory address.

All objects in your program are stored at some position in memory. That position is identified on most platforms via a 32-bit (or 4-byte) integer.

A pointer holds that position or address, and is thus equivalent on most platforms to a 32-bit integer.

Often times you will have a large, memory-intense object which, for example, a function needs to use. Instead making a copy of that huge object, thus taking up twice the memory, you instead pass where it is in memory, i.e. a pointer to it.

C++ has built-in mechanisms for "dereferencing" pointers, i.e. treating them as if they were the objects to which they point. The notation for this is "->".

An example:


class 3DModel {
// LOTS of data...
};

3DModel gun_model = new 3DModel("gun.3dmodel", ...);

// You now have the gun model loaded into memory. However, there
// are three characters in your game which will use the same model.
// Instead of wastefully copying the model for each, you merely tell
// each of them where it is in memory, and they can act on it
// using that information.

// ...

Brute::init(3DModel* GunModel) { gun = GunModel };

// ...

Brute brute = new Brute(gun_model);

// You have now given Brute a pointer to gun_model, i.e. it's address
// in memory.

// ...

Brute::render() { gun->render(); }

// Brute uses the "->" operator to treat 'gun' as if it were that
// which it points to.

// All-in-all, Brute was able to use gun_model without having to
// wastefully copy all that memory. This is the main use of pointers.

Share this post


Link to post
Share on other sites
If u are a windows XP user create a shortuct to some file.Then right click it and select properties and then general tab. You will see that shortcut is only around 700 bytes. The shortcut is just a shortcut to the file as pointer is shortcut to the some place in memory.

When you declare a pointer like this

(1)SOME_TYPE (2)* (3) name_of_pointer = (4 adress of)& (5)some_type_variable;
ignore the parenthesis :)

1)is the type of variable we are pointing to
2)asterisks means that we are declaring pointer to the 1)
3)name of our pointer
4)if we have some variable (5) we can take it adress by & (adress of operator)
5)some variable defined previously

you declare a pointer to the beggining of memory ocupied by some_type_variable.
To program can't recognize if is it a beggining memory of integer,some class or
some other type ( SOME_TYPE 1 )so we need to tell compiler to what type we are pointing to (1).

Look at this if we need to pass a whole 3d model in a function as a parameter
( foo( 3D_Model model);
foo(our_model);
)
we will copy our whole model to the function !!!
but if we do like this
( foo( 3D_Model* model)
foo(&our_Model)
)
we would only pass 1 or 2 bytes insted of 5+ megabytes so calculate the boost you have.

If we use pointers as function parameters we would work on objects passed to us,
if we use some_type (not pointer to SOME_TYPE)we would copy parameter and do our work on a copy of a parameter like this


void foo (int some_int)
{
some_int = 2;
};
{
int a = 5;
foo(a);
cout << a;
}




Cout will write a 5 cos we have assigned a 2 to the copy of "a" if we used a pointer to the some_int we would get a 2 as result

I hope this has helped you.

EDIT: Oh 3 post till i written this :D
Did anyone played a heawy weapon game from popcap games ?
Game rocks and i am not from the Arizona!!!

Share this post


Link to post
Share on other sites
Pointers allow you to manipulate, create, and manage data in ways that regualr variables cannot.

One big example is the linked list. Linked lists are classes (or structures) that each contain a pointer to another structure of the same type. Each class "points" or is linked to another class.

Pointers also allow you to create new variables at runtime. Combined with a linked list, you can create and manage objects in a list of any size (unlike the fixed size of an array).

What this means is you can have a non-fixed amount of a class or structure. For example, if you have an array of 50 enemies ( enemies[50] ) to represent the enemies in your game, 50 is the maximum amount that you could have. If you used pointers and a linked list to make enemies "on the fly," you could have an unlimited number of enemies. Not only that, but you can create enemies when you need them, and delete them when you don't. So at some times you may have 20 enemies, and at others 500. This is much more efficient than making large arrays when large classes or many classes are used.

Pointers and arrays are also very similiar (at least in c and C++ ) if you create an array of enemies ( Enemy enemies[50] ) then a pointer to an enemy class ( Enemy *penemy )m and set hte pointer to the array ( penemy = enemies ) these two expressions would have:

penemy and enemies
or
penemy[n] and enemies[n]
or
*penemy and enemies[0]

These are only a few uses for pointer, there are many more. When making games, pointers are actually one of the most helpful and useful data structures that you can use. They are very versitile and powerful, allowing you to do many things with data.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!