What is the biggest advantage of using pointers in ObjectiveC

I understand that 99% of you think "well *** ..." But please help me understand this concept of using pointers . I am sure my specific question will help many newbies.

I understand what ARE pointers are and that they are a reference to an address in memory and that with the help of the operator (*) you can get the value in this address.

Say:

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

Now I have an address in the memory of counter , and I can indirectly specify its value by doing the following:

 int x = *somePointer; 

What does x = 10 , right?

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

Appreciate it.

+7
source share
4 answers

Objective-C has pointers because it is an evolution of C that has made extensive use of pointers. The advantage of a pointer in an object-oriented language, such as Objective-C, is that after you create an object, you can pass a pointer to the object instead of walking around the object itself. In other words, if you have some kind of object that takes up a large amount of storage space, then transferring the pointer is much more efficient than copying the object itself. This may not be noticeable in simple cases, when you deal only with primitive types, such as int s, but when you start working with more complex objects, memory and time saving are huge.

More importantly, pointers make it easy for different parts of your code to communicate with each other. If variables can only be passed to functions "by value" instead of "by reference" (which happens when you use pointers), then functions can never change their inputs. They could only change the state of your program, either by returning a value, or by changing a global variable, the redundancy of which usually leads to sloppy, disorganized codes.

Here is 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 parse and return an NSDictionary if all goes well. But what if a problem occurs with the input string? We need a way to tell the user (or at least the programmer) what happened, so we have a pointer to a pointer to an NSError that will contain this information. If our method fails (possibly returning nil ), we can dereference the error parameter to see what went wrong. In fact, we do to give our method two different types of return values: it usually returns an NSDictionary , but it can also return an NSError .

If you want to know more about this, you may need to find "pointers to C" rather than "pointers to Objective-C"; pointers, of course, are widely used in Objective-C, but all the underlying mechanisms are identical to those of C.

+5
source

What is the biggest advantage of using pointers in ObjectiveC

I would say that the biggest advantage is that you can use Objective-C in general - all Objective-C objects are pointers accessible with pointers (the compiler and runtime will not allow you to create objects statically), so you won’t get them without them ...

+2
source

Item:

What if I tell you to write me a program that will support a set of counters, but the number of counters will be entered by the user when the program starts. We encode this using an array of integers allocated on the heap.

 int *counters = malloc(numOfCounters * sizeof(int)); 

Malloc works with memory directly, so by its nature it returns a pointer. All Objective-C objects are allocated using the malloc heap, so they are always pointers.

Item:

What if I told you to write me a function that read the file and then run another function when it was done. However, this other function was unknown and will be added by other people, people whom I did not even know.

For this, we have a “callback”. You should write a function that looks like this:

 int ReadAndCallBack(FILE *fileToRead, int numBytes, int whence, void(*callback)(char *)); 

This last argument is a function pointer. When someone calls the function you wrote, they do something like this:

 void MyDataFunction(char *dataToProcess); ReadAndCallBack(myFile, 1024, 0, MyDataFunction); 

Item:

Passing a pointer as an argument to a function is the most common way to return multiple values ​​from a function. In Carbon OSX libraries, almost all library functions return an error status, which creates a problem if the library function should return something useful to the programmer. This way you pass the address where you want the function to pass information to you ...

 int size = 0; int error = GetFileSize(afilePath,&size); 

If the function call returns an error, it is in error , if there was no error , error will probably be zero, and size will contain what we need.

+1
source

The biggest advantage of pointers in Objective-C or in any dynamic allocation language is that your program can handle more elements than the names you invent in your source code.

-one
source

All Articles