Alloc and init problems in Object c, alloc init

Source: Internet
Author: User

Alloc and init problems in Object c, alloc init
Alloc and init problems in Object c

Starting from NSString * name = [[NSString alloc] init], the instructor taught this sentence to allocate memory space and keep using it, I have never considered how it is implemented internally. I accidentally saw this code today.

NSString * name = [NSString alloc];

NSLog (@ "% p", name );

Name = [name init];

NSLog (@ "% p", name );

 

I tried to print it and found that the two memory addresses are different:

   13:19:51. 724 LessonMRC2 [] 0x100203850

13:19:51. 726 LessonMRC2 [1222:303] 0x7fff73ada7b8

Alloc is used to open up a memory space, and init is used for initialization. Why is initialization not initiated on the original memory space, but re-opened a memory space?

So I started to check the information and found a new confusion:

NSObject * obj = [NSObject alloc];

NSLog (@ "% p", obj );

Obj = [obj init];

NSLog (@ "% p", obj );

After printing:

13:23:10. 663 LessonMRC2 [1232: 303] 0x100103730

13:23:10. 665 LessonMRC2 [1232: 303] 0x100103730

Why is the address changed again?

Try printing NAArray

NSArray * name = [NSArray alloc];

NSLog (@ "% p", name );

Name = [name init];

NSLog (@ "% p", name );

Print:

13:26:15. 154 LessonMRC2 [1244: 303] 0x100103740

13:26:15. 156 LessonMRC2 [1244: 303] 0x100102f50

It is still different.

Why?

First, let's look at the init method of NSStrng:  

-(Id) init

 

{

 

If (self = [super init]) // The value is assigned again.

 

{

 

....

 

}

 

}

 

It can be analyzed from the code. If self = [super init] is not nil, the memory space will be re-allocated. This explains why after NSString and NSArray call the alloc] init] method, the memory address will be different,

But why is NSObject the same? We know that NSObject is the base class of all classes. When [NSString alloc] init] is executed, the [super init] called is the init method in NSObject, since NSObject is a base class, it cannot call super init. Therefore, when NSObject runs [[NSObject alloc] init], no space is allocated by init.

 

As to why does apple need to initialize an instance in two steps, I personally think it is convenient to initialize different methods after construction. If the new keyword is used, only one init can be called, rather than initwithname and other methods.

 

Knowledge Development:

If there is no init after NSString alloc, can the memory space after alloc be used? The answer is obvious. If it can be used, Apple does not need to provide an init method. What is the pointer after alloc? Suspension pointer.

If a local pointer is neither null nor set to point to a known object, such a pointer is called a hanging pointer. It is very dangerous in the program.
When the program runs the pointer, the program cannot judge the validity of the pointer, which may cause serious errors.

In object-c, this sentence Dog * dog = [[Dog alloc] init]; first of all, there is a dog class, and the dog class inherits from nsobject, when you use this dog class, you use the following sentence: Dog * dog = [[Dog alloc] init];
Then, all the attributes and methods of the dog class are available in your use. You can use the dog. attribute.
In oc, only single inheritance... multi-inheritance can realize classification category.
Hope to help you
The "initWithTitle" in object-c is a static method, not a common method. [UIViewController alloc] has opened up space to create objects.

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.