C-language pointer error-prone

Source: Internet
Author: User

C-language pointer error-prone

1. Memory leakage: the requested heap memory is not released.

2. Memory pollution. The following code:

If the struct contains only some underlined code, no error will be reported when it is written in the compiler, but the memory operation has already been illegal. The following will add some variables to the struct, and an error will be reported, do not allow variable definition (this error is especially difficult to debug.

3. null and invalid strings:

The blue line in the figure: the value of the pointer variable should be determined, rather than the memory pointed by the pointer.

4. pointer out of bounds: for example, str [3] = "abc ";

5. the superposition of pointers will constantly change the pointer direction.

For example, char * p = "sdfg"; p ++; printf ("% s \ n", p); printed result: "dfg ";. The pointer only needs to be changed. If it is superimposed four times, no content will be printed, because the pointer has already pointed to the Terminator.

6. Do not upload the address of the local variable in the stack area. For example:

Char * get_str () {char str [] = "abcdedsgads"; // stack, printf ("[get_str] str = % s \ n", str ); return str ;}

7. The same memory area is released multiple times. For example:

Har * p = NULL; p = (char *) malloc (50); strcpy (p, "abcdef"); if (p! = NULL) {// the function of the free () function only tells the system that the memory pointed to by p can be recycled. // That is to say, the right to use the memory pointed by p is returned to the system. // However, the value of p is still the original value (wild pointer), and the value of p points to the original memory free (p);} if (p! = NULL) {free (p );}

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.