# Bool returning function gives me a non binary value

This is a discussion on Bool returning function gives me a non binary value within the C++ Programming forums, part of the General Programming Boards category; I just started testing my program and I expect -of course- that there are some logical errors I have to ...

1. ## Bool returning function gives me a non binary value

I just started testing my program and I expect -of course- that there are some logical errors I have to truck down.

So, pointInLeaf returns a bool, which is then returned by pointInside and finally returned by another function (which is the named pointInside, but overloaded with one argument and public scope, while the others are of private scope. All functions belong to same class).

Code:
```bool Quadtree::pointInside(const Point3d& p)
{
bool bla = pointInside(root, p);
std::cout<< "here1 |" << bla <<"|" << std::endl;
return bla;
}

bool Quadtree::pointInside(node*& node, const Point3d& p)
{
if(!node->child[0]) // Is node a leaf?
{
bool bla = pointInLeaf(node, p);
std::cout<< "here2 |" << bla <<"|" << std::endl;
return bla;
}
...
}```
and look at my output
Code:
```here2 |1|
here1 |208|```
The value 208 varies. Before it was 76.

There is probably something wrong in the algorithm(just started to test the program), but this does not explain this.

EDIT:

2. Code:
`...`
What happens when this section executes ?

3. The function is sure to go into the body of this loop, for the tests I am performing now.

I should however mention that, when the program performs tests that I already know that they work, it goes as it should be, giving back a binary value.

4. Even if pointInLeaf returns an int converted to bool and is inlined, the result should still be a bool and printed as such. I'm not sure if a bool must be printed as 0 or 1 though, and unfortunately that is a part of the standard that I find a bit more challenging to interpret, heh.

Therefore, my best guess is that you have undefined behaviour in your program that lies in code that you did not show.

5. Does this give any unexpected results?

Code:
```#include <iostream>

bool foo();
bool bar();

bool foo()
{
bool poop = bar();
std::cout << "foo() " << poop << std::endl;
std::cout << "foo() direct" << bar() << std::endl;
return poop;
}

bool bar()
{
bool poop;// = 208;
std::cout << "bar() "<< poop << std::endl;
return poop;
}

int main(void)
{
foo();
return 0;
}```

6. I've always thought a bool is one of two values zero or not zero.
From the C++11 working draft

Document Date: 2012-02-28 Number: N3376
Section 4.12
A prvalue of arithmetic, unscoped enumeration, pointer, or pointer to member type can be converted to a
prvalue 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. A prvalue of type std::nullptr_t can be converted to a prvalue of
type bool; the resulting value is false.
It looks like you should be testing/printing true/false not the integral value. When printing you should probably set the boolalpha manipulator to print true or false instead of the integral value.

Jim

7. Originally Posted by jimblumberg
I've always thought a bool is one of two values zero or not zero.
No, what you quoted are the rules for conversion to bool, not the rules as to the valid values of bool, or how a bool value should be printed. Refer to clause 4.7 instead for how a bool would be converted to an integer type... but that still does not cover precisely how a bool would be printed.

8. I'm having trouble locating the relevant section in the standard, but I suspect that the values for bool, beyond what the standard says in 4.12, are implementation defined.

9. Originally Posted by Elkvis
I'm having trouble locating the relevant section in the standard, but I suspect that the values for bool, beyond what the standard says in 4.12, are implementation defined.
2.14.6 Boolean literals
3.9.1 Fundamental types p. 6
4.5 Integral promotions p. 6

All suggest bool must be either true (1) or false (0)

But
48) Using a bool value in ways described by this International Standard as “undefined,” such as by examining the value of an
uninitialized automatic object, might cause it to behave as if it is neither true nor false.

10. Originally Posted by laserlight
No, what you quoted are the rules for conversion to bool, not the rules as to the valid values of bool, or how a bool value should be printed. Refer to clause 4.7 instead for how a bool would be converted to an integer type... but that still does not cover precisely how a bool would be printed.
True but Section 4.7 doesn't really tell you much IMO.
Section 4.7 Integral conversions.
If the destination type is unsigned, the resulting value is the least unsigned integer congruent to the source
integer (modulo 2 where n is the number of bits used to represent the unsigned type). [ Note: In a two’s
complement representation, this conversion is conceptual and there is no change in the bit pattern (if there
is no truncation). — end note ]

If the destination type is signed, the value is unchanged if it can be represented in the destination type (and
bit-field width); otherwise, the value is implementation-defined.

If the destination type is bool, see 4.12. If the source type is bool, the value false is converted to zero and
the value true is converted to one.
The conversions allowed as integral promotions are excluded from the set of integral conversions.
So it seems important to know if bool is signed or unsigned, and how many bits the bool can contain. Depending on how bool on the system in question is defined the conversion from an int to a bool could lead to an implementation defined value.

Jim

11. Originally Posted by SirPrattlepod
2.14.6 Boolean literals
3.9.1 Fundamental types p. 6
4.5 Integral promotions p. 6

All suggest bool must be either true (1) or false (0)
thanks for the pointers. I only have the 2011 draft standard, not the final standard, but in 4.5 p6, it says "A prvalue of type bool can be converted to a prvalue of type int, with false becoming zero and true
becoming one." so it appears that the values of true and false are very clearly and well-defined. however it also appears the value of an uninitialized bool object still remains undefined. the size of storage assigned to an object of type bool appears to be implementation-defined. on some compilers, it may be 1 byte, while on others, it may be equal to the size of an int, or any other variation that allows a set of two values.

12. Originally Posted by Elkvis
thanks for the pointers. I only have the 2011 draft standard, not the final standard, but in 4.5 p6, it says "A prvalue of type bool can be converted to a prvalue of type int, with false becoming zero and true
becoming one." so it appears that the values of true and false are very clearly and well-defined. however it also appears the value of an uninitialized bool object still remains undefined. the size of storage assigned to an object of type bool appears to be implementation-defined. on some compilers, it may be 1 byte, while on others, it may be equal to the size of an int, or any other variation that allows a set of two values.
I agree.

13. Sirplattelbot, that's not the case. These are uninitialized variables!

Here is the function's missing part, which, eventually is executed once.
Code:
```bool Quadtree::pointInside(node*& node, const Point3d& p)
{
if(!node->child[0]) // Is node a leaf?
{
bool bla = pointInLeaf(node, p);
std::cout<< "here2 |" << bla <<"|" << std::endl;
return bla;
}
std::cout<< "entered " << std::endl;
if(!node->child[7]) std::cout << "NULL" << std::cout; // This is NOT null
pointInside(node->child[0 + (p.x() >= node->split.x())        // The index results to 7
+ 2 * (p.y() >= node->split.y())
+ 4 * (p.z() >= node->split.z())], p);
}```
output
Code:
```entered
here2 |1|
here1 |24|
24            // that's the output from main function```
So, our result from this, is that the when printed, the bool value is not guaranteed to be binary?

14. is it possible that pointInLeaf is returning an uninitialized bool?

15. Thought I should mention this earlier. Sorry.
No, pointInLeaf, returns true or false only. You can see that the cout in line 6, gives as a value equal to one.

Page 1 of 3 123 Last