How to avoid the generation of wild pointer in C language
This article introduces the knowledge of "how to avoid the generation of wild pointers in c language". Many people will encounter this dilemma in the operation of practical cases, so 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!
1. Initialize NULL time. The metric variable must be initialized to NULL.
Because when you first create it, except for static-decorated pointer variables, none of the pointer variables automatically become NULL pointers, and their default values are random.
2. Release the time setting NULL. The value of indicator p is NULL, and the memory space referred to by pointer p is freed.
Delete and free simply free up memory space, but the value of pointer p is not NULL.
In general, if statements are used to test whether the pointer is NULL to determine whether the pointer is legal.
Example
# include#includevoid main () {int * p = new int (6); printf ("before memory release delete p; if% p\ n", p); / / delete release memory delete p; if (p! = NULL) / / pointer to unknown / random memory printf ("after memory release p\ n", p); / / * p = 7 / / operate unknown / random memory address, program crashes abnormally system ("pause");} / * output: before releasing memory, p:016950E8 releases memory after p:016950E8 releases memory, press any key to continue. . This is the end of the content of "how to avoid the generation of wild pointers in c language". 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!