"OBJECTIVE-C"-null pointer and wild pointer

Source: Internet
Author: User

First, what is a null pointer and a wild pointer

1. Null pointer

1> A pointer that does not store any memory address is called a null pointer (null pointer)

The 2> null pointer is a pointer that is assigned a value of 0, and its value is 0 before it is specifically initialized.

All two of the following are null pointers:

    1. 1 Student *s1 = NULL;
    2. 2
    3. 3 Student *s2 = nil;
Copy Code


2. Wild Hands

the "wild Pointer" is not a null pointer, it is a pointer to "junk" memory (memory not available). The wild hands are very dangerous.

Two examples of wild pointers and null pointers

Next, use a simple example to compare the difference between a wild pointer and a null pointer.

1. First, open Xcode's memory Management debug switch, which can help detect garbage memory

<ignore_js_op> <ignore_js_op>

 


2. Customize the Student class and add the following code to the main function

    1. 1 Student *stu = [[Student alloc] init];
    2. 2
    3. 3 [Stu Setage:10];
    4. 4
    5. 5 [Stu release];
    6. 6
    7. 7 [Stu Setage:10];
Copy Code



run the process sequence, you will find the 7th line error, is a wild pointer errors!
 

3. Next analyze the cause of the error

1> the 1th line of code is executed, there is a pointer variable stu in memory that points to the student object

Student *stu = [[Student alloc] init];
 
Assuming that the address of the student object is 0xff43, the address of the pointer variable stu is stored in 0xee45,stu student object's address 0xff43. That is, the pointer variable stu points to the student object.

2> Next is the 3rd line of code

[stu Setage:ten];

This line of code means: send a setage: Message to the student object that Stu points to, that is, the Setage: method that calls the student object. For now, this student object still exists in memory, so there is no problem with this code.

3> Next is the 5th line of code

[Stu release];

This line of code means: send a release message to the student object that Stu points to. Here, when the student object receives the release message, it is immediately destroyed and the memory consumed is reclaimed.

(The specific usage of release will be discussed in detail in OC memory management)
 
The student object is destroyed, and the memory with the address 0xff43 becomes "garbage memory", but the pointer variable stu still points to the memory, when Stu is called a wild pointer!

4> finally executes the 7th line of code

[stu Setage:ten];

The meaning of this code is still: send a setage: Message to the student object that Stu points to . but after the execution of Line 5th, the student object has been destroyed, its memory is already garbage memory, if you go to access this piece of memory, it will report the wild pointer error. This memory is no longer available, it is not yours, and you have to visit it, it is certainly not legal. So, this line of code Error!

4. If you change the code, you will not get an error

    1. 1 Student *stu = [[Student alloc] init];
    2. 2
    3. 3 [Stu Setage:10];
    4. 4
    5. 5 [Stu release];
    6. 6
    7. 7 Stu = nil;
    8. 8
    9. 9 [Stu Setage:10];
Copy Code


Note the 7th line of code, Stu becomes a null pointer, and Stu no longer points to any memory
 
because Stu is a null pointer and does not point to any object, the Setage of line 9th: The message is not outgoing and does not cause any impact. Of course, there will be no error.

5. Summary

1> The use of wild pointers to send messages is very dangerous, will error. That is, if an object has been recycled, do not manipulate it, and don't try to send it a message..

2> The use of a null pointer to send a message is not a problem, that is, the following code is not error:

[nil setage:ten];

Original link:http://www.cnblogs.com/mjios/archive/2013/04/22/3034788.html

"OBJECTIVE-C"-null pointer and wild pointer

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.