2

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?

2 Answers 2

2
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.

Sign up to request clarification or add additional context in comments.

2 Comments

OP asked this in end : instance and the 'pointer' the same thing, created at the same time? Objc compiler is also RTL right ? So pointer will be created in end right ?
The instance and pointer are not the same thing, And I think there will be situations where the space for the pointer is allocated before the space for the instance and vice versa. Or in reality the space for the pointer won't be allocated at all in many cases, as it will just be stored in a register and not in memory. (As I understand it local variables will only be allocated space in memory if there more variables in use at one time than available registers or the address of a variable is needed)
1

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!

1 Comment

I can't vote up your answer yet, but thanks for this it has helped!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.