functions memory management C++
c++, function, memory-management
Solution
C++, like C, is a call-by-value language, so in general copies of parameters are always made.
When:
void f( int x ) {
}
is called, a copy of its parameter is made and passed to the function. When:
void f( int * x ) {
}
is called, a copy of the pointer is made and passed to the function.
The exception to this is when references are used:
void f( int & x ) {
}
no copy is made, but internally a pointer is (probably) used to pass the address of the parameter - you are not supposed to think about this however.
Exactly the same thing applies to return values:
int f() {
return 1;
}
a copy of the value 1 is made and returned to the caller. If the function returned a pointer, a copy of the pointer would be made. Once again, references are the exception, in that no copy is made, but internally a pointer is (probably) used to return the value.
Problem
i have a little bit lame question, but it's time i have this finally clear. consider regular function with some parameters and a return type. My questions are: are there always made some copies of parameters? i mean even if the function expects reference or pointer as parameter, there are actually new references/pointers created, right? when the function is over are there some destructors called for those? is it the same with return values? is the returned value also copied from the context of the actually performed function? or are those just addresses somewhere and the value in the context is destructed too? i probably didn't express it too clearly sooo.. if you just explained in your way how does it work with memory when some function is called i would be thankful. I have just casual idea about function of processors, but i have already dealt with assembler so there is at least something to work with.