Alan
Alan

Reputation: 1

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?

Upvotes: 1

Views: 1855

Answers (3)

JaMiT
JaMiT

Reputation: 17072

  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).

Upvotes: 2

Nathan Pierson
Nathan Pierson

Reputation: 5585

For reference:

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 become true.

So p has the value of the null int* pointer, and therefore becomes false.

Upvotes: 2

Rahul Khanna
Rahul Khanna

Reputation: 127

I hope this answers your Question about what is actually happening

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

Related Questions