Why is everything pointer in Objective-C

I come from the background of PHP / Javascript, where things are stored in a variable directly in most cases, where we also have Object / Classes / Methods, etc. It was an OOP.

Now I'm starting to learn Objective-C. I understand the basics of pointers. But now everything is a pointer. This is the part that I do not get. Why don't we like PHP / Javascript with direct assignment? We are still doing OOP.

thanks

+4
source share
6 answers

If you look at the semantics of JavaScript and many other OO languages โ€‹โ€‹(possibly including PHP, but I'm not sure and don't want to guess), you will see that these languages โ€‹โ€‹offer the same indirect Objective-C target with pointers, In fact, internally these languages โ€‹โ€‹use pointers everywhere. Consider this snippet (JavaScript):

function f(obj) { obj.x = 1; // modifies the object referred to directly obj = {x: 2}; // doesn't affect caller } var foo = {x: 0}; f(foo); // passes a pointer/"reference" // foo.x === 1 

This is roughly equivalent (C, since I don't know Objective-C), something like this, modulo manual memory management, static typing, etc.:

 struct Obj { int x; }; void f(struct Obj *obj) { obj->x = 1; obj = ...; // you get the idea } struct Obj *foo = malloc(sizeof(*foo)); foo->x = 0; f(foo); free(foo); 

It is just explicit in Objective-C, because this language is a superset of C (100% backward compatibility and interoperability), while other languages โ€‹โ€‹do away with explicit pointers and make an indirect relation implicit to them.

+7
source

In PHP, you also only work with pointers, but transparently. Indeed, you use object references

+2
source

The reason that Objective-C designers decided to use pointers on the entire Objective-C object includes the following:

  • In this way, they can handle memory management behind the scenes without interrupting the ability of programmers to do this on their own.
  • Fast listing of objects.
  • (Perhaps the most important). It makes it possible to have types of identifiers that can pass nil (null) values โ€‹โ€‹without a program crash.
+2
source

To build on other answers here: in PHP and other languages, you still use pointers. That's why there is still a difference between passing by reference and passing by value. There are several good sites that help explain the difference , both in syntax and in what it means to pass any of these methods.


Edit: Refer to the second link in my post. My interpretation of this information is that PHP is the default by default. Adding an ampersand before a variable during a function call passes a link (or rather, the address of the variable). In fact, passing by reference is a passing of a pointer, and when passing by value, a copy of the value is completely. They also have different consequences for their use (the link allows you to change the initial value of a variable, even outside the scope of the function, etc.).

+1
source

The purpose of C is a strict superset and extension of ANSI C, so native types that can be compatible with the language have been limited (perhaps by the original implementation). But this ANSI C compatibility has proven to be one of the benefits of using Objective-C mixed with reuse of cross-platform C code.

BTW, OOP and "security" are almost orthogonal concepts. Each of them has different potential costs in terms of consumption of processor cycles and / or energy consumption of the userโ€™s battery.

+1
source

Objects are created using the +alloc method, which allocates space for a new object on the heap. In C, and therefore in Objective-C, the only way to refer to memory on the heap is through a pointer.

0
source

Source: https://habr.com/ru/post/1384409/


All Articles