Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I am writing a game and my friend has offered to help me as it is a RPG and will take a long time to do the "scripting" bit of the game.

The problem is IMO he's not that good a programmer :( (add flame war here). He has only programmed in Java and VB and keeps saying really stupid things to me like "Why don't you drag and drop an onClick event" to design my UI when I'm using DirectX.

I tried explaining pointers to him but his response was, if it's just a variable that holds a memory address, why don't you just use an int?

I create an instance of an attack class and give the creature a pointer to it so if several creatures use the same attack there is only one instance of it. He keeps saying why not put if statements in the creature class for every attack class and set true for the ones that are there.

He has programmed mainly in VB and a little in Java just to learn OOP. How can I explain advanced C++ concepts like pointers and memory management to him? He just doesn't understand there are no super functions like form.show in C++.

share

migration rejected from stackoverflow.com May 9 at 7:29

This question came from our site for professional and enthusiast programmers. Votes, comments, and answers are locked due to the question being closed here, but it may be eligible for editing and reopening on the site where it originated.

closed as too broad by MichaelT, GlenH7, Dynamic, amon, gnat May 9 at 7:29

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.If this question can be reworded to fit the rules in the help center, please edit the question.

    
Well, I'd say that a pointer is somewhat similar to an int (it might not be of the same size on different architectures though), but the compiler knows what type of data the pointer, well, points to - and that it is an address rather than just a number. For him as a Java programmer, you might try and tell him the pointer is similar to an object reference which also is a memory address with type information (primitives are handled differently though, you can't put locally created ones on the heap, i.e. method local variables etc.). –  Thomas Jun 27 '11 at 11:13
14  
Point him here c2.com/cgi/wiki?EmptyYourCup –  Wudang Jun 27 '11 at 11:13
    
@cularis i have thought of this but buy refusing his help it is like telling him hes not good enough to work with me and hes a really good friend –  Skeith Jun 27 '11 at 11:14
10  
Hmmm, it's interesting how rude and dismissive everyone's comments have been so far. Sure, the guy sounds completely ignorant when it comes to pointers. But that's the whole point. Before I learned about them, I didn't know about them either. There's nothing this person has said that makes them completely beyond hope as a programmer or as an intelligent person. They may not learn C++ overnight (no, make that won't), but that doesn't mean they're so stupid they're not worth teaching, either. Shame on you guys. Now you know why you're not the one with friends, and able to ask this question. –  Cody Gray Jun 27 '11 at 14:30
2  
I think any Java programmer who's worth his salt would be familiar with the concept of pointers, I don't think anyone with at least a year of Java programming experience has never tried to call println on an object that doesn't override toString. –  Mahmoud Hossam Jun 27 '11 at 18:06

9 Answers 9

I think the way you explain this to a Java or VB.NET programmer (who has only used those languages) is to... not explain it to that programmer. Explain it as you would explain it to any regular person. Pointers are more powerful than any construct expressible in Java or VB.NET. Therefore, any attempt to understand pointers in terms of features available in the other languages is not going to be possible. Whatever abstraction someone might draw on top of those languages to express pointers will invariably fall apart when it comes time to do anything like pointer arithmetic.

If your programmer wants or needs to learn how pointers and C or C++ work, they have to learn the language(s). And they need to learn those languages as what they are, not as "Java/VB.NET, with a few of the semantics changed" -- because there's little code worse than Java written in C++. They can go on and on about how Java is part of the C family of languages, and therefore things should translate from one to the other simply. Unfortunately, that's not always the case. Spanish and French are part of the same family of languages too, but it's unreasonable to ask someone who knows only Spanish to comprehend a text written in French. The difference between these languages is a similar gulf. (A larger jump would be something like to a functional language (e.g. Haskell), which would be more like jumping from Spanish to Russian)

If he is unwilling or unable to understand pointers, then he's probably not all that good a Java or VB.NET programmer either, and maybe it's time for him or her to stop contributing to your project.

share
    
i am writing am engine from scratch and he would be using the engine but i what trying to explain the architecture of the engine so he could see how it worked and use it properly, difficult when he dosen't understand pointers –  Skeith Jun 27 '11 at 11:21
5  
@Skeith: That's true. But it's not something you're going to be able to force him to do. He is going to have to realize that C and C++ are not Java, and learn it like he would learn any new language. If he can't do that then he's not the right person for your project. –  Billy ONeal Jun 27 '11 at 11:23
2  
+1 "there's little code worse than Java written in C++." Quite true. Wish I could also upvote "therefore things should translate from one to the other simply." Your spoken language comparison is excellent. –  Michael K Jun 27 '11 at 13:21

I always find it kind of amusing that people from a Java context have problems understanding pointers, when in fact it boils down to a single simple statement:

A pointer in C or C++ is equivalent to a reference in Java

The difference being on some of the use cases. In particular, there is no garbage collector, so you will need to manage the memory held through the pointer (if is has been dynamically allocated), and you can perform simple arithmetic on pointers (arrays are commonly used as a pointer to the first element).

