Oh boy, there is a lot of wind being blown in this topic.
To simplify things, everything sent to a function gets copied (or generates an object) if--
-An implicit cast is done (sending an int to a function that takes an Integer object for example (and for further clarity, the Integer object has a constructor that isn't marked explicit and takes an int)) (non-pointer)
-You send a value of a built-in type to a function (non-pointer)
-You send a value of an object to a function (non-pointer)
-*You send a pointer to a function
* yes, a copy of the actual pointer is sent to the function, not the pointer itself.
To elaborate on the pointer issue, think about what a pointer's purpose is first. A pointer simply points to an address (with the restriction of the type of address the pointer can point to).
When you pass a pointer to a function, you are passing a copy of the pointer (so you're sending a copy-pointer to the function. It still references the same address though).
Alterations to the elements of the pointer will obviously effect the original version because the original version and the copy pointer point to the same address, so the de-referenced objects of both are the same.
However, if you change what the pointer is pointing to in the function, it changes what the copy is pointing to and not the initial pointer.
For example, if you were to assign NULL to a local parameter pointer in a function, it won't make the initial pointer point to 0 also, since the local parameter pointer is just a mere copy.
However, if your parameter is a reference to a pointer, you're sending the actual (NOT a copy, but the ACTUAL) pointer to the function and the assignment of NULL to that pointer will effect the original version since no copy was generated.
There are probably exceptions, but I wanted to point the pointer issue out specifically since it seemed that people weren't addressing it clearly. This caused some confusion for me when I started learning C++ until I found a post identical to what I just mentioned here.
If corrections are needed please let me know. This is what I understand about pointers sent to functions.
Note: You don't have this issue with regular references since a references denotes that you are passing the actual object as the parameter. Furthermore you cannot treat a regular reference like a pointer, but more-so like a constant pointer.
Example--
#include <iostream>
void alterValue(int*, int);
void falseAlterPointer(int*, int, const std::size_t);
void alterPointer(int*&, int, const std::size_t);
void printInfo(int*);
/**
* Sending a copy of a pointer to this method and incrementing
* a value derefenced by the copy by arg
*/
void alterValue(int *val, int arg){
(*val) += arg; // altering value
}
/**
* Attempts to alter the pointer, but fails since only the copy is altered.
*/
void falseAlterPointer(int *val, int newVal, const std::size_t size){
val = new int[size];
*val = newVal;
printInfo(val);
std::cout << "Warning! Copy pointer was changed, not original" << std::endl;
}
/**
* This time, altering the actual pointer itself. No copy is sent to this method.
*/
void alterPointer(int *&val, int newVal, const std::size_t size){
val = new int[size];
*val = newVal;
std::cout << "Original pointer was changed =)" << std::endl;
}
/**
* Convenience method to reduce code repetition
*/
void printInfo(int *arg){
std::cout << *arg << ", " << arg << std::endl;
}
int main(){
int *tPtr = new int[1];
*tPtr = 10;
printInfo(tPtr); // displaying value and current address pointer points to
alterValue(tPtr, 5); // incrementing the value by 5
printInfo(tPtr); // displaying value and current address pointer points to
falseAlterPointer(tPtr, 20, 3); // attempts to alter the pointer -- we'll see that it doesn't
printInfo(tPtr); // proof here
alterPointer(tPtr, 20, 3); // altering the actual pointer
printInfo(tPtr); // result
return 0;
}