Before you look at this article, please understand first: That is C for all the access controls we provide. It is just the restrictions on our compilation phase, that is, the compiler ensures that you have completed the task. The correct behavior, if your behavior is incorrect, then you want to construct any executable program. But if you really have an executable code phase, both C, C , or PASCAL, everyone is the same, do you think that the machine code generated by the C and C compilers will be different, you think that the machine code generated by C will have access. Is it limited? So you are wrong. What const, private, unparalleled in the read-only data segment (Const variable), will not give you any restrictions, you can use all memory modification tools or write a program to a process space A variable is modified, no matter what it is in your impression, or public, for you, you are the same, how do you want to In addition, don't make a magic for what the advanced bundle of C , it is only a few more than the code generated, it is far without what you imagine, all work is compiled. When you are finished, when you really have executed, the computer is completely meticulous in accordance with the code generated by the compiler. Do you understand what I am talking about? By the way, if you have been contained in front of the compilation, as long as you disassemble a C code, you will say: It turned out to be such a thing, C is just a higher level of abstraction, but as long as you solve Open the yarn, return to the issue of the problem, everything will be no longer mysterious ... (The following disassembly code is from Visial C 7.0). Let us start from the variable ----- not what simple variable you imagine, the variable is an amount that can be changed during the execution of the program. For an angle, the variable is the name of a memory area, which represents this memory area. When we modify the variable, it will cause changes in the content in the memory area. But if you have learned a compilation or computer composition principle, then you will clear what the name is not existing for a memory area, and the flag it only is his address, so we want to modify a memory area. The content, only knows his address to be implemented. It seems that the so-called variable is just an abstraction that the compiler gives us, let us not have to learn more details and reduce our thinking spans. For example, the following statement: int A = 10; In accordance with our thinking habits, "there is a variable A, its value is 10", everything is so natural. We don't have to care about what the so-called address and some other details. However, in the underlying implementation of this statement, A is no longer a variable, it is just a tag, representing an address tag: Mov DWORD PTR [A], 0ah; how, this statement is not like the above Easy to accept, because it needs to know more details, you can hardly get any help of the compiler, all the thoughts must be done by yourself. This statement should be interpreted as "write 10 to a memory area address of A). what did you say? A Some like a pointer? Yes, it is indeed like it, but it is not, but their process seems to be similar. The leap here is actually from a realistic issue to the specific address and the span of the memory area.
two. Quote: You can have a reference, but the compiler only has a pointer (address) to have seen the first one, you must have a certain understanding of the compiler's work. In fact, the compiler is a conversion between the programmer and the underlying. Layer, it converts a high-level language code into a low-level language code, the larger the conversion span completed by a compiler, then it will become more complicated because the programmer works by him. The C compiler must be complicated than the compiler compiler. If I ask you the same reference and pointer? You may say that it is of course different, the pointer is easy to generate an unsafe factor, but it will not be, is it true? Let's look at the following code: INT * E = new int (10); int & f = * e; delete e; f = 30; what do you think above, I feel unsatisfactory, it and pointer Have the same hidden danger. Because the memory area it is cited is illegal. I personally think that the so-called reference is actually a pointer, and only the interfaces are not the same, and the referenced interface has a certain limit. The pointer can be one-to-one, but the reference can only be one-on-one, that is, & refer cannot be changed, but it can't say that one-on-one is safe, but the dangerous coefficient is reduced. The reference is more easily controlled than the pointer. OK, let's talk about the pointer, people who have had a compilation experience will definitely say, grace, some places in the pointer, some are like compilation, especially the "*", how to "[]" in the assembly. Oh, it is indeed, it also covers an addressing process. It seems that the pointer is indeed a relatively low-level thing. However, the reference is not that is not that, although the programmer is used to safely a lot. But you have to clear, only you can have references, the compiler doesn't have this tool, the computer doesn't know this thing. Therefore, its underlying mechanism is actually the same as the pointer. Don't believe there is only one memory copy, don't think that the reference can save you a spatial space, because all this will not happen, the compiler will still interpret the reference as a pointer. Whether you don't believe it, please see the following code: int & b = a; lea eax, [a]; MOV DWORD PTR [B], EAX; assigning A to a piece of memory B = 50; MOV EAX, DWORD PTR [B]; MOV DWORD PTR [EAX], 32H; INT * D = & A; Lea Eax, [A]; MOV DWORD PTR [D], EAX * D = 60; MOV EAX, DWORD PTR [ D] MOV DWORD PTR [EAX], 3CH; The above code is from the specific compiler, how, believe it, good, let me make a perhaps not very appropriate, you must edit the linear form and The stack of the stack, the linear table is a very flexible data structure, there are many operations above, but the stack, it is a linear table for restrictive operation, and its underlying operation is actually achieved by linear table operation. . It is like the relationship between Stack and Vector, so the relationship between the pointer and the reference is better than the relationship between the linear tables and stacks, the reference is the limited pointer, which is not the same as the external interface and pointer, but the bottom layer is the same.