简体   繁体   中英

Objective-C: object instance pointers

I'm new to Objective-C and trying to get my head around pointers. This isn't my first port of call; I'm hoping somebody will be able to discern the source of my confusion and explain this concept in plain English.

In the following code (from a Treehouse lesson) an instance, ball, of an object class, Sphere, is created.

#import <Cocoa/Cocoa.h>
#import "Sphere.h"

int main()
{
Sphere *ball = [[Sphere alloc] init];

[ball setRadius:25];

NSLog(@"ball radius %f", [ball radius]);

return 0;
}

I understand that in Objective-C, all instances of an object class are pointers.

I understand that 'the ball pointer points to the memory that is allocated for the ball object', as someone else noted for me elsewhere.

But what's happening in terms of actual memory allocation?

Are the 'instance' and the 'pointer' the same thing, created at the same time?

Sphere *ball

This allocates some memory big enough to fit a pointer. (on the stack)

[Sphere alloc]

This allocates some memory elsewhere (in the heap) big enough to fit an instance of the class Sphere, and sets all the instance variables to 0. It returns the address of the space allocated for the object (In other words a pointer to the new object).

[[Sphere alloc] init]

The init method then initialises all the fields and other initialising. (Assuming you've overriding init in Sphere, the implementation of init in NSObject does nothing). This has nothing to do with allocating space in memory for the object itself, though it often allocates space for other objects referenced by the object. It returns the memory address of the object, most of the time this is the same address that was returned by alloc.

The memory address returned by [[Sphere alloc] init] is then stored in the ball variable. (by the = )

A pointer is just a number, a memory address that corresponds to a memory location. An object is an instance of a class, and the object refers to the actual memory allocated for the object.

instance and pointers are not 100% identical, although in the code they are both represented by your ball variable.

The answer to your question can be found in the line:

Sphere *ball = [[Sphere alloc] init];

Let's take a closer look, this line contains two method calls: alloc and init .

alloc is a class method implemented in NSObject , it is described as follows in the documentation : Returns a new instance of the receiving class.

This means that at this point, the operating system is going to allocate memory for an object of type Sphere .

init is the initializer and described in the docs as follows: Implemented by subclasses to initialize a new object (the receiver) immediately after memory for it has been allocated. You can override this method in your implementation of the Sphere class to perform your own acxtions (typically you would set values for the class's properties here).

After this line of code has been executed, you have 1. allocated the physical memory for the instance of class Sphere and 2. initialized the same instance. This means that this instance (or object , these terms are equivalent) is now ready for your use.

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM