Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to avoid the generation of wild pointer in C language

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report