Skip to main content
gamedev.net
🔒 Locked

when to use a pointer

Started by Trashcann 7 replies 300+ views
Original Post
Trashcann
Trashcann
When exactly do you USE a pointer? i know how to declare them but why and when and where use them?
ermokwww.truevision3d.com
cowsarenotevil
cowsarenotevil
I replied in your other thread. If my reply didn''t make sense I''ll try to explain better.
-~-The Cow of Darkness-~-
Trashcann
Trashcann
yah thanks, i answered it there, just felt like making a new thread for this topic for the peeps who didnt see my other thread
ermokwww.truevision3d.com
cowsarenotevil
cowsarenotevil
Pointers are just like variables, but they can''t hold anything and don''t take up any memory unless they are "new''ed." This is useful because you can create a class with a pointer to itself in it, and then "new" the objects (instances of the class) inside the class with a recursive function. Then you can write data to each "node" (instance of the class) and initialize or delete the pointers as needed to shrink/grow the list. Basically you use them to create variables that you can make or get rid of as needed.
-~-The Cow of Darkness-~-
Trashcann
Trashcann
oh ok. but all that about the list linking as was mentioned in my other thread, i dont understand it lol i guess ill just learn about it later. thanks bro
ermokwww.truevision3d.com
Way Walker
Way Walker
quote:
Original post by Trashcann
oh ok. but all that about the list linking as was mentioned in my other thread, i dont understand it lol i guess ill just learn about it later. thanks bro


Linked lists are one of the most common ways of representing a collection of objects. Here's some (C) code


#include <stdio.h>
#include <stdlib.h>

struct myIntList {
struct myIntList *next;
int data;
};

int main(int argc, char **argv) {

// Create the list

struct myIntList list, *current, *new;

// list is the first link, let's initialize it

list.data = 1;
list.next = NULL;

// Add an object to the list

new = malloc(sizeof *new);
if (!new) { return EXIT_FAILURE; }
new->data = 2; // Initialiaze the new link

new->data = NULL;
list.next = new;

// Print out the data in the list

for (current = &list current != NULL; current = current->next) {
printf("%d\n", current->data);
}

}


Thus, each link in the list has some data and a pointer to the next link in the chain.

[edited by - Way Walker on July 20, 2003 1:02:07 AM]
Zipster
Zipster
quote:
Original post by cowsarenotevil
Pointers are just like variables, but they can''t hold anything and don''t take up any memory unless they are "new''ed."

But pointers do take up memory, 4-bytes on a 32-bit machine. While they aren''t like classical variables that you assign classical values to, they still have to hold the addresses. That requires space.
Trienco
Trienco
quote:
Original post by Zipster
But pointers do take up memory, 4-bytes on a 32-bit machine. While they aren''t like classical variables that you assign classical values to, they still have to hold the addresses. That requires space.


to go one step further. despite being another type they can be regarded a simple unsigned int containing the address. as far as i know some compilers tend to store information about their state by using certain values. cant remember where i found that list, but if they end up like 0xcdcdcdcd or other obviously not so random values you can tell whats up with them (except of course "dont try to use it").

f@dzhttp://festini.device-zero.de
Teknofreek
Teknofreek
Pointers are useful in a variety of situations. Probably the most common use of them is when you have a group of variables(eg Characters in a game, Pixels in an image, etc). There are a number of ways to go about managing these(eg. Array, Linked-list, etc) but most of them boil down to using pointers in some fashion.

Another way that they are useful is that, because they are just an alias for a location in memory, that you can have one set of the real variables and use pointers to make different lists which all use some or all of those same variables. For example, let''s say that you have a bunch of characters in your game. You might want:

- One list to handle all of them for the basic stuff
- Another list for the characters that are on Team A
- Another list for the characters that are on Team B
- Another list for bots
- Another list sorted in space for collision
- etc

So, in this situation you can have just one set of Character variables, but you can have a bunch of different lists which point at one or more of these variables for each of your tasks.

Does that make sense?

-John
John

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.