A detailed description of reference passing and pointer transfer function parameters in C + +

Source: Internet
Author: User

First, the analysis of the pointer this stuff:

Conceptually, a pointer is essentially a variable that holds the address of a variable, logically independent, and can be changed, including the change in the address it points to and the data stored in the address it points to.

The graph above shows the value and address of the variable when the program is run, what is the memory length like?

Note that the pointer is a variable, of course, it has a memory space, which is stored in an address, through this address we can find the object it points to.

Description: Two letters p and N on the far left, what does it mean? Later in the introduction of the process of compiling procedures used, first sell a lawsuit. If the following writing things you do not understand, it is OK, look down, I do not believe you read the final compilation of the principle of a little knowledge, you still do not understand!

Again to analyze the reference to this stuff:

Whereas a reference is an alias, it is logically not independent, its existence is dependent, so the reference must be initialized at the outset, and the object it refers to cannot be changed throughout its life cycle (it can only be attached to the same variable from beginning to end).

The above paragraph, if not understand, it doesn't matter, look down.

In C + +, pointers and references are often used for parameter passing of functions, however, pointer passing parameters and reference passing parameters are inherently different:

A pointer pass parameter is essentially the way a value is passed, which is passed an address value. In the process of value passing, the formal parameters of the called function are treated as local variables of the modulated function, that is, the memory space is opened in the stack to hold the values of the arguments put in by the key function, thus becoming a copy of the argument. The characteristic of value passing is that any operation of the function on the formal parameter is done as a local variable, without affecting the value of the argument variable of the main key function. (Here is the address value of the argument pointer itself will not change)

Description: The red line is the space for another function that can modify the value of n by a pointer, or modify its own value to point to another address, no longer pointing to N. But anyway, it can never change the value of P. Because the parameter is passed in the way that the value is passed.

Note: What can I do to modify P-values? The memory space in which the variable identifier corresponding to P can be modified is called modifying the value of P.

In the process of reference passing, the formal parameters of the called function also open up the memory space in the stack as a local variable, but the address of the argument variable that is put in by the key function is stored. Any operation of the modulated function on the formal parameter is handled as an indirect addressing, that is, the argument variables in the central Melody function are accessed through the address stored in the stack. Because of this, any action taken by the modulated function on the parameter affects the argument variables in the keynote function.

Description: Ref is no longer the address where the pointer holds n, it instead holds the address of P and transparently performs indirection on ref, so any operation on ref modifies the value of the memory space corresponding to the variable identifier p.

For example, the program executes: Ref =0012FF23 (a random memory address, meaning to modify the memory value of the pointer p);

Ref first addressing, based on the memory space address 0x0012ff36 of ref in the compiler symbol table, locate the variable identifier corresponding to the memory space in which the 0012ff40 is placed.

The second addressing, the 0012ff40 as the address, found in the memory space is stored in the 0012ff44.

Then change the 0012ff44 to 2 (decimal).

Reference passing and pointer passing are different, although they are a local variable on the function stack space, but any handling of reference parameters is handled by an indirection to the relevant variables in the key function. For pointers passing parameters, if you change the address of the pointer in the called function, it will not affect the relevant variable of the key function. If you want to change the related variable in the key function by passing the pointer parameter, you have to use a pointer to the pointer, or a pointer reference.

Description

(1) When the pointer passes the parameter, the case is the picture above, and the pointer p copies the contents of its memory into the memory space of Q. So both P and Q are pointing to an instance object in the heap space, which means that they all store the address of the instance object in the heap space. But if I change the address in the memory of Q in the function of being tuned, which means that Q doesn't point to this instance object, then these are all legal. And we cannot modify the memory contents of p in the main function, that is, the heap memory instance object that P points to cannot be modified by P.

