Hi :)
I have been programming in c++ for only a little while and im going through semi ok and enjoying it, but no matter how much i look through the net i cant understand why i need to use pointers.
I know how to use them and have made small programs using them to help me learn but i just cant see why i should bother with them.
If its not too much to ask i would love an example of where pointers are better and more effeicent to use then standard variables.
Primarily, pointers are used for dynamically allocated memory. The STL containers allow programmers to do a lot with dynamically allocated data these days without wong with pointers, but those containers still rely on pointers internally to get the job done.
Some libraries/APIs also pass back pointers to interfaces for the programmer to access as well, but this is more of an extension of the above usage.
'Standard' variables are also allocated on the stack and dynamically allocated objects are on the heap. Applications have more heap memory to work with than stack memory. So if you're working with large amounts of data, it has to be stored on the heap.
You can't really compare pointers to "standard variables" since they solve different problems.
First of all, accessing a value through a pointer is never faster or more efficient than through a "standard variable".
Pointers should be used under three circumstances:
1) What jRaskell said -- very large data should be allocated from the heap (thus you get a pointer to it) and not on the stack as a "standard variable" would be allocated;
2) You don't know at compile time the length of the array to allocate, so you have to dynamically allocate it (thus you get a pointer).
3) You need a variable to live longer than the scope in which it is declared. One solution to this is simply to copy the variable (return it from a function, for example), however this solution is not always the best or viable, as it may be very expensive to copy the object or the object may not be copyable at all.
You don't just have pointers to data, but code too.
The idea of a device driver implies calling functions that are not fixed, but determined at runtime. For example, printing to a postscript printer or a plotter actually calls different functions. This flexibility can only be achieved using pointers.
Additionally, polymorphism in C++ works by using a pointer to a base type that may in fact point to one of many derived types. Access to member functions are redirected thru a vtable, a table of pointers to functions, all of which require you to use a pointer to the base class.
3) You need a variable to live longer than the scope in which it is declared. One solution to this is simply to copy the variable (return it from a function, for example), however this solution is not always the best or viable, as it may be very expensive to copy the object or the object may not be copyable at all.
You could just use a static variable for that though :l
Another reason could be where you want a function to modify a variable (taking an example from K&R)
1 2 3 4 5 6
void swap (int* a, int* b) {
int* temp;
*temp = *a;
*a = *b;
*b = *temp;
}
You don't want temp to be a pointer though. By doing so, swap will not have the affect you want it to have.
Temp needs to be a separate memory location (ie just a local variable of type int).
If you only make the changes Zhuge mentioned, so the code looks like this:
1 2 3 4 5 6
void swap(int& a, int& b) {
int* temp;
temp = &a;
a = b;
b = *temp;
}
This is what's going to happen.
Line 3 will point temp to the same value reference by a.
Line 4 will set the value referenced by a = to the value referenced by b.
Line 5 will set the value referenced by b = to the value pointed to by temp.
But what is temp pointing to? It's pointing to the value referenced by a, which you just assigned to the value referenced by b. Meaning now a and b are both equal to the value originally in b.
the swap doesn't occur because you're only pointing to the value you want to temporary cache.
Awesome thanks everyone for the reply, i have been looking around the net for ages trying to understand why to use them and this answers it all. Thanks :D