Summarized some common pointer error-prone issues (6) and common pointer Problems

Source: Internet
Author: User

Summarized some common pointer error-prone issues (6) and common pointer Problems

Misuse of security issues and pointers

Declaration and initialization pointer (initialization failed)

Misuse pointer

Release Problems

Pointer declaration:

// Macro definition (only replacement)

# Define PINT *;

PINT ptr1, ptr2; is actually defining int * ptr1, ptr2; a pointer, an integer constant.

// Typedef name the existing data type (better than macro definition)

Typedef int * PINT;

PINT ptr1, ptr2;

Differences between macro definition and typedef: differences between c/c ++ typedef and # define

// Difference between macro definition and typedef
# Include <stdio. h> # define INTPTR1 int * int main () {typedef int * INTPTR2; int a = 1; int B = 2; int c = 3; const INTPTR1 p1 = &; // pointer constant. The value pointing to the variable cannot be modified, but it can be changed to another variable const INTPTR2 p2 = & B; // constant pointer, read-only, and the value pointing to the variable can be modified. INTPTR2 const p3 = & c; // constant pointer, read-only, you can modify the value pointing to the variable. Printf ("% d \ n", * p1, * p2, * p3 ); printf ("a = % d B = % d c = % d \ n", a, B, c); p1 = & c; * p2 = 9; * p3 = 10; printf ("% d \ n", * p1, * p2, * p3 ); printf ("a = % d B = % d c = % d \ n", a, B, c); return 0 ;}

Pointer not initialized before pointer usage (wild pointer)

Handle Uninitialized pointers (you cannot only check the pointer content to determine whether it is valid)

Always use NULL to initialize pointer

Use the assert function (used to test whether the pointer is null); assert (pi! = NULL );

If it is null, output: Assertion failed: pi! = NULL;

Use third-party tools

Pointer usage Problems

Several causes of buffer overflow:

Index value not checked when accessing array elements

Be careful when performing pointer arithmetic operations on Array pointers

Use functions such as gets to read strings from quasi-inputs on the other side

Misuse of functions such as strcpy and strcat;

Test NULL

When using functions such as malloc, you must check the return value. Otherwise, the program may be terminated abnormally.

Lost pointer (after releasing the pointer, but still referencing the original memory, it will generate lost pointer)

Memory accessed beyond the array Boundary

The address calculated by subscript does not check the index value.

Incorrect array Length Calculation

When passing an array to a function, you must pass the array length at the same time. This information helps the function avoid crossing the array boundary.

#include<stdio.h>#include<stdlib.h>#include<string.h>void replace(char buffer[],char replacement,size_t size){    size_t count = 0;    while(*buffer!=NULL && count++ <size){        *buffer = replacement;        buffer++;    }}int main(){    char name[8];    strcpy(name,"Alexander");    replace(name,'+',sizeof(name));    printf("%s\n", name);    printf("%d\n", sizeof(name));}

Incorrect sizeof Operator

#include<stdio.h>#include<stdlib.h>#include<string.h>int main(){    int name[20];        int *pbuffer= name;    for(int i=0;i<sizeof(name)/sizeof(int);i++)       {        *(pbuffer++)=0;    }    printf("%d\n", sizeof(name));} 

Must match the pointer type

It is a good idea to always use the appropriate pointer type to install a sentence.

Bounded pointer

Bounded pointers are restricted to valid areas. For example, there is an array of 32 elements, and the pointer used for this array is not allowed to access any memory before or after the array.

String security issues

String-related security issues generally occur when data is written beyond the end of the string.

Pointer arithmetic operations and struct

Only pointer arithmetic operations are performed on the array, because the array must be allocated to the continuous memory block, and the pointer arithmetic operation can get a valid offset. However, they should not be used in the structure Because struct fields may be allocated in discontinuous memory areas.

Function pointer Problems

Memory release Problems

Repeated release

  

Clear sensitive data (clear sensitive data before releasing the memory)

Summary:

Related Article

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.