Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does object have a pointer and not an int?

If we type

MyObject *obj = [[MyObject alloc] init];

"obj" is a pointer to the memory address.

...When we create an int, we type:

int x = 10; 

Why don't we type?

int *x = 10; 

The question is, why do we need a pointer to object and not int, float, etc...

like image 607
BlackMouse Avatar asked Mar 28 '13 08:03

BlackMouse


People also ask

Why do we need pointers to objects?

Sometimes you want to pass an object using a pointer (regardless of how it was allocated) because you want the function to which you're passing it to have access that that specific object (not a copy of it).

Is a pointer always an integer?

No, pointers are not integers. A pointer is an address.It is merely a positive number and not an integer.

Why do we use pointers to objects in C++?

A pointer is a type of variable that carries location information. In this case, the example variable will store the address of an Order object that we want to interact with. We initialize the pointer variable by using the C++ new operator to construct a new object of type Order.

Are objects just pointers?

In java and objective-c, a variable representing an object is generally a pointer to that object. However, it seems that in C++, it's common to have non-pointer types hold objects.


5 Answers

Efficiency.

Moving an int from one place to another is easy. Moving an object needs a little bit more work from the CPU. Moving the address of an object is as easy as moving an int.

In plain C, it is common to handle pointers to structs for the same reason. C makes it easy with the -> operator.

like image 72
mouviciel Avatar answered Oct 18 '22 02:10

mouviciel


There are languages where you can create objects “without a pointer”, on the stack. C++, for example. One great thing about having objects on the stack is that they get automatically deallocated when the scope ends, which helps with memory management. It’s also faster.

One bad thing about having objects on the stack is that they get automatically deallocated when the scope ends and the stack disappears. And since objects are usually longer-lived than local variables, you would have to copy the object’s memory somewhere. It’s entirely possible, but it complicates matters.

And it’s not just the memory lifecycle that’s complicated with stack-based objects. Consider assignment, foo = bar for two object types. If the objects are always pointers (Class*), you just assigned a pointer and got two pointers to the same objects; easy. If foo is stack-based (Class), the assignment semantics starts to get blurry – you could well end with a copy of the original object.

Introducing a rule that all objects are allocated on the heap (“with pointers”) is a great simplification. And as it happens, the speed difference doesn’t matter that much, and the compiler can now also automatically insert code to deallocate heap-based objects after they go out of scope, so it’s generally a win-win situation.

like image 33
zoul Avatar answered Oct 18 '22 03:10

zoul


You can have pointer for int, float as well.

Objects are created on heap. To access it, you need the address. Thats why they are of pointer types.

like image 27
Apurv Avatar answered Oct 18 '22 04:10

Apurv


Because that is the nature of an object.

Objective-C is directly derrived from C. That is why objects are referred to as pointers. An int-type variable of the size of an memory address is a pointer. In the end, an object in memory is not much different from an struct in memory.

However, when working in Objective-C it is advisable to think of these variables as references of objects rather than pointers to an object's memory areas. Think of them like Java does and do not spend much thoughts on how the system manages the references. There are far more important things to think of such as alloc/retain vs. release/autorelease or following the much easier ARC rules respectively.

BTW:

MyObject obj;

That would declare an object, not a pointer. It is not possible in Objective-C (afaik) and certainly not reasonable. But if it was reasonable and possible, that is what the syntax would look like.

int *x; 

That does create a pointer to an int. For using it you would have to allocate memory and assign its address to x. Rarerly reasonable in Objective C either but quite useful in standard C.

like image 30
Hermann Klecker Avatar answered Oct 18 '22 03:10

Hermann Klecker


its the difference between objects being on the stack or the heap. going int x = 10 x is now on the stack. int *x = 10 is just simply wrong (well most likely not what you want) since that is declaring a pointer to address 10 whatever that may be. you would want int *x = malloc(sizeOf(int)); as CodaFi suggested. that will allocate memory on the heap of the size of an int.

going MyObject *obj = [[Myobject alloc] init]; the compiler behind the scenes is allocating your object to the heap for you, but its basically the same principle

like image 31
Fonix Avatar answered Oct 18 '22 02:10

Fonix