The difference between new and Alloc Init

Source: Internet
Author: User

Background notes,new is more old-fashioned, and later found only one newallocinit This notation, reserved new One is backwards compatible, and two is often a simpler way of writing. In fact, the new is called internally by the allocinit.  

Source code:

+New { ? ? IDNewObject = (*_alloc) ((Class) Self,0);//Application Space? ? Class Metaclass= self->Isa;?? Class Metaclass= self->Isa;??return[NewObject init];??//Initialize? ?Else? ? ? ?returnNewObject;} +alloc {??return(*_zonealloc) (Class) Self,0, Malloc_default_zone ()); }? -init {??returnSelf ;?} 

[ClassName New] is basically equivalent to [[ClassName alloc] init]. The difference is only in the memory of the alloc allocated when the zone is used, what is this zone? When allocating memory to an object, the associated object is assigned to an adjacent Memory area, so that it can be used at a very low cost and improves program processing speed .

What's to separate alloc from Init?  

1. Multiple init methods can be used

2. Show call is better than implicit call

If you do not need to use the other INIT functions, such as initwithstring, just use [NSObject alloc] init] , then the new method is more convenient

* New doesn ' t support custom initializers (like initwithstring) * Alloc-init are more explicit than new

The difference between new and Alloc init

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.