Pointer Clarification

Started by
14 comments, last by Lactose 8 years, 7 months ago

int main()
{
void runStandardIMNEWintro();
}

Ahem...

I've punched through "Beginning C++ Through Game Programming (4th ed)" and also reviewed numerous blog posts and forum threads about pointers. Alas, I still struggle.

Can someone (as best you can) attempt to draw out a real-world example of how Pointers are useful?

I understand that they link to the information at the "address" of a variable, I just don't understand the... Point?

If possible, review the Class below and provide an example of why a Pointer would be used and why it is useful:


class Goblin
{
public:
int gobHP;
void gobSpell();

private:
m_Name;
}

If the above seems too bizarre (Or I'm just bad at giving an example of something to use...) feel free to lay it out differently. I'm in the process of coding a text-based game as my first foray into true C++ coding and want to make sure that I am implementing the cleanest and most effective code that is (reasonably) possible.

Thanks in advance!

Advertisement

As you already know, pointers are just addresses of stuff in memory (plus some type information). It's okay that you don't understand what they are good for, it was the same for me when I started. But you will stumble upon a use case sooner or later and then it will probably be like "whoa, it's not that complicated actually".

There are several cases, where pointers (or in modern idiomatic C++ increasingly often smart pointers, but that's a somewhat more complicated concept) play an important role:

  • You use dynamically allocated memory, say with new or malloc. These things return the address of the memory they allocated, which you have to store somehow in order to use the memory. That "somehow" would be a pointer of some kind. Again, in C++ you usually use a container class instead of that.
  • You want to pass around a piece of memory to be used in other parts of your program. Either you want this other part to directly manipulate a piece of data you have in the local part or the data is just to big to pass it as a function argument by value. In both cases you would pass a pointer (or a reference, which is a related concept).

There are more use cases, but these may be the most common ones. For your Goblin class, there is nothing I would see where you could use a pointer to your advantage. But using a pointer usually results from the interaction of different code parts (and you show only a declaration of a class, no actual code).

A pointer says "A thing is over there", rather than making copies of things everywhere.

Almost everywhere in programming you want to only have a single copy of something, then have pointers over to the original thing. You do not want to have hundreds or thousands of copies of that thing, nor do you want to create copies for no good reason.

When you want one goblin to deal damage to a zombie, you don't want to have a function that says "This is the goblin that hit you", but in the process create an entire duplicate copy of the goblin to send in the data. Instead you pass a pointer to the actual goblin that hit. It says "look on the other end of the pointer, that is the thing that hit you."

The benefit of using pointers is that they give you a level of indirection -- its often handy to know where to find a thing without having to have the actual thing with you, carrying it around all the time. To you as a programmer, a variable that's a pointer is basically an address with a name, and in most programming languages, having a name for something grants you the power (potentially) to change it.

So, for example, a pointer can be null or it can be a valid address -- that's a crude but useful mechanism for saying "Either I have no information (pointer is null) or I have information and its located here". A pointer (an address with a name) can also be changed -- that means it might point to one thing at one point in your program, or to a different thing at a later time -- by dereferencing a pointer (dereferencing collapses a pointer into what it points to), you essentially have a single name for different objects at different times in your program -- for example, say that your protagonist character can target single enemies and track their movement; a pointer can point to Goblin A now, and Goblin B later.

Another thing you can do with a pointer is increment and decrement it to look at an adjacent object of the pointer's type -- this means, for example, that you can start at the beginning of an array and walk towards the end looking at each element, and you can do that without an indexing for-loop (maybe you want to use a do or while loop.

Pointers are also lightweight (almost always a machine word, so 32 or 64 bits) compared to many classes or structures you might use or write. If you were to pass parameters of those heavy types by value (not using a pointer or a reference) then you copy the whole thing pessimistically (you pay the cost of copying even if you don't actually use it later) -- but if you pass those heavy types by pointer or by reference, then you only pay the small cost of copying the pointer instead -- later you can use the pointer to find the thing and you can look at it where it stands (no copying) if you don't need a unique copy of it; you can make a copy of it if you need, but you only pay the cost of doing so when you do.

throw table_exception("(? ???)? ? ???");

Let me answer your question by asking you a question. Say you have a text file where you wrote down goblins with their hp and names, now your read the file and you get the goblin names and hp values. Your program doesn't know anything about the number of goblins, it'll only find out after reading the file, how would you make an array of all these goblins?

When you want to ask something from another person, or give him/har something, it's useful if they are nearby, say in the same room as you.
As long as they all stay near you, everything is fine. However others may also want to speak with them (not to mention everybody has a life of his/her own), so how to solve this?

We have invented phone numbers and addresses for this. It gives you the possibility to speak with them or to drive to them and bring or get something, even if they are not always in the same room as you.
The phone numbers and addresses act as pointers for you, so you can locate the other person, and do your business with him/her when needed.

Can someone (as best you can) attempt to draw out a real-world example of how Pointers are useful?

Say you went to a party, you are with a heavy coat, you pass by the cloakroom and leave your cloak, the guy working there gives you a paper that says "Wardrobe 16". You enjoy the party and before you leave you go back to the cloakroom and deliver the paper, the guy gives your coat back.

In this example the wardrobe is the memory block, the coat is the memory content and the paper is the pointer.

There are several utilities for pointers, but in the general case they are useful to access memory from scopes other than the one it was created. The use and the scope is determined by where the memory is being stored (text segment, stack or heap), but I guess this would be a deeper discussion.

Currently working on a scene editor for ORX (http://orx-project.org), using kivy (http://kivy.org).


Say you went to a party, you are with a heavy coat, you pass by the cloakroom and leave your cloak, the guy working there gives you a paper that says "Wardrobe 16". You enjoy the party and before you leave you go back to the cloakroom and deliver the paper, the guy gives your coat back

I like this metaphor!

First, thank you very much to everyone that shared their feedback and knowledge about the subject. I definitely feel that everyone's comments have set me in the right direction to fully understand pointers. (As with most things, I imagine using them in a program will be the easiest way to fully grasp their usefulness.) Below are a few poster-specific questions/comments:

A pointer says "A thing is over there", rather than making copies of things everywhere.


Almost everywhere in programming you want to only have a single copy of something, then have pointers over to the original thing. You do not want to have hundreds or thousands of copies of that thing, nor do you want to create copies for no good reason.

When you want one goblin to deal damage to a zombie, you don't want to have a function that says "This is the goblin that hit you", but in the process create an entire duplicate copy of the goblin to send in the data. Instead you pass a pointer to the actual goblin that hit. It says "look on the other end of the pointer, that is the thing that hit you."

To clarify on your comments.. If I were to build two functions, one that provides the last creature the goblin damaged and another to read the goblin's current HP amount. I would use Pointers to read the data for that particular Goblin. The alternative would be building a new goblin into each function in an attempt to read that data... Which as I type that, doesn't even seem like it would work.

(Also, I would need separate pointers if there were more than 1 goblin that needed variables referenced? Correct?)

Let me answer your question by asking you a question. Say you have a text file where you wrote down goblins with their hp and names, now your read the file and you get the goblin names and hp values. Your program doesn't know anything about the number of goblins, it'll only find out after reading the file, how would you make an array of all these goblins?

Hmm. Well, I suppose I would do something like the following: (Forgive me for any newbish formatting):


// Build a Class initially...

goblin totalgoblins[MAX_GOBS];

int numGobs = 0;
totalgoblins[numGobs++] = "goblin";

Hmm, well while I attempt what you asked, I am realizing that building an Array to represent items like HP values and Names isn't working very well... So, is it safe to say that the following is possible:

Goblin 1's Address (which is Pointed to...): 123

I could pull specific information from that address such as HP values and names? I ask because I was under the impression only a specific type of integer could be stored at the address, i.e. and INT or STRING value, for example.

Say you went to a party, you are with a heavy coat, you pass by the cloakroom and leave your cloak, the guy working there gives you a paper that says "Wardrobe 16". You enjoy the party and before you leave you go back to the cloakroom and deliver the paper, the guy gives your coat back.

In this example the wardrobe is the memory block, the coat is the memory content and the paper is the pointer.

There are several utilities for pointers, but in the general case they are useful to access memory from scopes other than the one it was created. The use and the scope is determined by where the memory is being stored (text segment, stack or heap), but I guess this would be a deeper discussion.

This is an amazing analogy! Thank you! Similar to a question I asked above. Would the 'coat' hold multiple variables? (i.e. Name of the coat, color, size, etc.?)

Thanks again everyone!

I think it could be useful for you to research a bit more on how a computer works on a low level.

For the CPU, there are no such things as "objects", "strings" or even "variables", those concepts only exists in your compiler.

For the CPU, It is just one huge block of memory, where each byte has an individual address.

A variable is then simply a human readable name for a particular address (which address is impossible to know, and could be different depending on circumstance)

At that address, you can technically choose to store whatever you want. Its just bytes of data. It can be strings, integers, floats, goblins, or whatever.

The "type" of the variable however, defines what is actually stored at the address. If the variable is of type "int", there is an int stored there. If it is of type "goblin" there is a goblin stored there.

A pointer then, is a special type of variable. At its location in memory, instead of storing an actual "goblin", or "int" it stores the address of another variables location, so it can share the data of that variable.

This topic is closed to new replies.

Advertisement