valid or invalid pointer

Sep 27, 2013 at 4:34am
Hello Forum,

How do we distinguish between the valid and invalid pointer ?

Check the following code
1
2
3
4
5
6
7
void func(int *i)
{
   if(i)
   {
       // does it mean that i is valid 
   }
}


So if we check for if(i != NULL) , does it mean that the pointer is valid ?

Any thoughts ?


Regards
Sajjadul
Sep 27, 2013 at 5:02am
So if we check for if(i != NULL) , does it mean that the pointer is valid ?


No. It means the pointer is not NULL. In general, there is no way to check if a pointer is "valid" so you should avoid situations where you have invalid pointers hanging around.
Sep 27, 2013 at 6:17am
Semantically, the null pointer is the only pointer that with certainty is always "valid". Not the "safe to dereference" kind of valid though.
Topic archived. No new replies allowed.