Use new or alloc/init

Source: Internet
Author: User

Use new or alloc/init2010-07-15

1. in actual development, new is rarely used. Generally, all we see when creating objects is [[classname alloc] init], but it does not mean that you will not be exposed to new, [classname new] will still be seen in some code, and this question may also be asked during the interview.

2. What is the difference between them? Let's look at the source code:

-------------------------------------------------------

+ New

{

Id newobject = (* _ alloc) (class) Self, 0 );

Class metaclass = self-> ISA;
If (class_getversion (metaclass)> 1)

Return [newobject
Init];
Else

Return newobject;

}

-------------------------------------------------------

Alloc/init is like this:

-------------------------------------------------------

+ Alloc

{
Return (* _ zonealloc) (class) Self, 0, malloc_default_zone ());

}

-Init

{

Return self;

}

-------------------------------------------------------

Through the source code, we found that [classname new] is basically equivalent to [[classname alloc] init]. The difference is that alloc uses zone when allocating memory, what is this zone? When allocating memory to objects, it allocates the associated objects to an adjacent memory area, so as to consume a small amount of money during calls and increase the processing speed of the program;

3. Why is new not recommended?

I don't know whether it is found: if new is used, the initialization method is fixed and only init can be called. What if you want to call initxxx? No! It is said that the initial design fully draws on the Smalltalk syntax. Legend has it that allocfromzone already exists at that time: This method, but this method requires passing the parameter ID mycompanion = [[theclass
Allocfromzone: [self Zone] init]; this method is as follows:

+ Allocfromzone :( void *) Z

{
Return (* _ zonealloc) (class) Self, 0, Z );

}

It was simplified as follows:

+ Alloc

{
Return (* _ zonealloc) (class) Self, 0, malloc_default_zone ());

}

However, there is a problem: This method only allocates memory to the object and does not initialize instance variables. Does it return to the processing method like new: Call the init method implicitly inside the method? Later I found that "display call is better than implicit call", so I separated the two methods.

Therefore, this method is now available. When I was interviewed, I was asked this question: "Why should we separate alloc from init ?" At that time, I still couldn't answer the question, and finally said, "To facilitate the call of different initialization methods as needed." I remember that the interviewer didn't respond very well and I felt dissatisfied with the answer, now I think the answer is justified.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.