Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the biggest advantage of using pointers in ObjectiveC

I realize 99% of you think "what the h***…" But please help me to get my head around the this concept of using pointers. I'm sure my specific question would help lots of newbies.

I understand what pointers ARE and that they are a reference to an adress in memory and that by using the (*) operator you can get the value in that address.

Let's say:

int counter = 10;
int *somePointer = &counter;

Now I have the address in memory of counter, and I can indirectly point to its value by doing this:

int x = *somePointer;

Which makes x = 10, right?

But this is the most basic example, and for this case I could use int x = counter; and get that value, so please explain why pointers really are such an important thing in Objective-C and some other languages... in what case would only a pointer make sense?

Appreciate it.

like image 909
NickCave Avatar asked Feb 20 '13 23:02

NickCave


1 Answers

Objective-C has pointers because it is an evolution of C, which used pointers extensively. The advantage of a pointer in an object-oriented language like Objective-C is that after you create an object, you can pass around a pointer to the object instead of passing around the object itself. In other words, if you have some object that takes up a large amount of storage space, passing around a pointer is a lot more memory-efficient than passing around a copy of the object itself. This may not be noticeable in simple cases when you’re only dealing with primitive types like ints, but when you start dealing with more complex objects the memory and time savings are enormous.

More importantly, pointers make it much easier for different parts of your code to talk to each other. If variables could only be passed to functions “by value” instead of “by reference” (which is what happens when you use pointers), then functions could never alter their inputs. They could only change the state of your program by either returning a value or by changing a global variable—the overuse of which generally leads to sloppy, unorganized code.

Here’s a concrete example. Suppose you have an Objective-C method that will parse a JSON string and return an NSDictionary:

+ (NSDictionary *)parseJsonString:(NSString *)json
                            error:(NSError **)error;

The method will do the parsing and return an NSDictionary if everything goes okay. But what if there’s some problem with the input string? We want a way to indicate to the user (or at least to the programmer) what happened, so we have a pointer to a pointer to an NSError, which will contain that information. If our method fails (probably returning nil), we can dereference the error parameter to see what went wrong. What we’ve effectively done is to give our method two different kinds of return values: usually, it will return an NSDictionary, but it could also return an NSError.

If you want to read more about this, you may have better luck searching for “pointers in C” rather than “pointers in Objective-C”; pointers are of course used extensively in Objective-C, but all of the underlying machinery is identical to that of C itself.

like image 128
bdesham Avatar answered Oct 31 '22 02:10

bdesham