The difference between new and alloc/init in Objective-c

Source: Internet
Author: User



New is rarely used in real-world development, and generally we see [[ClassName alloc] init], but it doesn't mean you won't touch new, you'll see [ClassName new] In some code, and when you go to an interview, is also likely to be asked this question.



So what's the difference between them? Let's look at the source code first:


 
+ new { 
    id newObject = (*_alloc)((Class)self, 0); 
    Class metaClass = self->isa; 
    if (class_getVersion(metaClass) > 1) 
        return [newObject init]; 
    else 
        return newObject; 
}         


and alloc/init like this .


+ alloc { 
    return (*_zoneAlloc)((Class)self, 0, malloc_default_zone());  
} 

- init { 
    return self; 
}


through the source, we find that [className new] is basically equivalent to [[ClassName alloc] init], except that the zone is used only when alloc allocates memory.



So, what is this zone?



When allocating memory to an object, it allocates the associated object to an adjacent area of memory, so that it can be used at a very low cost and improves program processing speed.






Why is it not recommended to use new?



I don't know if you've found it. If you use new, the initialization method is fixed and can only call init. And what do you want to call initxxx? No way It is said that the original design was completely borrowed from the Smalltalk syntax. Legend has it that time has Allocfromzone: This method, but this method needs to pass a parameter id mycompanion = [[Theclass allocfromzone:[self Zone]] init];



This method looks like this:


+ allocFromZone:(void *) z { 
    return (*_zoneAlloc)((Class)self, 0, z);  
}


It was later simplified to the following:


+ alloc { 
    return (*_zoneAlloc)((Class)self, 0, malloc_default_zone());  
}


However, there is a problem: This method simply allocates memory to the object and does not initialize the instance variable.



Is it going back to new like this: implicitly invoking the Init method inside a method? Later it was found that "the display calls are always better than implicit calls ", so the two methods were later separated.






In summary, new and alloc/init are functionally almost identical, allocating memory and completing initialization. The difference is that the new method can only be initialized with the default Init method, which is used in a alloc way, with other custom initialization methods.



The difference between new and alloc/init in Objective-c


Related Article

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.