2

I understand the basic logic of using pointers (returns location), and you can play around with it via int a = 10; int* b = &a; int* c = b; and have a location point to a reference of a location, thus pointing to a value like above in code.

Question: When is it appropriate to use them?

*Is it appropriate only when managing memory? *Is it appropriate only when you have a huge solution file? *Is there a social norm among C++ programmers when to use it? (I'm sorry if this sounds weird but in our readings the author seems to use them randomly).

I'm studying game programming and we're covering this now. I've taken pointers and references out of the code, and still wound up with the same values and results, so I'm just confused on why this is a useful thing.

  • 1
    Generally, references are preferred over pointers unless you really can't avoid them (such as implementing standard library containers which require dynamic allocation of objects). Another example is Google's coding convention, where output arguments are passed to functions by pointer instead of non-const references, so as to make the fact that they are modified obvious at first glance. –  Nov 02 '13 at 21:32
  • http://stackoverflow.com/questions/162941/why-use-pointers – codedude Nov 02 '13 at 21:35
  • Pointers are a big source of errors. References are implicit pointers designed to avoid doing the same errors. You are free to use anyone. – Pierre Emmanuel Lallemant Nov 02 '13 at 21:36

2 Answers2

2

C++ supports pointers mostly to stay compatible with C and to let you continue to do low-level memory stuff, making your own data structures, and so forth.
The language does support references, basically an abstraction away from pointers, which basically means you name variables like

int a;
int& b = a; //alias for a

Passing by reference or const reference is a common way in C++ to save execution time. If you have a super huge mega-class whose member variables take up a huge amount of space, and then you pass it by value to a function, the program has to copy all that data onto the stack to execute the function, which is undesirable in almost all cases. Passing by reference does not copy anything, so it is much cleaner and faster.

Zach Stark
  • 565
  • 3
  • 8
  • In most day-to-day programming with C++, you don't need to use pointers very often. You would use a `vector x;` for example, instead of `int *xp = new int[50];`. It's safer and easier, and you can pass it by reference if needed. – Aaron McDaid Nov 02 '13 at 22:02
0

There are many different cases when you require pointers and references, so I will try to list a few of them:
Memory - if you are using huge objects or other types which require a lot of space you don't want to copy them into a function, instead you use a reference, essentially passing the address of your object.
Speed - same as above, passing a reference is simply faster than copying it
Multiple return values - you can pass variables in via reference and write your results directly into them, allowing you to have more than one return value (and returning things like arrays)
Dynamically allocating memory - if you create new objects during runtime (as opposed to having everything set at compile time), you need to use pointers

UnholySheep
  • 3,967
  • 4
  • 19
  • 24