views:

352

answers:

11

Possible Duplicates:
Why use pointers?
Passing a modifiable parameter to c++ function

Why would I want to have pointer parameters? The only reason I can see is for small functions to attempt to reduce confusion and a smaller memory footprint.

+2  A: 

There are many reasons to have pointers as parameters. A major reason is polymorphism. The pointer can point to a base class object or a subclass, and will call methods on that object accordingly.

I recommend Accelerated C++ by Koenig, he does a good job of explaining how polymorphism works in C++ via pointers (and references).

Matt Greer
Note that polymorphism works correctly with references as well.
James McNellis
Your first reason doesn't require pointers.
JSBangs
Yeah, I said "and references". C++ happens to have two ways of invoking polymorphism. Pointers being one of them.
Matt Greer
+3  A: 

Pointers are the only way to accept C-style strings and arrays. They're also the only way to share data between multiple threads (besides global objects, which... ew).

JSBangs
+4  A: 
  1. Compatibility with C code where references aren't available.
  2. Variable sized arrays.
  3. Option to have a missing parameter, i.e. NULL.
Mark Ransom
Variable sized arrays aren't in C++. (Or were you referring to dynamically allocated arrays?)
Billy ONeal
@Billy Oneal, It's variable to the function, not to the caller. I.e. `void Foo(int* array, int count); int first[10]; int second[20]; Foo(first, 10); Foo(second, 20);` You could declare instead `void Foo(int array[], int count);` but it would be exactly the same thing, just a pointer in disguise.
Mark Ransom
@Mark: Ah.. thanks for explaining. +1
Billy ONeal
+1  A: 

Other than the standard uses for a pointer parameter (that others have already stated), one reason that I can think of is to implement callbacks. You can have a function that accepts a function pointer as a parameter.

Vivin Paliath
+1  A: 

I assume you mean as opposed to references? The thing that you can do with pointers is set them to NULL, which allows you to specify an uninitialized state. This can sometimes be useful, but the corollary to that is that if you want to enforce that NULL cannot be passed to your function then using a reference makes that clear.

bshields
+2  A: 

Two reasons:

  1. A pointer on a 32-bit platform is only 4 bytes. If the data you're passing to the function is more than 4 bytes, you save some call time by passing the structure by reference instead of by value. (You may give up this performance advantage in indirection costs later, but that's where profiling comes in.)

  2. You can pass a non-const pointer to let the function modify the passed data.

Warren Young
Point 2 can also be accomplished with references. Point 1 is totally moot today - moving a few bytes is extremely fast, getting speed-ups is better done in more efficient algorithms and protocols, not shaving 10 bytes off a function call.
iconiK
Point 2 is a question of syntactic preference: some like to make it explicit that they're passing an address, not a value. Point 1 is not "moot". That would mean it doesn't matter how big your data structure is. Try passing a 10 MB linked list by value some time.
Warren Young
@iconiK point 1 can be accomplished with references, but it's definitely not moot because of the reason you stated. Objects are frequently much bigger than 10 bytes. Evaluating and passing the address of an object can be a huge speed up over copying the object, especially when it's a frequently called function.
Graphics Noob
@Graphics Noob, which is why pimpl and implicit sharing are excellent ways to speed up copying.
iconiK
+1  A: 

if you pass a simple variable to a function, the function creates a copy of that variable for use within the function's scope. When the function completes execution, any changes made to the passed simple variable won't be shown. This is because the variable itself was never altered within the function only a copy of it was. Passing a pointer to the variable to a function solves this problem.

int some_function(int num) {
   return num++;
}

int num1 = 15;

std::cout << some_function(num1) << std::endl; //prints 16

std::cout << num1 << std::endl; //prints 15
jordanstephens
Yes but I can just pass by reference instead. Why would I want to have pointers?
A: 

IMO, pointers are made out in a lot of C++ documentation to be far more important than they actually are, whereas references are rarely discussed in introductory material (other than brief mentions in terms of parameters).

While pointers certainly have value (arrays, string, dynamic memory, etc.), a lot of their functionality can be implemented more simply with references. The only time I really use pointer parameters regularly is when I am using a third-party library which has such a use built into it.

Using references removes the copy-assignment step implicit in straight class parameters while not requiring tiresome pointer dereferencing (using * or ->) over and over again. In addition, learning to use references is important for applications such as operator overloading, which is vital for precision control of your code.

tlayton
+5  A: 

In http://stackoverflow.com/questions/1322517/passing-a-modifiable-parameter-to-c-function/1322547#1322547 I answered when to use a reference instead of a pointer.

Conversely, prefer a pointer to a reference when any of the following are true:

  • It can be null
  • It can be changed (to point to something else)
  • It must be deleted

Some people also prefer a pointer when the thing that's being pointed at should be mutated, saying that the difference between a const reference and a non-const reference isn't obvious enough to the reader.

ChrisW
A: 

Plenty of reasons, but references should be the "standard". Use pointers only if a reference won't do. Some pros and cons:

  • Pointers can have NULL values, something that's extremely useful
  • Pointers can be of generic types and be reinterpreted as other types (though don't you dare use void *)
  • Pointers can be used to pass huge arrays without a huge memory footprint

  • In the case of generic types, compiler optimizations can operate better with references, because the compiler has more information on used types

  • References arguably make code more readable, more in-line with java and c# and thus better readability by other types of developers

  • References do not actually pass the entire object, but rather a pointer to it, however more type information is available to the compiler.

Stefan Valianu
A: 

There is one more difference between pointer and references: -pointer can be a NULL-pointer -reference references always an real object [f(T &v); f(NULL); isn't really good idea]

LonliLokli