Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the essence and significance of C++ citation?

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the knowledge of "what is the essence and meaning of C++ citation". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

I. the meaning of citation

References exist as variable aliases, so they can be used instead of pointers in some cases

References are more readable and practical than pointers.

The following is illustrated by code, which can be written in the C language:

# include void swap (int* a, int* b) {int t = * a; * a = * b; * b = t;} int main () {int a = 1; int b = 2; swap (& a, & b); printf ("a =% d, b =% d\ n", a, b); return 0;}

The following is the output, and you can see that afield b is swapped.

If you use the reference used by C++, use the following code:

# include void swap (int& a, int& b) {int t = a; a = b; b = t;} int main () {int a = 1; int b = 2; swap (a, b); printf ("a =% d, b =% d\ n", a, b); return 0;}

The following is the output. It should be noted that when referencing as a formal parameter of a function, initialization is not required. Initialization occurs when the function is called (once the formal parameter is initialized, it represents two specific external variables).

2. Special citation

Const reference

You can declare const references in C++

Const Type& name = var

Const references let variables have read-only properties

When a const reference is initialized with a constant, the C++ compiler allocates space for the constant value and uses the reference name as an alias for the space

So the above code, b = 5 is incorrect, because b is already a read-only variable, but you can still change the value of this read-only variable through the pointer.

Conclusion: initializing a const reference with a constant will generate a read-only variable

Let's look at a piece of code to deepen our understanding:

# include void Example () {printf ("Example:\ n"); int a = 4; const int& b = a; int* p = (int*) & b; / / b = 5; * p = 5; printf ("a =% d\ n", a); printf ("b =% d\ n", b);} void Demo () {printf ("Demo:\ n"); const int& c = 1 Int* p = (int*) & c; / c = 5; * p = 5; printf ("c =% d\ n", c);} int main (int argc, char * argv []) {Example (); printf ("\ n"); Demo (); return 0;}

The following is the output:

If you uncomment those two lines (b = 5 # c = 5), the following result will be output, and the compiler will report an error that both b and c are read-only variables.

Third, whether references take up storage space

Let's look at a piece of code:

# include struct TRef {char& r;}; int main (int argc, char * argv []) {char c = 'centering; char& rc = c; TRef ref = {c}; printf ("sizeof (char&) =% d\ n", sizeof (char&)); / / char type occupies 1 printf ("sizeof (rc) =% d\ n", sizeof (rc)) / / sizeof (c) = > 1 printf ("sizeof (TRef) =% d\ n", sizeof (TRef)); / /? Printf ("sizeof (ref.r) =% d\ n", sizeof (ref.r)); / / sizeof (c) = > 1 return 0;}

Below is the output. You can see that sizeof (TRef) occupies 4 memory space, and we know that pointers occupy 4 memory space, so what exactly is the relationship between pointers and references? Section 4 to analyze.

IV. The essence of citation

The internal implementation of the reference in C++ is a pointer constant

Note:

The C++ compiler uses the pointer constant as the internal implementation of the reference during compilation, so the reference takes up the same amount of space as the pointer.

From the point of view of use, the reference is just an alias, and C++ hides the details of the storage space of the reference for the sake of practicality.

You can also have a good understanding of the number of bytes occupied by references through the following code:

# include struct TRef {char* before; char& ref; char* after;}; int main (int argc, char* argv []) {char a = 'asides; char& b = a; char c =' c'; TRef r = {& a, b, & c}; printf ("sizeof (r) =% d\ n", sizeof (r)); printf ("sizeof (r.before) =% d\ n", sizeof (r.before) Printf ("sizeof (r.after) =% d\ n", sizeof (r.after)); printf ("& r.before =% p\ n", & r.before); printf ("& r.after =% p\ n", & r.after); return 0;}

The following is the output. You can see that the structure occupies 12 bytes, and the before and after pointers each take 4 bytes, so the ref reference also takes 4 bytes. The starting memory address of the after minus the initial memory address of the before is 8, while the before pointer takes 4 bytes. From this level, we can also know that the ref reference takes 4 bytes.

To gain a deeper understanding of the nature of references, you can disassemble them in Visual Studio 2012, as shown in the figure below, now make a breakpoint at return 0, and then click the local Windows debugger to start executing the code.

After executing the code, right-click the blank area and choose to go to disassembly.

Let's take a look at the disassembly part of the code, mainly look at the reference part of the assembly code, lea eax, [a] to take the address of a, stored in the eax register, mov dword ptr [b], eax means to save the address of a to the corresponding 4 memory spaces of b. It can be said that the internal implementation of a reference is a pointer, so the reference takes up memory space and takes up the same amount of memory as the pointer.

V. points for attention in citation

References in C++ are designed to replace pointers in most cases

Functionality: can meet most situations where pointers are needed

Security ∶ can avoid memory errors caused by improper pointer operation.

Operational ∶ is easy to use without losing its powerful function.

Let's return the reference through a function to introduce the considerations of the reference.

# include int& demo () / from the perspective of internal implementation, you want to return a pointer int* const {int d = 0; printf ("demo: d =% d\ n", d); return d; / / essentially, equivalent to return & d} int& func () {static int s = 0; printf ("func: s =% d\ n", s); return s / / essentially equivalent to return & s} int main (int argc, char* argv []) {int& rd = demo (); int& rs = func (); printf ("\ n"); printf ("main: rd =% d\ n", rd); printf ("main: rs =% d\ n", rs); printf ("\ n"); rd = 10; rs = 11; demo (); func () Printf ("\ n"); printf ("main: rd =% d\ n", rd); printf ("main: rs =% d\ n", rs); printf ("\ n"); return 0;}

The following is the output. You can see that when compiling, you begin to warn that local variables cannot be returned. If you continue to run, you can see the alias of rd = 9658356 Rd for d. In theory, it should output 0, why output 9658356? This is because the variable represented by rd is destroyed when the demo function call returns, and it represents a variable that does not exist, so rd is meaningless.

The rule that must be followed in references: do not return references to local variables. If the local variable is static, you can. Because the storage area of a static local variable is a global storage area, its space is not destroyed by the return of the function.

This is the end of the content of "what is the essence and meaning of C++ citation". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report