Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

larswik

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
This has me stumped and hoping to have some light shed on it.

myFraction = [Fraction alloc];
myFraction = [myFraction init];

The book says "The allco method is guaranteed to zero out all of the objects instance variables. However, that does not mean that the object has been properly initialized for use. You need to initialize an object after you allocate it."

I don't understand what they mean by "zero out". My guess would be that it means if I have any instance variables in objects they would disappear or be zeroed out??? But how can they be zeroed out if I still need to initialize them? Wouldn't they already be zeroed (not storing any variables) when the second step happens?

Thanks,

-Lars
 

HiRez

macrumors 603
Jan 6, 2004
6,265
2,630
Western US
This has me stumped and hoping to have some light shed on it.

myFraction = [Fraction alloc];
myFraction = [myFraction init];

The book says "The allco method is guaranteed to zero out all of the objects instance variables. However, that does not mean that the object has been properly initialized for use. You need to initialize an object after you allocate it."

I don't understand what they mean by "zero out". My guess would be that it means if I have any instance variables in objects they would disappear or be zeroed out??? But how can they be zeroed out if I still need to initialize them? Wouldn't they already be zeroed (not storing any variables) when the second step happens?
When you call init, it calls the init method of the object, which can do additional setup, such as setting default values, creating instance variables like arrays, dictionaries, or other objects, checking the system for information, loading resource files, or many other things. alloc just allocates the memory at a low level, init does most of the setup at the object level. So you need them both, and also I've never seen them done separately like you have there. You'd typically write it like:
Code:
Fraction *myFraction = [[Fraction alloc] init];
 

larswik

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
HIREZ, thanks again. So that could be loading preference files and things like that, I understand. The book started out using that example to simplify the examples. He then wrote it the way you did and said that this is the way people do it.

I am re reading this section again tonight to I better understand it. Thanks for helping me learn!!!

-Lars
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.