| CodeGuru Home | VC++ / MFC / C++ | .NET / C# | Visual Basic | Newsletters | VB Forums | Developer.com |
|
|||||||
| CodeGuru Individual FAQs The indivdual FAQs for CodeGuru. See the specific Topic FAQ forums for index pages and links to these Frequently Asked/Answered Questions. |
![]() |
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
||||
|
||||
|
C++ General: What are the different methods to pass parameters to functions?
Q: How are parameters passed to functions?
A: In C++ there are two ways: by value and by reference. Passing by value means that a copy of the object is made on the callee’s stack and altering the object means altering a local copy so the caller’s object is unchanged when the function returns. Passing by reference means that the address of the object is send (a reference holds an address but behaves like an object) so that the callee can directly alter the original object. Q: What about passing by pointer? A: There is no such thing; it is a misconception. A pointer is a memory address. You can pass a pointer to a function, but the pointer is passed by value. You can distinguish between the two this way: if the parameter has a trailing & then it is passed by reference, otherwise by value. Q: May I see some examples? A: Yes. I was about to show you some. In this first example an integer is passed by value to function foo(), which increments it. Because a copy is made the variable in main remains unchanged and the program’s output is 0 1 0. Code:
void foo(int i)
{
i++;
cout << i << endl;
}
int main()
{
int i = 0;
cout << i << endl;
foo(i);
cout << i << endl;
return 0;
}
In this second example an integer is passed to foo() by reference, which means that altering it will directly affect the object in main and the output is 0 1 1. Code:
void foo(int &i)
{
i++;
cout << i << endl;
}
int main()
{
int i = 0;
cout << i << endl;
foo(i);
cout << i << endl;
return 0;
}
Code:
void foo(int *i)
{
(*i)++;
cout << *i << endl;
}
int main()
{
int i = 0;
cout << i << endl;
foo(&i);
cout << i << endl;
return 0;
}
What if you want to pass a pointer to a buffer to a function and want the function to (re)allocate memory for it and fill it with some data? Simply putting it like this: Code:
// This code is WRONG
void foo(unsigned char *buffer)
{
// delete the previous buffer
if(buffer != NULL)
delete [] buffer;
// allocate new memory
buffer = new unsigned char[2];
// fill the memory
buffer[0] = 0;
buffer[1] = 1;
}
int main()
{
unsigned char * buffer = NULL;
foo(buffer);
delete [] buffer;
return 0;
}
Code:
// This is the corrected code
void foo(unsigned char* &buffer)
{
// delete the previous buffer
if(buffer != NULL)
delete [] buffer;
// allocate new memory
buffer = new unsigned char[2];
// fill the memory
buffer[0] = 0;
buffer[1] = 1;
}
int main()
{
unsigned char * buffer = NULL;
foo(buffer);
delete [] buffer;
return 0;
}
Last edited by cilu; May 14th, 2007 at 08:17 AM. |
![]() |
| Bookmarks |
|
||||||
| Thread Tools | Search this Thread |
| Display Modes | |
|
|