Pointers? What's the point?

Started by
24 comments, last by Qw3r7yU10p! 19 years, 3 months ago
we've had a rash of "why use pointers?" posts lately.
i hope the search function works soon.

Beginner in Game Development?  Read here. And read here.

 

Advertisement
Quote:Original post by ToohrVyk
Pointers are almost not necessary in C++. They may be useful in some cases, but I'm still wondering if iterators, auto_ptrs or shared_ptrs and references could not be used to almost entirely replace pointers in a game. The only places where pointers would need to be used is when interfacing with pointer-using APIs, or when allocating memory, but this is in my opinion marginal.


Even if you are using iterators and smart pointers you'll still have to access the data using pointer syntax:

SmartPtr ptr(new Thing);
ptr->DoSomething();

I do prefer using references wherever possible and only pointers when necessary, which is usually as you say, with legacy APIs.

Using references means you put the onus for checking the validity of the data on the person giving it to you.
Quote:Original post by Kronikle66
Am I missing something? Why would you ever need to know the memory address of a variable? If you're going to substitute a variable, why not just replace the entire variable altogether instead of replacing the contents of where it's located in memory? I honestly don't understand the benefits of using pointers and addresses, and I still find it to be the hardest code to understand.

Does anyone have any insight as to why they're so crucial or maybe can possibly offer a very easy to learn article about them?

Thanks.

-Kronikle
Try looking up previous posts e.g. http://www.gamedev.net/community/forums/topic.asp?topic_id=178153

Basically, you can't do Jack without them in C++! It would be like not travelling by any other means other than on foot. You aren't going to get very far, and there are a lot of places you can't get to at all.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
Three words: Recursive Data Structures.

Recursive data structures include lists, trees, and graphs.

(using Haskell syntax, which is pretty clear if you're familiar with math)
data List a = Nil | Cons a (List a)data Tree a = Leaf a | Branch a (Tree a) (Tree a)data GraphNode a = Node a [Node a]


In C++ terms, these things would need to be implemented in terms of pointers.
struct ListNode {  DataType nodecontents;  listNode* next;}struct TreeNode {  DataType nodecontents;  TreeNode* left;  TreeNode* right;}struct GraphNode {  DataType nodecontents;  std::vector<GraphNode> edges;}


I know there is stuff in the standard libraries for some of this, but these are trivial examples. Without pointers, your data would need to have a structure that is strictly hierarchical (classes within classes with vectors, etc.), and that could be purely determined at compile time.
---New infokeeps brain running;must gas up!
References in C++ and Java are 'pointers' too...altough Java uses a sort of C++ 'smart pointers'!
If you write a program you must use data and the only way to access data is use their memory address!
And without pointers some problems are impossible to resolve (as Flarelocke wrote).
"Pointers - because it's easier to give 2 people your address than to duplicate your house."
Even if you don't have a great grasp on pointers, you need to know what they are. Most APIs use pointers often to access the computer's resources. For instance, Win32 has handles which are the same as pointers and are used to access the screen and bitmaps, and just about everything else.
-----------------------------Play Stompy's Revenge! Now!
The problem with a lot of pointer tutorials or cs classes, is they don't really make a good case for what you actually use pointers for, and how much you will use them. Once you become familiar with pointers, and continue programming more, you will realize you could not live without them.
Quote:Original post by Kylotan
"Pointers - because it's easier to give 2 people your address than to duplicate your house."


This is the greatest quote ever! (btw, is this an original quote? or else where did you hear it?)

Generally, the higher level the language, the less you need to understand pointers. In modern object oriented languages like C++ or Java, it is possible to create programs without ever being aware of them. However, at lower level, the computer makes EXTENSIVE use of pointers.

If you want to understand why pointers are essential to computing, try learning about an assembly language.

If you look around, you'll find that most computer architectures can only move 4 or 8 bytes at a time (DMA and STRING commands aside). For example, a CPerson class that may take 24 bytes for a name, 4 bytes for an age, 4 bytes for a gender. Such a trivial-seeming class takes up 32 bytes which would require 8 MOV commands on a 32-bit Intel processor. If you are constantly passing CPerson objects to a function, that can take a lot of time for such a small class!

Instead, what a smart programmer would do would be to pass a pointer instead of copying the object. "To pass a pointer" means to take the address of the object (actually the address of the first byte of the object) and pass THAT instead. That takes only one MOV command for the processor. In addition to this, the function can use the pointer to change data in the original object (where if you copied the object, you would have to copy it back to save changes).

In low level languages like C and C++, pointers are probably the most common cause of Runtime errors and are someone difficult to trackdown. Therefore in more modern laguages, pointer usage is often carefully restricted.

In modern OOP languages, pointes still exist, but they aren't called pointers. Whenever you pass by reference, you are actually passing by a pointer. Whenever you instanciate an object in Java or C#, you are creating an object on the heap and receiving a Pointer to that object.

Hope that helped ^^;
Quote:Original post by Tac-Tics
Quote:Original post by Kylotan
"Pointers - because it's easier to give 2 people your address than to duplicate your house."


This is the greatest quote ever! (btw, is this an original quote? or else where did you hear it?)
LOL![lol]
I agree. Brilliant!
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

This topic is closed to new replies.

Advertisement