# Thread: Bool returning function gives me a non binary value

1. Now, I have a problem in the creation of the tree(seg fault), which means that some modifications I made changed things.

When I fix this, maybe I will get the same behavior again.

Maybe yes, maybe no, I am not sure laserlight. :/

2. Originally Posted by iMalc
You'll note that with this code it can fall off the end without returning a value. Now consider that just because it isn't going down that path, that this does not mean that the function must behave correctly when it goes down the other path.
Almost. The first sentence is certainly true, but the second sentence incorrectly asserts the code isn't going down the path leading to undefined behavior - it IS going down that path, hence why you see "entered" in the program output. The "here2" is from the SECOND call of pointInside - note that, when the if statement evaluates to false, the function calls itself recursively. It's this nested call that returns true. The outer call falls off the end, leaving an undefined return value.

Here's the code path being taken ( -> denotes a function call and <- a return; conditional expressions are replaced with the constant that they must be evaluating to):

Code:
```bool Quadtree::pointInside(const Point3d& p)
{
bool bla = pointInside(root, p);

//->  bool Quadtree::pointInside(node*& node, const Point3d& p)
{
if(false) // Is node a leaf?
{
}
std::cout<< "entered " << std::endl;
if(!node->child[7]) std::cout << "NULL" << std::cout;
pointInside(node->child[0 + (p.x() >= node->split.x())
+ 2 * (p.y() >= node->split.y())
+ 4 * (p.z() >= node->split.z())], p);

//-> bool Quadtree::pointInside(node*& node, const Point3d& p)
{
if(true) // Is node a leaf?
{
bool bla = pointInLeaf(node, p);
std::cout<< "here2 |" << bla <<"|" << std::endl;
return bla;
}
}
//<- Returns 1 (known via printout)
}
//<- Falls off the end of the method without a return; the return value is garbage

std::cout<< "here1 |" << bla <<"|" << std::endl;
return bla;
}
//<- Returns another undefined value since bla is garbage as the last function returned garbage```

3. Oh, of course that's it.
I was looking at the program output from the original post by mistake when I examined the code and did not click that there was updated output further down.

4. From cat's post
><- Falls off the end of the method without a return; the return value is garbage
Where did you see this?

Code:
```/**
* Search a leaf, in order to efficiently determine whether
* a point lies or not, inside the polytope.
* @param p - query point
* @return - answer of query
*/
{
bool a = pointInside(root, p);
std::cout<< "here1 " << a << std::endl;
return a;

}

/**
* Search a leaf, in order to efficiently determine whether
* a point lies or not, inside the polytope.
* @param node - current node
* @param p - query point
* @return - answer of query
*/
bool Quadtree::pointInside(Node*& node, const Point3d& p)
{
if(!node->child[0]) // Is node a leaf?
{
bool a = pointInLeaf(node, p);
std::cout<< "here2 " << a << std::endl;
return a;
}
std::cout<<(p.x() >= node->split.x())
+ 2 * (p.y() >= node->split.y())
+ 4 * (p.z() >= node->split.z()) << " = index\n";

pointInside(node->child[(p.x() >= node->split.x())
+ 2 * (p.y() >= node->split.y())
+ 4 * (p.z() >= node->split.z())], p);
}

/**
* Check if point p is inside(or at the edges) of the halfspaces,
* that are in the leaf.
* @param p - query point
* @return - answer of the query
*/
bool Quadtree::pointInLeaf(Node*& leaf, const Point3d& p)
{
/* If p is outside any of the half-spaces defined in the list,
* return 0 (immediately). p is inside the polytope only
* if it is inside all the half-spaces relevant to this box. */

if(leaf->relevantHalfspaces.size() == 1 && leaf->relevantHalfspaces.at(0) == -1) // All halfspaces have to be checked
{
for(size_t i = 0 ; i < P->getH() ; ++i)
// Dot product of the hyperplane and the point > min distance to origin
if( p * P->getHalfspaces().at(i).getCoords() > P->getHalfspaces().at(i).getD() )
return false;
return true;
}

std::cout<< "inPointLeaf\n";
for(std::vector<int>::const_iterator it = leaf->relevantHalfspaces.begin() ; it != leaf->relevantHalfspaces.end(); ++it)
// Dot product of the hyperplane and the point > min distance to origin
{cout<<"p is " <<p << endl;
cout<<P->getHalfspaces().at(*it).getCoords()<<" and " <<P->getHalfspaces().at(*it).getD()<<endl;
if( p * P->getHalfspaces().at(*it).getCoords() > P->getHalfspaces().at(*it).getD() )
return false;
}
return true;
}```
Now, I tested with an octree that I am sure it has 9 nodes, everyone without garbage and I got this
Code:
```5 = index
inPointLeaf000
inPointLeaf
p is 1.100000000000000,0.500000000000000,0.700000000000000

-0.000000000000000,-0.000000000000000,-1.000000000000000
and -0.000000000000000
p is 1.100000000000000,0.500000000000000,0.700000000000000

0.000000000000000,-1.000000000000000,0.000000000000000
and 0.000000000000000
p is 1.100000000000000,0.500000000000000,0.700000000000000

-1.000000000000000,-0.000000000000000,-0.000000000000000
and -0.000000000000000
here2 1
here1 32
32
Polytope's destructor called!```

5. Originally Posted by std10093
From cat's post
><- Falls off the end of the method without a return; the return value is garbage
Where did you see this?
At the end of this function:
Code:
```/**
* Search a leaf, in order to efficiently determine whether
* a point lies or not, inside the polytope.
* @param node - current node
* @param p - query point
* @return - answer of query
*/
bool Quadtree::pointInside(Node*& node, const Point3d& p)
{
if(!node->child[0]) // Is node a leaf?
{
bool a = pointInLeaf(node, p);
std::cout<< "here2 " << a << std::endl;
return a;
}
std::cout<<(p.x() >= node->split.x())
+ 2 * (p.y() >= node->split.y())
+ 4 * (p.z() >= node->split.z()) << " = index\n";

pointInside(node->child[(p.x() >= node->split.x())
+ 2 * (p.y() >= node->split.y())
+ 4 * (p.z() >= node->split.z())], p);
}```

6. But this function, at the end, calls itself and.......oh!! I have missed the keyword return!

Bravo cat, you are a real cat! (I do not know if that phrase holds true in English).

7. Oh, you mean you didn't already know it was falling off the end from when I mentioned it on the previous page?
I thought it was pretty clear given the last line is not a return statement. I guess it's one of those things where if you wrote the code yourself then you see what you expect to see, which it is one reasons why code reviews are so useful.

8. > I guess it's one of those things where if you wrote the code yourself then you see what you expect to see, which it is one reasons why code reviews are so useful.
I am not sure what the 1st part means, but yes, code reviewing is so useful. When I was writing the code, I totally missed that!