The easy-to-wrong point of C language pointers

Source: Internet
Author: User

1. Memory leak: The requested heap memory is not disposed.

2. Memory pollution: in front of illegal operation using memory (no error), written in the back of the error. The following code:

When the structure is only underlined part of the code, written in the compiler will not error, but at this time has caused the illegal operation of memory, the following to add some variables to the structure, this will be an error, do not let the definition of variables (this error is particularly difficult to debug) need attention.

3. The determination of an empty string and an illegal string:

The part of the picture that draws the Blue line: The value of the pointer variable should be judged, not the memory that the pointer points to

4. Pointer out of bounds: such as: str[3] = "ABC";

5. The pointer overlay will constantly change the pointer's direction.

such as: char *p = "SDFG";  p++; printf ("%s\n", p); Print result: "DFG";. The pointer only wants to be changed, if it is superimposed 4 times, it will not print the content, because the pointer is now pointing to the Terminator.

6. The address of the local variable of the stack is not to be exported. Such as:

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

7. The same memory area is released multiple times;

Har *p = null;p = (char *) malloc (n), strcpy (p, "abcdef"), if (P! = NULL) {//free () function only tells the system p points to the memory can be recycled//That is, p points to the memory use Back to the system//However, the value of P is still the original value (wild pointer), p still points to the original memory free (p);} if (P! = NULL) {free (p);}

  

The easy-to-wrong point of C language pointers

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.