Skip to main content
gamedev.net
🔒 Locked

Pointers and Passing

Started by programmermattc Aug 1, 2006 at 12:42 PM 4 replies 500+ views
Original Post
programmermattc
programmermattc
I've been reading up on pointers and references and finally realized passing by a pointer changes the value of the original variable (like passing a pointer of an int of 5 to a function that adds 3 but returns void). Now trying to understand this in game programming maybe I'm not seeing it right. Many functions I've seen accept some type of pointer, is this because it's easier to change the actual value rather than return a number and set that number equal to the original variable? Also I've heard pointers are a big 'no-no' in game programming but I've seen them in many programs. (Maybe I'm noticing them in the wrong context) Is this true?
=============================RhinoXNA - Easily start building 2D games in XNA!Projects
ToohrVyk
ToohrVyk
Quote:
Original post by programmermattc
I've been reading up on pointers and references and finally realized passing by a pointer changes the value of the original variable (like passing a pointer of an int of 5 to a function that adds 3 but returns void). Now trying to understand this in game programming maybe I'm not seeing it right. Many functions I've seen accept some type of pointer, is this because it's easier to change the actual value rather than return a number and set that number equal to the original variable?


For numbers, you might not see the point. If the passed object is larger (a texture, for instance), you don't want to create a modified copy every time you change a pixel, but rather modify the original without copying.

However, remember that it is usually preferable to pass objects by reference (instead of pointers), because it eliminates the possibility of a null pointer.

Quote:
Also I've heard pointers are a big 'no-no' in game programming but I've seen them in many programs. (Maybe I'm noticing them in the wrong context) Is this true?


No, it's false. Pointers are necessary because referencing is useful, and references themselves are not re-seatable. However, it is true that it is often preferrable to use smart pointers, such as boost::shared_ptr, or std::auto_ptr for RAII.

Palidine
Palidine
In C++ pointers are necessary for game programming. With the amount of memory that games throw around you absolutely need dynamic memory allocation, which necessitates pointers.

Generally when people pass a pointer to a function they should really be passing a reference, and generally it should be a const one. Typically there's no good reason to pass a pointer as an argument instead of a reference; there's obviously exceptions to this. =)

I personally don't like passing function arguments under the assumption that their value will change. Sometimes it's necessary like when you have a function that's much more efficient calculating 2 values than having 2 functions independently calculate that value. But in your int example, that should certainly be the return type, not an int* argument. Generally when you see functions like that it's because someone just learned how to use pointers, thinks it's cool, and so use them to do silly and confusing things.

-me
nullsquared
nullsquared
Quote:
Original post by Telastyn
Not a big no-no, but perhaps a little one. In modern C++, you're better off using references or smart pointers rather than common 'bald' pointers where you can.


A reason you might be seeing lots and lots of pointers is if you're using a C library (there is no such thing as a reference in C)... And since many people use C libraries with C++, you'll see a lot of pointers.

But then again, most games now need dynamic allocations, so pointers are a must.
programmermattc
programmermattc
Looking back at some tutorials now maybe I haven't seen too many pointers lol. So does this relate to why certain items (like ZeroMemory()) take an address-of of a variable instead of the variable itself? (Because the function would then run a function-scoped copy)
=============================RhinoXNA - Easily start building 2D games in XNA!Projects

Topic Locked

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

Sign in to reply to this topic.