Reputation: 626
The "Objective-C for Java Programmers, Part 1" intro by David Chisnall states that
Unlike objects, which are always passed by reference, structures are commonly passed by value.
I am very new to Objective-C (coming from C++) and so I am having trouble understanding this. In C++, I could pass a structure either by a pointer or by reference, but never by value (probably because it is very inefficient).
How does Objective-C accomplish this? Does it really push all structure members, one by one, in a stack-like manner?
What happens if the structure is made of large/complex objects?
Upvotes: 3
Views: 903
Reputation: 1176
It copies all the structure members to the stack. Remember, that complex objects in the structure are stored by pointers to it, so when you copy structure, you have copy and copies of points to complex types. Of course copy of pointer points to the same place in memory as original, so you perform actions on original objects then.
This approach when you have copies of primitive types and original complex objects leads to unmaintainable mess, even when you know what you are doing at the moment.
Upvotes: 2
Reputation: 9796
Yes it makes a copy of the whole structure and this one is passed to the calling function, and any modification made won't affect the original one. And the disadvantage is of course because it is slow, but also consumes twice space.
Upvotes: 5