reference or pass by value

closed account (j3bk4iN6)
I read this www.cplusplus.com/doc/tutorial/pointers.html
my question is which to use for a function, pointer, reference, or pass by value. My understanding is that if you wanna change a variable that is out of scope then you would use a pointer in a function.
Passing by value, by pointer-to-const, and by const-reference all prevent the caller's actual
parameter from being changed by the function. Passing by pointer (to non-const) and by
(non-const) reference allows the function to change the caller's actual parameter.

Passing by value makes a copy of the caller's actual parameter onto the stack.
References are typically implemented in the compiler as pointers, so passing by reference
and by pointer both store a pointer onto the stack, which is very cheap.

From a performance perspective, pointers and references are equivalent. However, pointers
can be NULL whereas references cannot. A well-implemented function that takes a pointer as
parameter must check the pointer for NULL before dereferencing, and then deal with the
possibility of NULL (often times, but not always, an error case). A well-implemented function
that takes a reference as parameter need not check the reference for NULL since it cannot be,
so the error case is avoided. But accessing the data "referred to" by a pointer or a reference
requires one extra memory access than if passed by value.

From an implementation perspective, accessing the data "referred to" by the pointer requires
the dereference operator (*). Accessin the data "referred to" by a reference is the same as
if the parameter were passed by value. Examples, to illustrate:

1
2
3
int by_value( int x ) { return x + 5; }
int by_pointer( const int* x )  { return x == NULL ? 0 : *x + 5; }  // Returns 0 if x == NULL or *x == -5...
int by_reference( const int& x )  { return x + 5; }  // Same as by_value case! 


In general, pass by value for easy-to-copy types and you don't need to change the caller's
actual parameter. If you don't need to change the caller's actual parameter but it is expensive
to copy onto the stack, use const references. If you do need to change the caller's actual
parameter, use a non-const reference. You should use pointers only when NULL is not an
error case for your function.
omg, Thanks J, finally that just clicked for me... Makes sense now :D I couldn't figure out when to use which and more importantly what the difference was. That was an extremely well written and short explanation, should be in Articles.
closed account (j3bk4iN6)
So this use of memory all relates to the 1960's when computers were 32kb and memory allocation was more stringent, because I notice alot of things in C you have to deal with memory allocation, where newer languages like python allocate dynamically.
I think even in today's world with quad core 64bit technology computers even the finer details all add up in large scale programs, and C++ was created for that purpose.
closed account (j3bk4iN6)
python uses a very sophisticated garbage collection, and leaving memory management up to the user can lead to potential hacks. high level programmers do make mistakes, like when iphone got hacked.
Last edited on
So does java, but try writing such a large scale program in python or java... memory/CPU overload...
There's places for all different languages it's up to the programmer/project manager to figure out the best use of language for the project.
Last edited on
closed account (j3bk4iN6)
I was trying to explain that to python diehards
yea they seem to come hand in hand with linux phreaks, however most are smart enough to realize knowing a large range of languages is more beneficial.
closed account (j3bk4iN6)
In your opinion is visual studio proffesional edition a better tool for making an intranet gui for excell, because you can do it with python.
Topic archived. No new replies allowed.