2

I wrote a little test to check for null pointer, I simplified it with int and 0, 1, instead of real classes, what I'm trying to test is something like this: return p ? 1 : 0; which in real world would be return p ? p->callmethod() : 0;

bool TestTrueFalse();
void main()
{
  int i = TestTrueFalse();

}

bool TestTrueFalse()
{  
    int one = 1;
    int * p =&one;    
    *p = 0;  

    return p ? 1 : 0;
}

now, you can see, that once the pointer becomes 0 again, the test fails, why? what's wrong with this? what's the solution?

ra170
  • 3,643
  • 7
  • 38
  • 52
  • i am very confused by the question and the code. if you want to check for a null pointer, `if(p == 0) { ... }` should be good enough, right? Your code does not use consistent data types (bool vs. int) and it just doesn't make much sense either. – tenfour Aug 25 '10 at 15:13
  • I don't see how the test can fail, you never set the pointer to null. The test should succeed. – CB Bailey Aug 25 '10 at 15:13
  • 1
    @tenfour: "if(p == 0) { ... } should be good enough, right?" `if(!p){...}` should be good enough... – SigTerm Aug 25 '10 at 15:18
  • bool shouldn't be treated as integer. You should return true/false, not 1/0 – SigTerm Aug 25 '10 at 15:20
  • @SigTerm: While I agree with you, it is purely a point of style. C++ has a well defined conversion from integers to `bool` so there's nothing technically wrong with returning `1` and `0`. – CB Bailey Aug 25 '10 at 17:28
  • @Charles: While it's a point of style, I think it a significant one. While this confusion won't cause the compiler to do anything unexpected, mixing `bool` and `int` doesn't work in general, even if limited to values of 0 and 1. – David Thornley Aug 25 '10 at 19:27

6 Answers6

4
*p = 0;  

you probably meant

p = 0;

*p = 0 sets what the pointer points to, not the pointer

AndersK
  • 35,813
  • 6
  • 60
  • 86
1

When testing a pointer value with a conditional in C++, it will return true if the value is non-zero and false if the value is 0. In your sample p is slated to point at the local one and hence has a non-zero address (even though the value at the address is 0). Hence you get true

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
1

A null pointer is a pointer which points to the address 0, not the value 0.

To set a pointer to null, do:

p = 0;

To elaborate, your code sets the pointed-to-int to 0. For example:

int i = 1;
int *p = &i;
assert(*p == 1); //p points to 1

*p = 0;
assert(*p == 0 && i == 0); //p points to the same location, but that location now contains 0
jA_cOp
  • 3,275
  • 19
  • 15
1

The code *p = 0; does not set the pointer to null. It sets what p is pointing to zero.

Starkey
  • 9,673
  • 6
  • 31
  • 51
0

A pointer is an address in memory. int *p = &one; takes the address of the variable one, and stores it in p. *p = 0; stores 0 in the memory pointed to by p, meaning that the value of one is now 0. So, you have changed what p points to, but not p itself. TestTrueFalse() will return 1.

Dima
  • 38,860
  • 14
  • 75
  • 115
0

to test it for a null pointer before inspecting the value pointed to you might use code like

if(ip != NULL)

taken from http://www.eskimo.com/~scs/cclass/notes/sx10d.html

NULL might be safer in your code, as it is more compiler independent than just writing 0. and it might also be more clear for others to read in your code.

a1337q
  • 770
  • 3
  • 10
  • 20
  • this might also be useful here: http://stackoverflow.com/questions/551069/testing-pointers-for-validity-c-c – a1337q Aug 25 '10 at 19:23
  • NULL, in C++, is required to be 0. Any compiler that treats the two at all differently is not a C++ compiler. The advantage of NULL is that it signifies that it's supposed to be a pointer value, but the disadvantage is that it can be misleading. (I'm really looking forward to `nullptr` in the next C++ standard.) – David Thornley Aug 25 '10 at 19:29