-2

To complete the object, these step are required.

1)

Class *myClass = [Class alloc]; 
// This will allocate the memory required by every variable and the class
// and also return the class instance. The documentation says memory for
// all instance variable is set to 0.

2)

myClass = [myClass init];
// complete initialization
// From the documentation, the init method defined in the NSObject class
// does no initialization, it simply returns self.

The question:


I write a class

@interface Fraction : NSObject
    - (void)print;
    - (void)setNumerator:(int)n;
    - (void)setDenominator:(int)d;
@end

@implementation Fraction
{
    int numerator;
    int denominator;
}

- (void)setNumerator:(int)n {
    numerator = n;
}

- (void)setDenominator:(int)d {
    denominator = d;
}

- (void)print {
    NSLog(@"%i/%i", numerator, denominator);
}

And in the main.m

Fraction *fraction = [Fraction alloc]; // without the initialization
fraction.denominator = 3;
[fraction print];

This is what I get on the console

0/3

1) The value of numerator is 0. I didn't set the numerator and it print 0. This is strange, because integer default value should be undefined. How can this happen?

2) With alloc, the memory for all instance variable is set to 0. But, how can I set denominator to 3 and it still works. To store value to the denominator (int) variable we need 4 bytes memory allocated.

3) The last question, when I do

- (id)init {
    self = [super init];
    if (self) {
        // Initialize self.
    }
    return self;
}

The init method was inherited from parent class, it sends init message to super class that is NSObject. And does no initialization, it simply return self (NSObject). Because Fraction inherited from NSObject,

Why I should not do this?

- (id)init {
    self = [self init]; // Inherited class can use parent method (init)
    if (self) {
        // Initialize self.
    }
    return self;
}

What does init method actually do?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Edward Anthony
  • 3,354
  • 3
  • 25
  • 40
  • Objective-C is fundamentally no different from most OO languages such as Java. In Java when you say `new MyObj(x,y,z)` the JVM first invokes an `alloc` function to allocate the storage and set the object type, and then invokes the constructor (named `MyObj(whatever)`, rather than `init`) to fill in values of the object. But, unseen by you, the constructor for your Java superclass (eg, Object) is invoked first, at the start of your constructor). Objective-C just makes this sequence more obvious. – Hot Licks Jun 01 '14 at 11:27

1 Answers1

2

The answer is actually pretty straightforward:

self = [self init];

is an infinite recursion that causes stack overflow:

Stack overflow

In general, though, one should avoid using self for accessing methods of partially constructed or partially destructed objects.

Community
  • 1
  • 1
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • `self = [[[self.self.self init] init].self.self init].self` – gaussblurinc Jun 01 '14 at 09:57
  • I don't get it. When init message was sent to the Fraction instance. It will looking for init method on the parent class. And it assign NSObject class to self. I tried it and the program works. Where does the infinite recursion occur? – Edward Anthony Jun 01 '14 at 10:06
  • Thanks for image and your explanation. I understand, can you help for question number 1 and 2 ? – Edward Anthony Jun 01 '14 at 10:08
  • @EdwardAnthony The #1 [is answered here](http://stackoverflow.com/q/1786781/335858) - the bits of all instance variables are zero-initialized. The #2 is straightforward, too - the memory for `denominator` is allocated in the `alloc`, and it has four bytes already, because it is an `int`. Then your code writes `3` into it, hence the printout that you get is correct. – Sergey Kalinichenko Jun 01 '14 at 10:13
  • @dasblinkenlight thank you :) it's clear now. I don't understand why those guys say undefined. But it pretty clear, it set to zero by default. http://stackoverflow.com/questions/5980749/does-an-int-in-objective-c-have-a-default-value-of-1 – Edward Anthony Jun 01 '14 at 10:21