The rest of it is pretty much the same: a pointer refers to an object like a reference (refernce in italics used in the Java sense, not C++ reference) refers to an object, the pointer/reference can be used to access the object but they have an entity of themselves (whenever this seems problematic for a Java developer, just ask why objects are not compared in java with ==, and ask them to tell you why). A pointer/reference can be null (note, I am not talking C++ references here), or it can refer to an object...

share
8  
You would be amazed how many Java programmers don't know that Java has references. If you ask them why they don't compare objects with == they reply "...because i've read that you schould use .equals() for this". –  Jacek Prucia Jun 27 '11 at 12:47
1  
A pointer in C or C++ is not the same thing as a reference in Java. For one simple reason, Java passes references by value to methods. And by the way methods aren't the same thing as functions in C or C++ either. The semantics are there for a reason, to make fundamental distinctions between the concepts! –  Jarrod Roberson Jun 27 '11 at 14:46
1  
@Jarrod: you probably meant C++ reference. In C/C++, pointers are passed by values. Though I don't agree with David because the simple statement still does not mean too much to a person with no understanding about pointers. It is these use cases differentiate C++ pointers from Java references. –  Codism Jun 27 '11 at 15:49
7  
@Jarrod: Actually, you'd have to pass a pointer to the pointer to change it from inside the function. If you just change the blob of data where it points that's no different than manipulation of a reference type in Java where the reference is passed by value. –  Billy ONeal Jun 27 '11 at 16:26
1  
I do not agree that they are the same; but I also am dubious of anyone really understanding what a Java reference is without having understood pointers from C/C++. Speaking for myself, I had no deep understanding of "references" other than some leaky heuristics about what happened when I changed something passed as a method parameter. After learning C, I was enlightened. This is also what made it possible for me to write my own definitions for VB win32 calls. –  Jeremy Jun 27 '11 at 19:13

To truly understand pointers you must first understand computers. Everything program-related in a computer resides in some sort of memory. And every memory cell has a specific address, so that the program knows where to find its data. Without addresses, no software of any kind can exist.

CPUs only knows about three kinds of input: instructions, data and addresses. Everything in a computer is either an instruction, some sort of data, or an address to data. CPUs have designated address registers (aka index registers) through which it can access data or perform arithmetic. For these registers, CPUs have support in their instruction set to handle addresses. They can for example alter data by getting handed the address to it, or perform various operations using addresses.

The C/C++ language merely reflects this utterly fundamental cornerstone functionality of the computer, by having a pointer type, which will get directly translated by the compiler into address-handing CPU instructions. All compiled programming languages do this, though most languages don't have a pointer type, they merely hide away all address-related arithmetic from the programmer, for good and bad.

share

As a kid I actually started off doing visual basic... and all the knowledge I had about programming was that you draw and object and double click it and put some code on it basically to make it work.I didn't had the knowledge about the possibilities of other types of programming languages.

When I started learning about Java even coding in a single page where I can refer back to classes and objects looked uncomfortable for me. I really wasn't sure up first, until I had good couple of weeks into it and also at the end of my degree in CS.

With all due respect to what I used to be before, a developer who only has a diploma or some experience in programming has a far different and shallow understanding than someone with a degree.

everyone can write codes... but not everyone understands it.

share
    
that is a brilliant explanation of my question, the only problem is I wrote my question so I know what it says and don't need it paraphrased, how about and answer next time. –  Skeith Jun 27 '11 at 15:39
    
+1 I think I have pretty much the same background :). –  MAK Jun 27 '11 at 16:01
    
So do you feel you understand pointers (or even Java references)? If so, how did you manage to understand them? –  Kathy Van Stone Jun 27 '11 at 16:21
    
@Skeith a one word answer to yours, understanding. if you read and understood what i paraphrased you would have known. –  Khwaja Anas Nasarullah Jul 1 '11 at 12:51
    
I understood them after understanding how memory addresses works... what a memory address stores. And how that storage might contain another address rather than content... not complicated but... –  Khwaja Anas Nasarullah Jul 1 '11 at 12:54

The best thing to do is don't try to explain them.

You aren't going to get any value out of him doing C++. That isn't really a bad thing.

What you should focus on is embedding a scripting language into your C++ engine that he will be more comfortable with.

I highly suggest Lua. There is a great open source project game engine that uses Lua called Catmother, that offers some great insight into scripting game objects.

Believe it or not JavaScript is another language that is very easy to embed into C++ applications and would make for a productive choice as well.

And you can embed Python as well.

share
1  
I gave it some thought and have decided not to redesign a 3 and a half year project just so it can use lua. adding a scripting language was a bad idea when I started, a bad idea throughout the project and is still a bad idea now. –  Skeith Jun 27 '11 at 15:36
1  
@Skeith: you haven't seen a bad idea 'til you've seen a VB programmer writing C++ code... –  Shog9 Jun 27 '11 at 17:55
    
