Application of C + + pointers and problems of attention

Source: Internet
Author: User
Tags constant

The pointer is a more headache-prone problem in C/A + + learning, and in the process of programming, pointers are often the cause of hidden bugs. Here to talk about the application of pointers and need to pay attention to some of the problems, which may have you usually did not notice the problem, hoping to help readers understand the good pointers.

  First, we have to recall the concept of the pointer, to facilitate the following introduction

A pointer is a variable or constant that holds an address value. For example, int a=1;&a represents a pointer constant ("&" denotes an address operator, or a reference). The int *b,b represents the pointer variable (Note that B represents the pointer variable instead of the *b), and * indicates the pointer variable. Note that int *b[2] and int (*b) [2] are different, int *b represents an array of pointers, and Int (*b) [2] represents an int pointer with two elements, and here you should pay attention to the problem of operation Precedence to help you understand pointer problems.

Here about the basic concept is enough, as for the specific use of methods, such as assignment, a lot of books are introduced, I will not say more.

  Ii. applications and issues of attention

1, understand the key to the pointer--to the type of pointer and the type of pointer to the understanding

①, pointer type: You can remove the pointer name, the rest is this pointer

For example: int *a;//pointer type int *

int **a;//pointer type int * *

int * (*A) [8];//pointer type int * (*) [8]

②, pointer to type: refers to the compiler will take that piece of memory into the type. Here you can simply remove the pointer name from the pointer declaration statement and the "*" number to the right of the name, and the rest is the type that the pointer points to.

I put them first because it's important to understand that they are the focus of the C + + pointer, and that they can make you a good base for your C + + programming.

2, the application of the pointer-transfer parameters.

In fact, it can be the equivalent of an implicit return value, which is more flexible than the return method, can be returned more values, look at the following examples of natural understanding:

#include "iostream.h"

void example (int *a1,int &b1,int C1)

{

*a1*=3;

++B1;

++C1;

}

void Main ()

{

int *a;

int b,c;

*a=6;

b=7;c=10;

Example (A,B,C);

cout "*a=" "*a<

cout "B=" <

cout "C=" <

}

Output: *a=18

B=8

c=10

Note that none of the values of *a and B have changed, and C has not changed. This is because A1 is a pointer to the *a (=6), which is also pointing to the same address as a, so the value of *a changes when the value that A1 points to changes. A parameter in a function uses a reference (int &b1), B1 is an alias for B, or it can be interpreted as a special pointer, so the value of B will change. The parameter int c1 in a function just works in a function and disappears when the function ends.

So it doesn't work in main ().

3. A problem about global variables and local variables

No nonsense, first look at the program:

#include "iostream.h"

int a=5;

int *example1 (int b)

{

A+=b;

Return &a;

}

int *example2 (int b)

{

int c=5;

B+=c;

Return &b;

}

void Main ()

{

int *a1=example1 (10);

int *b1=example2 (10);

cout "a1=" "*a1<

cout "b1=" "*b1<

}

Output results:

A1=15

b1=4135

How can *b1 be 4135, not 15? It's a procedural question, right?

Since a is a global variable, stored in the global variable's memory area, it always exists, while the local variable is present in the function's stack area and disappears after the function example2 () call ends, and B points to an indeterminate region, producing a pointer suspension.

The following is a disassembly of the example1 () and Example2 () (compiled with tc++ 3.0):

Example1 ():

Push BP; into stack

MOV bp,sp

mov ax,[bp+04]; passing parameters

add [00aa],ax; added

MOV ax,00aa; Returns the address of the result

Pop bp; recovery stack, out stack

ret; Exit function

Example2 ():

Push BP; into stack

MOV bp,sp

Sub sp,02

mov word ptr [bp-02],0005

mov ax,[bp-02]; passing parameters

add [Bp+04],ax; added

Lea AX,[BP+04]; the problem is here.

MOV sp,bp

Pop bp; recovery stack, out stack

ret; Exit function

Did you see the contrast? AX should be the address of the result that is stored. In Example2 (), the contents of [bp+04] are returned, so the pointer points to an indeterminate place where the resulting pointer hangs. In Example1 (), AX returns the address of the correct result.

4. Memory problem: Use pointers to focus on memory allocations and boundaries.

You should give the variable a proper space in the process of using the pointer, so as not to create an invisible error.

Please see the following code:

#include "iostream.h"

void Main ()

{

Char *a1;

Char *a2;

Cin "A1;

Cin "A2;

cout "A1=" <

cout "A2=" <

}

Input: ABC

123

Output:

A1=123

A2=

Null pointer Assignment

The pointer points to "null." The solution is to allocate the appropriate memory to these two strings. The 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 up memory space

Delete (A2);

}

This will result in the output of the correct results.

After allocating the appropriate memory, pay attention to free internal reference space, but also should be careful not to exceed the allocated memory size, otherwise there will be overflow phenomenon, resulting in unpredictable results.

5, about the special pointer--reference

A reference is sometimes more flexible than a pointer, and when it is returned it does not produce a copy of any variables, thus reducing the memory footprint and increasing the speed of execution. References are better understood than pointers, and are more intuitive to use. When a reference is used as a parameter, the address of the parameter is not changed, so it can be used as the left value.

Let's take a look at the following example:

#include "iostream.h"

Char ch[5]= "ABCD";

char &example (int b)

{

return ch;

}

void Main ()

{

cout "Ch=" <

Example (2) = "C";

cout "Ch=" <

}

Ch=abcd

Ch=abcd

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

  Third, summary :

Pointers are the key and difficult points in learning C/s, mainly because pointers are more abstract and not easy to understand. Use pointers to know where to put the pointer, and how to get the pointer to the right place. You need to apply a lot of pointers to the bottom of the system, so you need to understand the basic concepts of pointers, such as pointer types and types that pointers point to. Usually should pay attention to observe, understand the process of the procedure, when necessary, can disassemble the program, deepen the understanding of the pointer, this method is also suitable for learning other programming knowledge.

  Iv. End of :

The application of pointers is very extensive, the use of pointers can do many things, to become a good programmer, must have a more profound understanding of the pointer. The purpose of this article is to let everyone have a deeper understanding of the pointer, improve the ability of the application of pointers, the content is mostly I encountered in the actual programming problems. I believe I can give you a certain help.

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.