In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the knowledge of "is it appropriate for C++ to initialize null pointers with NULL?". In the operation of practical cases, many people will encounter such a dilemma, 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!
Null pointer in 1.C++98
We know that in good Cmax programming habits, it is best to assign an appropriate initial value to a variable when declaring it, otherwise unexpected errors may occur.
The danger of the pointer
When you create a pointer in C++, the computer allocates memory to store the address, but not memory to store the data the pointer points to. It is a separate step that provides space for the data, and ignoring this step is undoubtedly asking for trouble, as shown below:
Int* fellow;*fellow = 1234
Fellow is indeed a pointer, but where does it point? The above code does not assign an address to fellow, so in which memory unit will 1234 be placed? We don't know. There is a fellow that has not been initialized, it may have any value. Whatever the value is, the program interprets it as an address that stores 1234. But if the value of fellow happens to be 1000, the computer will put the data on address 1000, even if this happens to be the address of the program code, and the fellow is probably not pointing to the 1234 that you want to store, and this error can lead to some of the most hidden and difficult to track bug.
So in order to avoid this problem, we all need to initialize the pointer. This is how we usually initialize the pointer.
Int main () {/ / Null pointer definition int* p1 = NULL; int* p2 = 0; return 0;}
NULL is actually a macro, and we can see the following code in the traditional C header file (stddef.h):
As you can see, NULL may be defined as a literal constant of 0 or a constant defined as an untyped pointer (void*). No matter what definition you take, you will inevitably encounter some trouble when using a pointer with a null value. For example: what is the output of the following code?
Void f (int) {cout
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.