pointers, arrays.

This is a discussion on pointers, arrays. within the Game Programming forums, part of the General Programming Boards category; Could someone please give me a good example of how pointers and arrays are used effectively in a game? I ...

  1. #1
    C++ and openGL Raeliean's Avatar
    Join Date
    Jul 2005
    Posts
    28

    pointers, arrays.

    Could someone please give me a good example of how pointers and arrays are used effectively in a game? I just don't get it.. it's hard to learn when you think something is pointless.
    Be inspired.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Let's try this one. And consider the part about "Any claims that pointers are faster than array indexing are false." to be somewhat contested.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Epo
    Epo is offline
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    Here's an example. A quick one.
    Code:
    class cPlayer
    {
    public:
    	cPlayer();
    	virtual ~cPlayer();
    
    	//Positions
    	float pX, pY, pZ;
    
    	//Rotations
    	float rX, rY, rZ;
    
    	//Velocities
    	float vX, vY, vZ;
    
    	//Accelerations
    	float aX, aY, aZ;
    
    	//Player Name
    	char pName[20]
    };
    //A class for one player
    Now, without arrays, if you wanted to declare 100 players, you'd have to do something like:
    Code:
    cPlayer PlayerOne;
    cPlayer PlayerTwo;
    cPlayer PlayerThree;
    ... //Insert Four to NinetyEight in here
    cPlayer PlayerNinetyNine;
    cPlayer PlayerOneHundred
    Or, why not just use:
    Code:
    cPlayer Players[100]; //Note, that goes from Players[0] to Players[99]
    And, an even better choice would probably be Linked Lists (your question didn't ask about these, so you'll have to search up on your own. But don't worry, there are alot of resources out there on Linked Lists). The reason why I would recommend Linked Lists is because you're not bound by a specific number of players.

    Say you made an array of 100 players, what if you wanted 101 players?

    Or, to be safe, you declare 1,000,000 players in an array, now you're most likely just wasting a lot of memory. And you're still stuck in that bad case of "What if 1,000,001 players want to play?".

    With a Linked List (or the C++ Vector, though I prefer Linked Lists cause I like writing as much as I can myself, it helps me know what does what) you can add/delete players on the fly, so you'll always be using just the right amount of memory. Though I hear Vectors do some crazy thing where they use twice as much memory as necessary? Maybe somebody could tell me I'm wrong here...or clarify?

    As for pointers, say you had a function that updates a player structure, such as:
    Code:
    cPlayer UpdateXYZ(cPlayer Temp)
    {
    	//Update our velocities
    	Temp.vX += Temp.aX;
    	Temp.vY += Temp.aY;
    	Temp.vZ += Temp.aZ;
    
    	//Update our positions
    	Temp.pX += Temp.vX;
    	Temp.pY += Temp.vY;
    	Temp.pZ += Temp.vz;
    
    	return Temp;
    }
    
    //And it would be used as follows:
    int main(void)
    {
    	cPlayer Player;
    
    	//INITIALIZE ALL PLAYER VALUES HERE
    
    	Player = UpdateXYZ(Player);
    
    	return 0;
    }
    When calling UpdateXYZ(), the function has to COPY all of Player's values into Temp, which is not only SLOW, but wastes memory. And then, once it's all updated, it has to COPY all that information back into Player in main().

    Instead, something like:
    Code:
    void UpdateXYZ(cPlayer *Temp)
    {
    	//Update our velocities
    	Temp->vX += Temp->aX;  //Same as: (*Temp).vX += (*Temp).aX;
    	Temp->vY += Temp->aY;
    	Temp->vZ += Temp->aZ;
    
    	//Update our positions
    	Temp->pX += Temp->vX;
    	Temp->pY += Temp->vY;
    	Temp->pZ += Temp->vz;
    }
    
    //And it would be used as follows:
    int main(void)
    {
    	cPlayer Player;
    
    	//INITIALIZE ALL PLAYER VALUES HERE
    
    	UpdateXYZ(&Player);
    
    	return 0;
    }
    Now we're not copying, but instead, we're accessing the values of Player directly, and changing them directly. No extra memory needed (except to store the pointer to our cPlayer type), and much faster since there's no copying.

    Now, there are of course better ways to do any of these functions. But there it is, and hopefully, it's accurate.
    Last edited by Epo; 07-16-2005 at 08:24 PM. Reason: Prettying it up
    Pentium 4 - 2.0GHz, 512MB RAM
    NVIDIA GeForce4 MX 440
    WinXP
    Visual Studio .Net 2003
    DX9 October 2004 Update (R.I.P. VC++ 6.0 Compatability)

  4. #4
    ---
    Join Date
    May 2004
    Posts
    1,379
    Quote Originally Posted by Dave_Sinkula
    consider the part about "Any claims that pointers are faster than array indexing are false." to be somewhat contested.
    Contested how? isn't it just the same?

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Contested how?
    The statement was too general for Dave's taste. It's been changed to reflect the difference between naive changes based on unfounded performance differences between pointers and indices, and careful, advanced changes with the intent of improving performance.

    What I'm surprised about is that there aren't more contested parts of that tutorial.
    My best code is written with the delete key.

  6. #6
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Code:
    Though I hear Vectors do some crazy thing where they use twice as much memory as necessary? Maybe somebody could tell me I'm wrong here...or clarify?
    Thats a possibility of vector, since it doubles the size of the array when you go over the max, I believe (I assume its an array). Vector uses some sort of array (I believe thats part of what I read), and its resize function is the part that doubles its elements when you go over its max. Lets say you created your own array type class, that acted how a vector does. When you go over the amount of elements in your array (if you want to add player[3001] when the max of your array is player[3000]), a vector will resize your array to 2x the size of the array, leaving you with a 6000 in size array. Whereas if you specify the resize function yourself you could only add say 50 more allocated spots for players, leaving you with player[3050] as max, which makes more sense.

    Could someone please give me a good example of how pointers and arrays are used effectively in a game? I just don't get it.. it's hard to learn when you think something is pointless.
    Well.. the point of pointers is to save processing time, and temp. memory? and arrays can be passed as parameters of course, so add the two together. Lets say you have an array of players, like Epo's example of there, and you want to search the array for players whoes level is 10-50. To search the array you simply pass the pointer of the array to a function, and check the say GetHealth() function from each array element (player[1], etc.), assuming its a class that has the GetHealth function. Also if you wanted to change all of the players health to 0, that would be a better example of why you would have to use a pointer, and not copy the array over, because you have to be editing the memory address of the actual players. Thats all assuming the player array isnt globa, which it probably wouldnt be in a real game. Agh not such good examples.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with returning arrays using pointers
    By cuba06 in forum C Programming
    Replies: 9
    Last Post: 11-23-2007, 09:40 AM
  2. Array of Pointers to Arrays
    By Biozero in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 02:31 PM
  3. Pointers and multi dimensional arrays
    By andrea72 in forum C++ Programming
    Replies: 5
    Last Post: 01-23-2007, 03:49 PM
  4. pointers and arrays..
    By ahming in forum C Programming
    Replies: 1
    Last Post: 04-24-2004, 03:12 AM
  5. Stack functions as arrays instead of node pointers
    By sballew in forum C Programming
    Replies: 8
    Last Post: 12-04-2001, 10:13 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21