@Mr. CRT in a lot of ways Java is the new VB –  Jarrod Roberson Jun 27 '11 at 18:03
1  
Redesigning a 3 and a half year project may well be a bad idea. Using a higher-level scripting language for game logic in an RPG? You'll have to justify your assertion that that is a bad idea. –  Carson63000 Jun 28 '11 at 0:11
    
@Skeith - so I guess you know better than Blizzard? –  Jarrod Roberson Jun 28 '11 at 1:24

¡Hey, that's me! I'm a .NET developer and I started early in my career with Java.

If you're coming from .NET, just think byref and byval.

Just last month I started with Objective C to start programming on the iPhone. Your question made me realize I don't really know what pointers are, technically. So I read this article, which was REALLY helpful.

http://pw1.netcom.com/~tjensen/ptr/ch1x.htm

But even without that, the asterisk and pointer thing was kind of intuitive to me b/c in .NET you can pass variables by reference and by value. So you understand that some variables "point" or "reference" something in memory. This may not be "technically" the same thing, but I think it helps...

share
    
In C# you also have access to real pointers, which means you have the option of using them if you need them. –  Joe Internet Jun 27 '11 at 21:00

If he can help with the scripting, why not support a scripting language?

WoW is written in C++ but you can add custom functionality with the much easier LUA scripting language. Lots of other games use this system.

share
    
have no intention of having anything to do with a scripting language at all. There will never be a place for then in any project I am a part of! –  Skeith Jun 27 '11 at 15:45
2  
@Skeith - Why? That's sounds like a terrible requirement. –  Brendan Long Jun 27 '11 at 17:39
    
@Brendan why write code to access code that is designed to write code for me ? if i wanted this kind of development experience I would use an existing engine not build one from scratch. –  Skeith Jun 28 '11 at 8:27
1  
Adding a scripting language allows you to do some things a lot faster than in C++. GUI comes to mind. It's also often used to give third parties the ability to add functionality. I wrote some LUA addons for World of Warcraft for example. –  Carra Jun 28 '11 at 8:37
    
@Skeith - The point isn't necessarily that you need to use a scripting language; It's that adding support could make your engine usable by people who aren't you (like your friend). –  Brendan Long Jun 28 '11 at 15:51

I'll just respond to this part:

I tried explaining pointers to him but his response was, if it's just a variable that holds a memory address, why don't you just use an int?

It sounds like your friend doesn't understand the purpose of types. Everything in the computer can be represented as an integer. The differences between them are in meaning and size:

Meaning

  • A char is stored as a number (just like an int), but to us, it means something different. The fact that it's typed makes it easier for us to deal with it (things like function overloading -- how print(char) will be different than print(int), and just plain readability).
  • A pointer is also stored as a number, and in this case it is one, but it's not the same as an int. A char* is a pointer to a location in memory that has a char in it. An int* is a pointer to a location in memory that has an int in it. You could (in some cases) store the same data in an int, but you'd lose the type of the data that you're point at (since they're familiar with Java, ask why they use classes besides Object).
  • Pointers act differently than integers:
    • + and - will change a pointer by the size of its type. So if you have int* a;, then a + 1 will move to the next int in memory (if a were an int, it would increase by 4 or 8, depending on the machine).
    • The output format is usually different (pointers get printed out in hex)
    • Pointers can be dereferenced with * (as in int b = *a;)

Size

  • A char is generally smaller than an int.
  • A char* is not the same size as a char.
  • A char* (or void* or whatever) is not necessarily the same size as an int. Unlike other C++ types, I think the size of a pointer is just defined as "the size of a memory address in your machine's instruction set".

There are probably even more important differences between integers and pointers, but I think these are a fairly good overview of why they're not the same thing.

share
I tried explaining pointers to him but his response was, if it's just a variable that holds a memory address, why don't you just use an int?

The short and simple answer is that pointers have their own semantics which are not the same as integer semantics. Show him the results of this code:

#include <iostream>

int main(void)
{
    int x = 0, *p = NULL;

    std::cout << "Before increment: p = " 
              << (void *) p++ 
              << "; x = " 
              << x++ << std::endl;
    std::cout << " After increment: p = " 
              << (void *) p 
              << "; x = " 
              << x << std::endl;
    return 0;
}

Ask him why he thinks the result of p++ and x++ should be different.

Different but equally relevant answer: pointer values may not be representable in a plain int type depending on the platform, and conversions from pointer to int and back to pointer again may not be meaningful, as specified in the C Language Standard (§ 6.3.2.3):

5 An integer may be converted to any pointer type. Except as previously specified, the
result is implementation-defined, might not be correctly aligned, might not point to an
entity of the referenced type, and might be a trap representation.56)

6 Any pointer type may be converted to an integer type. Except as previously specified, the
result is implementation-defined. If the result cannot be represented in the integer type,
the behavior is undefined. The result need not be in the range of values of any integer
type.

Emphasis mine.

I don't have the C++ standard handy, but I think the language is pretty much the same.

share

Not the answer you're looking for? Browse other questions tagged or ask your own question.