Jump to content
  • Advertisement
Sign in to follow this  
ehmdjii

when are pointers true or false

This topic is 4595 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

int * i;

if (i)
{
//true
}

can a pointer be evaluated to true or false? when is it true and when is it false? what if it hasn't been assigned, like in the example? what if it has? what about the NULL - pointer? thanks1

Share this post


Link to post
Share on other sites
Advertisement
A pointer is 'false' if it is NULL (0), otherwise it is 'true'. But you should never use uninitialized pointers since they'll automatically point to a random location in memory. Messing with random memory is a bad idea and usually ends in a crash.

So remember to always initialize your pointers (to 0 if nothing else)!

Share this post


Link to post
Share on other sites
Values interpreted in a boolean context are true when non-zero and false when zero. NULL is just a preprocessor macro that evaluates to zero. Thus a 'NULL' pointer will be false, and any other pointer will be true.

Share this post


Link to post
Share on other sites
Quote:
C++ Standard (Final Draft), Section 4.12, Paragraph 1
An rvalue of arithmetic, enumeration, pointer, or pointer to member type can be converted to an rvalue of type bool. A zero value, null pointer value, or null member pointer value is converted to false; any other value is converted to true.

Enigma

Share this post


Link to post
Share on other sites
thank you all!

so when i leave the pointer uninitialized like in the above example, it will still evaluate to true, cause it points to random memory?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by Fruny
Values interpreted in a boolean context are true when non-zero and false when zero. NULL is just a preprocessor macro that evaluates to zero. Thus a 'NULL' pointer will be false, and any other pointer will be true.

AP

Share this post


Link to post
Share on other sites
The chance that some random garbage is in memory is higher
than that you accidentally hit a memory location with '0'/false..

So it will be true with high probability.

But like the other posters said, having uninitialized pointers
and doing sth with them is just asking for problems.

Share this post


Link to post
Share on other sites
if you set a pointer to Null it points to nothing.
So if you have set (pseudocode)

pointer1 = Null

if pointer1 = Null then Dosomething
then it will execute Dosmething.
You cannot set pointer to boolean values.

Share this post


Link to post
Share on other sites
Quote:
Original post by Fruny
Values interpreted in a boolean context are true when non-zero and false when zero. NULL is just a preprocessor macro that evaluates to zero. Thus a 'NULL' pointer will be false, and any other pointer will be true.
As far as I can tell, according to A working paper containing the Current ISO C standard incl TC1 and TC2[PDF] (on JTC1/SC22/WG14 - C), your last two statements aren't neccessarily true if the language in question is C (and the same likely applies to C++, but I haven't checked). I don't see any specification that indicates a 'null pointer constant' stored in a pointer will convert back to 0. It is specified that an "integer constant expression with the value 0" is a 'null pointer constant', but it seems that once it is converted to a pointer type, there is no guarantee that it can be converted back to the same value. Unless I'm missing something, conversion between pointers and integers is allowed, but the behavior is implementation-defined.

Of course, that paper isn't the actual current standard, but I think it is mostly the same, and what differences exist are things being considered for the next standard since.

Also, the behavior you describe is true on every system I've worked on or heard of, but it doesn't seem to be covered by the C standard. I doubt that C++ specifies the behavior either, because if it did and it was an actual 'hole' in the C standard it seems likely somebody would have noticed the difference and fixed it.

Share this post


Link to post
Share on other sites
Quote:
Original post by Extrarius
Quote:
Original post by Fruny
Values interpreted in a boolean context are true when non-zero and false when zero. NULL is just a preprocessor macro that evaluates to zero. Thus a 'NULL' pointer will be false, and any other pointer will be true.
As far as I can tell, according to A working paper containing the Current ISO C standard incl TC1 and TC2[PDF] (on JTC1/SC22/WG14 - C), your last two statements aren't neccessarily true if the language in question is C (and the same likely applies to C++, but I haven't checked). I don't see any specification that indicates a 'null pointer constant' stored in a pointer will convert back to 0.

I don't think this matters. [smile]

The standard specifies that "if (x) y" will execute y if "x compares equal to 0".

The rules for equality state that "if one operand is a null pointer constant, it is converted to the type of the other operand". Whilst x might not be a null pointer constant, 0 is. Hence, 0 is converted to a null pointer of the same type as x, and comparison proceeds as usual for pointer types.
Quote:

It is specified that an "integer constant expression with the value 0" is a 'null pointer constant', but it seems that once it is converted to a pointer type, there is no guarantee that it can be converted back to the same value. Unless I'm missing something, conversion between pointers and integers is allowed, but the behavior is implementation-defined.

Of course, that paper isn't the actual current standard, but I think it is mostly the same, and what differences exist are things being considered for the next standard since.

Also, the behavior you describe is true on every system I've worked on or heard of, but it doesn't seem to be covered by the C standard. I doubt that C++ specifies the behavior either, because if it did and it was an actual 'hole' in the C standard it seems likely somebody would have noticed the difference and fixed it.

C++ converts the result of boolean expressions to "bool" type before testing them. The conversion from pointer to Boolean is fully defined. So there's no problem in C++.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!