Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
263 views
in Technique[技术] by (71.8m points)

pointers - What happens we create a nullptr and use it in a IF condition in C++

Hi i am trying to see what happens when i create a nullptr in C++ and use it in a if statement. For example, my code is as following: int *p=nullptr; if(p){cout<<"Inside IF";}

My questions are as follows:

  1. What happens when we create a nullptr? I mean does a new pointer object is created that holds the value 0(as address) or something else? And if the address it holds is 0 then is that pointer pointing to a possibly valid address in the memory?
  2. What is happening when we use it in the IF statement ? I mean is the IF statement checking whether the value that the pointer object holds is 0 or something else?
question from:https://stackoverflow.com/questions/66058016/what-happens-we-create-a-nullptr-and-use-it-in-a-if-condition-in-c

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  1. What happens when we create a nullptr?

You can create a null pointer, but not a nullptr. The keyword nullptr denotes a literal value, not something that can be created.

Based on your code, you meant to ask what happens when a pointer variable is assigned nullptr. The answer to that is that your variable becomes a null pointer, a pointer that points to nothing and that evaluates to false when used in a boolean context.

I mean does a new pointer object is created that holds the value 0(as address) or something else?

We usually think of the value of a null pointer as being zero, but technically an implementation could use a bit pattern that has some non-zero bits. As long as you do not look too closely at the compiled code, go ahead and think of null pointers as holding the address 0.

And if the address it holds is 0 then is that pointer pointing to a possibly valid address in the memory?

The value of a null pointer must be distinct from all potentially valid memory addresses. If 0 is possibly a valid memory address in an implementation, then that implementation must use a different bit pattern to represent null pointers. (I am not aware of any implementations that fall into this category.)

  1. What is happening when we use it in the IF statement ?

The condition of an if statement is contextually converted to bool. When converting to bool from a pointer, the null pointer value converts to false and all other values convert to true.

I mean is the IF statement checking whether the value that the pointer object holds is 0 or something else?

Something else (but only by a fine shade of meaning). It checks whether the value is the null pointer value (which is almost certainly represented by zero bits on your computer, but technically not guaranteed to be so).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...