Reputation: 1
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:
Upvotes: 1
Views: 1855
Reputation: 17072
- 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.)
- 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).
Upvotes: 2
Reputation: 5585
For reference:
nullptr
, the null pointer literalstd::nullptr_t
, the type of nullptr
bool
conversions.The line
int *p=nullptr;
Creates an int*
pointer-to-int
with automatic storage duration. Then nullptr
is assigned to it, and is automatically converted to the null pointer value for the int*
pointer. It's not actually necessarily the case that this value is stored internally as 0
, although there's an implicit conversion from the integer literal 0
to the null pointer value for every pointer type.
Then, in the comparison
if(p){cout<<"Inside IF";}
There's an implicit conversion from every pointer type to bool
, which is applied in this if
statement. Specifically:
Boolean conversions
The value zero (for integral, floating-point, and unscoped enumeration) and the null pointer and the null pointer-to-member values become
false
. All other values becometrue
.
So p
has the value of the null int*
pointer, and therefore becomes false
.
Upvotes: 2
Reputation: 127
What you said is right nullptr allocates location and gives value 0 and 0 is false in c++
#include <iostream>
using namespace std;
int main() {
int *p=nullptr;
if(p){cout<<"Inside IF";}
cout<<&p<<endl;
cout<<p;
}
Output
0x7ffc92b66710 0
Upvotes: 2