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

The application of pointer and what are the attentive problems in Cstroke +

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

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "the application of the pointer and what is the problem of attention". In the operation of actual cases, many people will encounter such a dilemma. Next, 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!

First, let's recall the concept of pointer to facilitate the following introduction.

A pointer is a variable or constant that holds an address value. For example: int a pointer constant ("&" indicates an address operator, that is, a reference). Int * b indicates pointer variables (note that b represents pointer variables rather than * b) and * indicates pointer variables to be specified. Note that int * b [2] and int (* b) [2] are different. Int * b represents an array of pointers, while int (* b) [2] represents an int pointer with two elements. Pay attention to the priority of operation here, which is helpful to understand the pointer problem. It is enough to introduce the basic concepts here, but as for the specific methods of use, such as assignment, many books have introduced them, so I won't say any more.

II. Application and problems needing attention

1. The key to understanding pointers-- understanding the types of pointers and the types they point to.

①, pointer type: you can remove the pointer name, and the rest is this pointer.

For example: int * a bank / pointer type is int *

Int * * a bank / pointer type is int * *

Int * (* a) [8]; / / pointer type is int * (*) [8]

②, the type pointed to by the pointer: refers to the type that the compiler will treat that piece of memory as. Here, just remove the pointer name in the pointer declaration statement and the "*" sign to the right of the name, and the rest is the type that the pointer points to. The reason why I put them in the * position is to make it clear that they are the focus of learning cripple + pointers, and a correct understanding of them can make you lay a good programming foundation for cripple +.

2. The application of pointer-passing parameters.

In fact, it can be equivalent to an implicit return value, which is more flexible than return's method and can return more values. Just take a look at the following example:

# include "iostream.h" void example (int * A1 minint & b1mint C1) {* a1scotch3; + + b1; + + C1;} void main () {int * a; int bMarc; * aplom6; bactin 7; cout 10; example (adhorec); cout "* a =" * a < cout "" b = "< cout"c =" <}

Output: * axi18

Bread8

Cymbi10

Notice that the values of * an and b have changed, but c has not changed. This is because A1 is a pointer to * a (= 6), that is, it points to the same address as a, so when the value of A1 points to, the value of * a changes. The parameter in the function uses a reference (int & b1), which is an alias for b and can also be understood as a special pointer, so the value of b will change. The parameter int C1 in the function only works in the function and disappears when the function ends, so it doesn't work in main ().

3. A problem about global variables and local variables

Take a look at the program first:

# include "iostream.h" int astat5; int * example1 (int b) {aqumb; return & a;} int * example2 (int b) {int cymb5; bounc; return & b;} void main () {int * a1=example1 (10); int * b1=example2 (10); cout "A1 =" * A1 < cout "b1 ="* b1 <}

Output result:

A1: 15

B1mm 4135

* how can b1 be 4135 instead of 15? Is it the procedure? Isn't that right? Because an is a global variable, it always exists in the memory area of the global variable, while the local variable exists in the stack area of the function and disappears when the function example2 call ends, and b points to an uncertain area, resulting in pointer suspension.

The following is a disassembly of example1 and example2 (compiled with TC++ 3.0):

Example1:

Push bp; stack

Mov bp,sp

Mov ax, [bp+04]; pass parameters

Add [00AA], ax; addition

Mov ax,00AA; returned the address where the result is located

Pop bp; recovery stack, off stack

Ret; exit function

Example2:

Push bp; stack

Mov bp,sp

Sub sp,02

Mov word ptr [bp-02], 0005

Mov ax, [bp-02]; pass parameters

Add [bp+04], ax; addition

Lea ax, [bp+04]; that's the problem.

Mov sp,bp

Pop bp; recovery stack, off stack

Ret; exit function

Can you tell after the comparison? The ax should store the address of the result. In example2, however, the content of [bp+04] is returned, so the pointer points to an uncertain place, and the resulting pointer hangs. In example1, ax returns the address of the correct result.

4. Memory problems: use pointers to pay attention to memory allocation and boundaries

Variables should be given appropriate space during the use of pointers to avoid invisible errors.

Look at the following code:

# include "iostream.h" void main () {char * A1; char * a2; cin "A1; cin" a2; cout "" A1 = "< cout"a2 =" <}

Input: abc

one hundred and twenty three

Output:

A114123

A2 =

Null pointer assignment

The pointer points to "null". The solution is to allocate the appropriate memory to the two strings. Revised code

As follows:

# include "iostream.h" void main () {char * A1; char * a2; a1=new char [10]; a2=new char [10]; cin "A1; cin" a2; cout "" A1 = "< cout"a2 =" < delete (A1); / / Note, don't forget to free memory space delete (a2);}

At this point, you can output the correct results. After allocating the appropriate memory, you should pay attention to releasing the internal reference space, and you should also be careful not to exceed the size of the allocated memory, otherwise there will be an overflow, resulting in unpredictable results.

5. About special pointers-references

References are sometimes more flexible than pointers, and do not produce copies of any variables when they are returned, which reduces memory footprint and improves execution speed. References are easier to understand and more intuitive than pointers. When a reference is used as a parameter, the address of the parameter is not changed, so it can be used as a left value.

Let's look at an example:

# include "iostream.h" char ch [5] = "ABCD"; char & example (int b) {return ch;} void main () {cout "" ch= "< example (2) =" c "; cout"ch=" <}

Output result:

Ch=ABCD

Ch=ABcD

In the actual programming process, references or pointers can be flexibly used to improve the readability and execution efficiency of the program as much as possible.

This is the end of the content of "the application of the pointer and what are the problems to pay attention to" in Cracket +. 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