The same concept applies for objects. In previous chapters we worked with our BankAccount class. When doing so we wrote statements similar to the following:. For example, we can call a method on our account1 object without using an asterisk:.
Instead, we are creating a copy of the pointer to the object. Consider, therefore, the following code:. In the above example, we will end up with two pointers account1 and account2 that point to the same location in memory. We have not, therefore, created a copy of account1. For details on copying objects refer to the chapter entitled Copying Objects in Objective-C. Jump to: navigation , search. Navigation menu Personal tools. Namespaces Page Discussion.
Views Read View source View history. This page was last modified on 27 October , at Copyright Payload Media, Inc. All Rights Reserved. The most important consideration here is memory management. Given the diagram above, consider what would happen if you wrote this line:. This is a rather innocent line, and it does assign the value of myFraction to anotherFrac. But remember that myFraction contains a memory address, not an actual Fraction object. This means that now anotherFrac points to the same object as myFraction, and, more importantly, nothing points to the original anotherFrac:.
Now, you have no way to get to the original anotherFrac, and so that object will reside forever in memory or until the application is quit or the system is turned off. Regardless of where you are, leaking is not a good thing. The other thing to consider is the case of multiple pointers to the same object, as you might have when you pass a pointer to an object as an argument to a method as you usually do.
Any changes that you make to one of the pointers will also be reflected in the other because they point to the same object in memory, after all.
I've heard some people say that using pointers in Objective-C is a matter or experience, i. Is this true? And is that the extent of using pointers in Objective-C. Basically, apart from when you want to explicitly pass reference variable to methods, what are the rules for pointers in Objective-C? You use a pointer always when referring to something on the heap and sometimes, but usually not when referring to something on the stack.
Since Objective-C objects are always allocated on the heap with the exception of Blocks, but that is orthogonal to this discussion , you always use pointers to Objective-C objects. Both the id and Class types are really pointers. Where you don't use pointers are for certain primitive types and simple structures. How about when you should use them? You will either make heavy use of scoped pointers or shared pointers. How many threads are running in your application?
If the answer is "potentially a lot", shared pointers can turn out to be a performance bottleneck if used everywhere. However, it won't always be the case - only testing will tell you for sure. There is an argument that I like against shared pointers - by using them, you are allowing programmers to ignore who owns a pointer. This can lead to tricky situations with circular references Java will detect these, but shared pointers cannot or general programmer laziness in a large code base.
There are two reasons to use scoped pointers. The first is for simple exception safety and cleanup operations - if you want to guarantee that an object is cleaned up no matter what in the face of exceptions, and you don't want to stack allocate that object, put it in a scoped pointer. If the operation is a success, you can feel free to transfer it over to a shared pointer, but in the meantime save the overhead with a scoped pointer.
The other case is when you want clear object ownership. Some teams prefer this, some do not. For instance, a data structure may return pointers to internal objects.
0コメント