Null pointer and wild pointer

Source: Internet
Author: User

"Turn from" http://www.cnblogs.com/mjios/archive/2013/04/22/3034788.html

Directory of this document

    • First, what is a null pointer and a wild pointer
    • Two examples of wild pointers and null pointers

Description: This objective-c topic is a prelude to learning iOS development, and to enable programmers with experience in object-oriented language development to quickly get started with objective-c. If you do not have programming experience, or are not interested in objective-c, iOS development, please ignore. Before studying this topic, it is recommended to study the C language topic first.

first, what is a null pointer and a wild pointer1. 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 Student *s1 = null;2 3 Student *s2 = nil;

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

2. Customize the Student class and add the following code to the main function
1 Student *stu = [[Student alloc] init];2 3 [Stu setage:10];4 5 [Stu release];6 7 [Stu Setage:10];

Run the program, 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:10];

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:10];

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 Student *stu = [[Student alloc] init];2 3 [Stu setage:10];4 5 [stu release];6 7 stu = nil;8 9 [stu Setage:10];

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 do not try to send a message to it.

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:10];

Null pointer and wild pointer

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.