C ++ Pointer and Reference vomit blood arrangement pointer & amp; Reference, vomit blood Pointer

Source: Internet
Author: User

C ++ Pointer and Reference vomit blood arrangement pointer & Reference, vomit blood Pointer

Many people have a headache and confuse when talking about the pointer of C ++. It is often confused with variable names, references, etc. In fact, the main reason is that many programmers have problems with basic knowledge, resulting in confusion of many basic concepts. This article focuses on analyzing and comparing pointers and references from the basic concepts. This article focuses on the following aspects:

1. variable representation;

2. pointer structure and principle;

3. structure and principle of reference;

4. Application and precautions of pointers in Array;

5. the pointer cannot be dereference;

I. Form of Variables

Many people think that variables are very simple. variables are defined and applied every day. But are you sure you want to stop? How is variable defined in computer science? Then how does variable store values in the memory? So let's answer the above questions one by one. First of all, the most important thing is the definition of variable. When you declare a variable, the computer binds the specified memory space to the variable name. This definition is simple but abstract, for example, int x = 5; this is the simplest variable assignment statement. We often say "x is equal to 5". In fact, this statement is wrong. x is just the name of a variable, it is not equal to any value. The correct translation of this statement should be: "Assign 5 to the memory space named x". In essence, the value 5 is assigned to a memory space, the memory space name is x. Remember: x is just a simple alias, and x is not equal to any value. The figure below shows:

The operation of variables in the memory actually requires two steps:

1) Find the memory address corresponding to the variable name.

2) retrieve the value in the memory space corresponding to the address based on the found address.

Ii. structure and principle of pointers

What is a pointer first? Like any variable, the pointer variable also has a variable name, which corresponds to the memory space of the variable name, except that the pointer has the following special features: the memory space storage value corresponding to the pointer variable is exactly a memory address. This is also a feature that distinguishes pointer variables from other variables. For example, a pointer is defined as follows:

 

int x = 5;int *ptr = &x;

 

Ptr is a positive variable name. The value in the memory to which the Pointer Points is obtained through the pointer is called dereference. I do not know what the Chinese translation is. [Shame], haha. Dereference

 

Its representation relative to the memory space is as follows:

Note: Do not drill into the variable name x or the tip of ptr. Do not think about where these variable names are stored. The variable name is just the code name of a memory space, we should be concerned with the memory address corresponding to these variable names. According to the above analysis, we can see that pointer variables and any variables in the memory form is the same, just because their stored values are special.

Iii. structure and principle of reference in memory

Reference is also frequently used in C ++, especially when used as a function parameter, You need to modify and update the external value of the function inside the function, it can be said that there are many reference scenarios. However, programmers generally have difficulty or do not pay attention to the analysis of the reference and pointer. They only know how to apply the reference, rather than analyzing the reference in detail. Next I will analyze this reference. First, we must make it clear that reference is a special pointer. From this we can see that the storage structure of the reference in the memory should be the same as the above pointer, which is also the memory address of the storage. For example, the definition of reference is as follows:

int x = 5;int &y = x;

Reference and pointer have the following three differences:

1) The reference can directly obtain the value of the memory space to which it points without the need for dereference. For example, in the above example, you can directly obtain the value of the memory space pointed to by reference y without * y.

2) The value assignment operation of reference does not require an address operator to assign values. You can directly use the variable name. For example, in the preceding example, int & y = x, int & y = & x;

3) The reference must have an initial value when declaring it, and the memory address pointed to by the reference variable cannot be changed. Unlike pointer, the reference variable can flexibly point to other addresses.

Shows the structure and relationship between reference and pointer in the memory:

4. Application and precautions of pointers in Array

In C ++, an Array-type variable arr is essentially a pointer to the first element of the Array. String is actually an array of the char type in C ++. For example, char arr [] = {'A', 'B', 'C', 'D ', 'E', '\ 0'}; this is a string "abcde", where arr [0], arr [1], arr [2] ...... the difference between two values may not necessarily be 1 byte. compiler will automatically determine the difference between them based on the array type; in addition, strings in c ++ can also be expressed in the string literals (Translate by great god) mode, that is, char * arr2 = "abcde "; however, the string represented by string literal is read only and cannot be modified. For example, * (arr2 + 1) = 'F'; this statement produces an error. The memory format is shown in:

V. The pointer cannot be dereference.

But when the value of a pointer is invalid, this pointer cannot be dereference. Which of the following situations is invalid? There are mainly the following situations:

1) when the value of this pointer is NULL, this pointer cannot be dereference. Because the pointer is NULL, it indicates that the Pointer Points to the address block with the memory address 0. The memory space with the memory address 0 has no value, so it cannot be dereference. For example: int * ptr = NULL; cout <* ptr <endl; is incorrect.

2) When a pointer is deallocte or the memory space of a pointer is erase, the pointer cannot be dereference. For example, the following code:

int *function(int a){        int temp = 5;    return &temp;}

The pointer returned by the code above cannot be dereference, because temp will be reclaimed by the system after it is out of scope, and the memory space occupied by temp has been erase, therefore, it returns a pointer to the memory space occupied by the erase. It cannot be dereference. Otherwise, an error occurs. A warning will be given during the compilation phase. during runtime, if dereference occurs, there will be an error.

The pointer and reference of C ++ are summarized here.

 

If you have any questions, please leave a message or suggest. Thank you.

 

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.