-3

Possible Duplicate:
alloc and init what do they actually do

we do code like [[className alloc] init],so what actually happens in alloc and what happens in init,my understanding is that alloc will create instance of that class and allocate chunk of memory for that instance,then what is use of init?

thanks in advance.

Community
  • 1
  • 1
vishwa patel
  • 21
  • 1
  • 3

3 Answers3

4

alloc: it allocates the memory for the object you create, increasing the retain count

init: it initializes the object, that you created, with custom values and properties if you provide them, else with default values and properties

Neo
  • 2,807
  • 1
  • 16
  • 18
4

then what is the use of init?

Simple: it does what alloc doesn't do (because it couldn't do). It initializes custom instance variables, sets properties to default values, etc...

3

The alloc method does the allocation and increase the retain count by 1 and Whatever is returned by +alloc must be -released somewhere.

init is responsible for initializing the object after the allocation. consumes the retain count of the messaged object and produces an object of retain count +1 (not 1, but "plus 1"); the result returned from init must be released to be managed correctly.

and by the way, +alloc is a class method and -init is an instance method

Charan
  • 4,940
  • 3
  • 26
  • 43