Go C + + pointers and references

Source: Internet
Author: User

Transfer from http://www.cnblogs.com/tangxiaobo199181/

Algorithm life public Number: Algorithm Life Source: http://www.cnblogs.com/tangxiaobo199181/This copyright belongs to the author, welcome reprint, but without the consent of the author must retain this paragraph statement, and in the article page obvious location to the original link, Otherwise, the right to pursue legal liability is retained.



C + + pointers and references vomiting blood finishing pointer&reference

Said C + + pointers, many people are very headache, also very confuse. Often confuse it with variable name, reference (reference), in fact, the main reason is that many programmers have problems with the mastery of basic knowledge, which leads to confusion of many basic concepts. This article starts with the most basic concepts, focusing on analyzing and comparing pointers and references. Mainly from the following aspects of the focus on the explanation:

1. Representation of variables (variable); 2. The structure and principle of the pointer; 3. The structure and principle of the reference; 4. The application and considerations of pointers in array; 5. There are several cases where pointers cannot be dereference; one: The form of a variable

When it comes to variables, many people find it very simple to define variables and apply variables every day. But did you stop and savor the details of what is a variable? How is the definition of a variable (variable) defined in computer science? And how does variable store the value in memory? So follow the above question, we come to one by one of the solution, first of all, the definition of variable, when you declare a variable, the computer will specify a piece of memory space and variable name binding; This definition is very simple, but actually very abstract, for example: int x = 5; This is the simplest variable assignment statement, we often say "x equals 5", in fact this argument is wrong, X is just a name of the variable, it is not equal to any value. The correct translation of this statement should be: "Assign 5 to the memory space named X," which is essentially assigning a value of 5 to a piece of memory space, and this memory space name is called X. Remember: X is simply an alias, x is not equal to any value. The diagram is as follows:

The operation of the variable in memory is actually going to take 2 steps:

1) Find the memory address that corresponds to the variable name.

2) According to the address found, take out the address corresponding to the memory space inside the value to operate.

# #二: The structure and principle of pointers

First, what is a pointer? The pointer variable, like any variable, has a variable name, and the memory space corresponding to the variable name, except that the pointer is unique in that the memory space that corresponds to the pointer variable stores the value exactly as a memory address. This is also one of the characteristics of the pointer variable that distinguishes the other variables. For example, a pointer is defined as follows:

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

PTR is a variable name. Get the value of this pointer to the memory by the pointer is called dereference, this Chinese translation is called What I do not know. "Ashamed", haha. Dereference

Its relative to the memory space is represented as follows:

Special reminder: Here do not get into the variable name x, ptr horn inside, do not think about where these variable names are stored, the variable name is just a piece of memory space code name, we should be concerned about these variable names corresponding to the memory address. As you can see from the above analysis, pointer variables are the same as any variables in memory, simply because the values they store are relatively special.

Three: The structure and principle of reference in memory

References (reference) are also often used in C + +, especially when you need to modify the values outside of the function inside the functions as function arguments, which can be said to be rich in reference scenarios. But programmers generally difficult or not to pay attention to analysis reference and pointer, just know how to apply it, and not to specifically analyze the reference. Here I will briefly analyze this reference. The first thing we have to make clear is that reference is a special kind of pointer. From this, it can be seen that the storage structure of reference in memory should be the same as the pointer above, and also the address of the stored memory. For example reference is defined as follows:

int x = 5;int &y = x;

Reference and pointer are mainly in the following 3 different points:

1) reference does not need dereference to get directly to the value that points to the memory space. For example, in the previous example, direct y can get the value of the memory space pointed to by reference y without the need for *y.

2) Reference assignment operation also does not need to address the value of the assignment, you can directly through the variable name, such as the above example, int &y = x, without the need for int &y = &x;

3) Reference must have an initial value at the time of Declaration, and the memory address that the reference variable points to cannot be changed, unlike pointer, which can be flexibly re-pointing to other addresses.

The structure and relationships of reference and pointer in memory are as follows:

Four: Application of pointers in array and considerations

In C + +, an array-type variable, arr, is essentially a pointer to the first element of the array. string strings in C + + are actually a char type of array, for example: char arr[] = {' A ', ' B ', ' C ', ' d ', ' e ', ' + '}; This is a string of strings "ABCDE", where arr[0],arr[1], Arr[2] ..... The difference between the values may not necessarily be 1byte, according to the type of the array to judge, compiler will automatically determine the difference between them; In addition, in C + + string can also be expressed in the form of string literals (for the Great God translation), namely: char arr2 = "ABCDE", but the string represented by string literal is read only, cannot be modified, for example: /c0> (arr2+1) = ' F '; This sentence will produce an error. Its representation in memory is as follows:

V: The situation where the pointer cannot be dereference

But the value of a pointer is invalid, then the pointer is not dereference. So what are some of the invalid? There are several main situations:

1) When the value of this pointer is null, the pointer is not dereference. Because the pointer is NULL, it means that the pointer points to an address block with a memory address of 0, the memory address of 0 is not a value, so it cannot be dereference; for example: int ptr = NULL; cout<<ptr< <endl; Is wrong.

2) If a pointer is DEALLOCTE or the memory space where a pointer resides is erase, then the pointer is not dereference; For example, the following code:

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

The pointer returned by the above code is also not dereference, because temp is out of scope and will be reclaimed by the system, and the memory space occupied by temp has been erase, so it returns a pointer to the memory space that is erase. Also can not dereference, otherwise it will be wrong. The compile phase will give a warning, and in runtime, if the dereference will have error.

All right, C + + pointers (pointer) and references (reference) are summed up here first.



Algorithm life public Number: Algorithm Life Source: http://www.cnblogs.com/tangxiaobo199181/This copyright belongs to the author, welcome reprint, but without the consent of the author must retain this paragraph statement, and in the article page obvious location to the original link, Otherwise, the right to pursue legal liability is retained.

Go C + + pointers and references

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.