In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "the case introduction of the difference between C++ pointer and citation". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. let's study and learn the case introduction of the difference between C++ pointer and citation.
The difference between pointers and references in C++
Conceptually. A pointer is essentially a variable that stores the address of a variable, which is logically independent, and it can be changed, including the change of the address it points to and the change of the data stored in the address it points to.
The reference is an alias, it is not logically independent, its existence is dependent, so the reference must be initialized at the beginning, and its referenced object cannot be changed throughout its life cycle (it can only be attached to the same variable from beginning to end).
In C++, pointers and references are often used to pass parameters to functions. However, parameters passed by pointers and references are essentially different:
The parameter passed by the pointer is essentially a way of passing a value, and what it passes is an address value. In the process of value transfer, the formal parameters of the modulated function are treated as local variables of the tuned function, that is, a memory space is opened up in the stack to store the values of the arguments put in by the main function, thus becoming a copy of the arguments. The characteristic of value transfer is that any operation of the modulated function on the formal parameter is carried out as a local variable and will not affect the value of the argument variable of the main tone function.
In the process of reference transfer, although the formal parameters of the tuned function also open up the memory space in the stack as local variables, what is stored at this time is the address of the argument variable put in by the main tone function. Any operation of the modulated function on the formal parameter is treated as indirect addressing, that is, the argument variable in the main tone function is accessed through the address stored in the stack. Because of this, whatever the tuned function does to the parameter affects the argument variable in the main tone function.
Reference passing and pointer passing are different, although they are both local variables in the stack space of the called function, but any processing of reference parameters will operate on the relevant variables in the main tone function through an indirect addressing. For the parameters passed by the pointer, if you change the pointer address in the tuned function, it will not affect the relevant variables of the main tone function. If you want to change the relevant variables in the tone function by passing pointer parameters, you have to use a pointer to a pointer, or a pointer reference.
In order to further deepen the difference between pointers and references, let me explain the difference between them from a compilation perspective:
When compiling, the program adds pointers and references to the symbol table, which records the variable name and the corresponding address of the variable. The address value of the pointer variable on the symbol table is the address value of the pointer variable, while the address value of the reference on the symbol table is 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), while the reference object cannot be modified.
Finally, summarize the similarities and differences between pointers and references:
★ similarities:
● is the concept of address.
The pointer points to a block of memory whose content is the address of the memory it refers to, while a reference is an alias for a block of memory.
★ is different:
The ● pointer is an entity, while the reference is only an individual name
● references can only be initialized once when defined, and then immutable; pointers are mutable; references are "from one to end", and pointers can be "changed".
The ● reference has no const, and the pointer with const,const is immutable.
● reference cannot be null, pointer can be null
● "sizeof reference" gets the size of the variable (object) it points to, while "sizeof pointer" gets the size of the pointer itself.
The meaning of self-increment (+ +) operation of ● pointer and reference is different.
● references are type-safe, while pointers are not (references have more type checking than pointers
Thank you for your reading. The above is the content of "case introduction of the difference between C++ pointers and citations". After the study of this article, I believe you have a deeper understanding of the difference between C++ pointers and citations. Specific use also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.