| | |
Pass By Value, and what that really means
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: May 2006
Posts: 45
Reputation:
Solved Threads: 0
It has been a long time since I have used C++ (I am only 20, so maybe not that long) I have been using several languages in the last couple years(Java, ActionScript2 and 3, PHP), polluting my mind with many different idioms, and now need to hop back to C++.
Many questions have been coming to mind while I am trying to get back into the groove, and one thing that is causing some confusion is that functions pass arguments by value. This makes perfect sense for primitives, but I am getting lost in the semantics of pointers and references when it comes to structs and objects.
Now, arrays and pointers can be used "interchangeably", because an array IS a pointer to its first element (correct me if I am wrong). Is the same true of structs and objects?
So, if I am understanding correctly, when myFunc2 is invoked, it creates a Date reference and then assigns dt to the new reference(Constructor not called). Also the same for myFunc3, only we are using a pointer instead of a reference.
My confusion is in myFunc1. What is happening? If you pass by value, exactly what value does Date dt contain (a pointer?)? Is Date d a local copy of Date dt?
I am horribly comfortable with pass by reference. And for all intents and purposes, it seems that when you pass pointers and references, you are in essence passing by reference, although this reference is implicit, and is not a feature of the C++ framework (meaning it must be carefully managed, whereas in Java, there is no chance the object you reference will be unallocated or magically changed to another object)
If you can provide any insight into how structures and classes work internally, and how one goes about passing copies, references, and pointers to them.
Thanks for you time
Many questions have been coming to mind while I am trying to get back into the groove, and one thing that is causing some confusion is that functions pass arguments by value. This makes perfect sense for primitives, but I am getting lost in the semantics of pointers and references when it comes to structs and objects.
Now, arrays and pointers can be used "interchangeably", because an array IS a pointer to its first element (correct me if I am wrong). Is the same true of structs and objects?
C++ Syntax (Toggle Plain Text)
Date dt = new Date(); myFunc1(dt); myFunc2(dt); myFunc3(&dt); void myFunc1(Date d) {} void myFunc2(Date & d) {} void myFunc3(Date * d) {}
So, if I am understanding correctly, when myFunc2 is invoked, it creates a Date reference and then assigns dt to the new reference(Constructor not called). Also the same for myFunc3, only we are using a pointer instead of a reference.
My confusion is in myFunc1. What is happening? If you pass by value, exactly what value does Date dt contain (a pointer?)? Is Date d a local copy of Date dt?
I am horribly comfortable with pass by reference. And for all intents and purposes, it seems that when you pass pointers and references, you are in essence passing by reference, although this reference is implicit, and is not a feature of the C++ framework (meaning it must be carefully managed, whereas in Java, there is no chance the object you reference will be unallocated or magically changed to another object)
If you can provide any insight into how structures and classes work internally, and how one goes about passing copies, references, and pointers to them.
Thanks for you time
>>My confusion is in myFunc1. What is happening? If you pass by value, exactly what value
>> does Date dt contain (a pointer?)? Is Date d a local copy of Date dt?
Yes. structures (classes) can be passed by value just like POD (plain-old-data) types. The ocmpiler makes a copy of the structure and puts that into the parameter of the called function. Whatever changes the called function makes to the structure or class are NOT reflected in the copy of the calling function.
>> does Date dt contain (a pointer?)? Is Date d a local copy of Date dt?
Yes. structures (classes) can be passed by value just like POD (plain-old-data) types. The ocmpiler makes a copy of the structure and puts that into the parameter of the called function. Whatever changes the called function makes to the structure or class are NOT reflected in the copy of the calling function.
Last edited by Ancient Dragon; Apr 16th, 2008 at 9:46 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: May 2006
Posts: 45
Reputation:
Solved Threads: 0
Thanks for your reply. I have another question regarding the specifics of how that process works.
Does Date dt pass a pointer (or reference) and then myFunc1 dereferences and copies through some internal copy function, or is the entire structure pushed onto the stack, and then myFunc1 pops the data off the stack and places it into its own Date d.
I am curious as to how the compiler handles structures. Are there existing mechanisms invisible to the programmer that do the work for you, and if not, how does the compiler go about maintaining the integrity of structures.
Does Date dt pass a pointer (or reference) and then myFunc1 dereferences and copies through some internal copy function, or is the entire structure pushed onto the stack, and then myFunc1 pops the data off the stack and places it into its own Date d.
I am curious as to how the compiler handles structures. Are there existing mechanisms invisible to the programmer that do the work for you, and if not, how does the compiler go about maintaining the integrity of structures.
Last edited by petzoldt01; Apr 16th, 2008 at 10:07 pm.
•
•
Join Date: May 2006
Posts: 45
Reputation:
Solved Threads: 0
Thanks again.
I was getting confused because I wasnt exactly sure what the value of a structure is. Is the value of a structure the structure itself, or is there some invisible level of abstraction that makes it work out that way. (coming from Java has been making this difficult to grasp)
I was getting confused because I wasnt exactly sure what the value of a structure is. Is the value of a structure the structure itself, or is there some invisible level of abstraction that makes it work out that way. (coming from Java has been making this difficult to grasp)
Last edited by petzoldt01; Apr 16th, 2008 at 10:13 pm.
The compiler pushes the value of each variable contained in the structure onto the stack as if they were individual integers (or whatever POD they happen to be). But C or C++ programmers don't really have to be concerned about that (at first) because inside function1() it is treated as a structure, not as individual elements of the structure.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
![]() |
Similar Threads
- Conditionally initializing arrays HELP (C++)
- how to pass a form to another form (Visual Basic 4 / 5 / 6)
- Pass parameters between Javascript and ASP (JavaScript / DHTML / AJAX)
- need help understandin how to pass by reference. (Java)
- IPSec Pass-thru (Networking Hardware Configuration)
Other Threads in the C++ Forum
- Previous Thread: Add descriptor to array
- Next Thread: The Visual Studio Common IDE Package
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