(2) But the use of the application to pass parameters is not the same, in fact, the nature of the reference is still a pointer, but reference to the corresponding memory space is not the corresponding memory space for the storage of the contents of the backup. Reference memory space is stored in P (that is, p corresponding memory space address, such as the above 0012ff40, that is, we said the compiler's symbol table address, p is the corresponding 0012ff40, you write in the program P and write 0012ff40 are the same, Are the same memory space corresponding to the address, but p more easily memory programmer).

And this reference in the function of any use of a transparent way to access the indirect address, that is, you just modify the ref (modify the meaning of the ref corresponding to the memory of things), the compiler will do a transparent (transparent, because this process is the compiler to do it privately, Programmers cannot be seen or controlled. Of course, if you want to modify the compiler that can be the indirect addressing, you have any of the changes to the pointer p corresponding to the memory space.

And this with the association is not to be modified after initialization. In the end how can not be modified, see below:

The example has been said before.

So, how does the compiler do transparent indirection? Here's a look at the compilation process:

To further deepen the distinction between pointers and references, let me elaborate on the differences between them from a compilation perspective:

The program adds pointers and references to the symbol table at compile time, and the symbol table records the variable name and the corresponding address of the variable. The corresponding address value of the pointer variable on the symbol table is the address value of the pointer variable, and the corresponding address value on the symbol table refers to the address value of the Reference object. The symbol table does not change after it is generated, so the pointer can change the object it points to (the value in the pointer variable can be changed), and the reference object cannot be modified.

To illustrate this, let's talk about the basics of compiling:

Some simple knowledge of compiling principles:

compiler when compiling our program, the general compiler will scan our source program two times to complete the compilation (of course, if using a zipper-backfill can be scanned again, this is not our focus). One of the most important tasks in the first pass is to create a symbol table, and the second is to use these symbols for memory space operations. The compiler scans all the variables in the source program and then gives them a table with the contents of the table, including the name of your variable (symbol), the address in the memory space, and the type of the variable.

Note: The symbol table does not have content stored in this variable's memory space. As if our hotel management, the front desk may also have a table, which corresponds to the room number, room address, room size.

Such as:

Room 1th->b Area A building three floor 302 rooms->45 Ping

Room 2nd, room Three, building C,->b area, 432 rooms->80 Ping

"Room 1th", "2nd room" is for good memory, front desk is feel "b area a building three floor 302 room" Such words too troublesome.

The memory of the computer is similar, the only difference is that we do not care about the computer to me where the existence, also said I just know my is "what room", and I remember, computer also remember. I don't care where you leave me in which building number room. Every time I want to use, I say "what room", and then the computer through the above table for me to find the specific location of the room.

The compiler is the foreground, maintaining a table. The programmer is the tenant, I only know that I am the "number room".

See, in fact, the p and n variables, such as the symbol, is an address. As the above figure N is the beginning of the 0x0012ff40 Four memory storage units (because n is a variable of type int, occupy four storage units, this is related to the computer), the reason is to use n is simple, or old said 0x0012ff40, a will be ignorant.

Another thing is that p corresponds to the memory space in the storage of things, this is not in the symbol table, you must wait until the program to run, access to memory when you can see. When the program is running, any manipulation of the variable symbol (that is, the address of the corresponding memory unit) is translated into the operation of the corresponding memory unit.

Like what:

n=2; this operation.

The computer will first go to the symbol table to find the variable symbol, find its corresponding memory address, as shown in the figure above, holding N to find the 0x0012ff44, and then based on this address, found the memory of the real storage unit (n just for the sake of our memory to get a symbol, There is no such thing in physical memory, but we have the symbol table, which can be converted to the actual memory address. Then read and write to this memory space according to the program's needs.

If you do not define this variable n direct use, then the first scan when you do not register your n corresponding memory address is how much, you directly access here, then the compiler will prompt the variable undefined error.

Isn't it fun? Here's the return to today:

The compiler uses symbolic tables to implement transparent indirect address access to reference variables, so many of them are meant to illustrate how references are made in the symbol table.

If this is the memory space, what is the symbol table like?

See, ref this goods unexpectedly in the symbol table does not correspond to its own address (0X0012FF40), directly corresponds to other people's address. If you operate on ref, the computer takes ref to its memory address and reads and writes to that memory space. But a look-up table, is the address of the memory space p corresponds to the natural operation p corresponding memory space.

The symbol table is generated after the compiler first reads the source program, and then it is not changed, so the pointer can change the object it points to (changing the stored data in the memory space of the pointer variable p), and the reference object cannot be modified, that is, if ref is a reference to P, it will have to be a reference to p for a lifetime, Because the variable symbol table does not change, you just have to manipulate ref directly corresponds to the memory space of P (ref true memory space is not important, no one can see, no one can use). Some people may say that I output ref's address to see, sorry, the address is the above symbol table content. (If you really want to see it, you have to get the compiler to gouge it out and see what's inside it.)

Note: Since the reference itself is an alias of the target, the address of the reference itself is a meaningless value, so the memory address of the reference cannot be obtained in C + +. The address of the reference is the address of the destination, and C + + itself does not provide a way to get a reference memory address at all.

A detailed description of reference passing and pointer transfer function parameters in C + +

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.