Like Tree6Likes

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; Now, I have a problem in the creation of the tree(seg fault), which means that some modifications I made changed ...

  1. #31
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    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. :/
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  2. #32
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Quote Originally Posted by iMalc View Post
    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
    Last edited by Cat; 08-22-2013 at 10:45 PM.
    laserlight likes this.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  3. #33
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    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.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #34
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    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 Quadtree::pointInside(const Point3d& p)
    {
        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!
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  5. #35
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    Quote 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);
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #36
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    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).
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  7. #37
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    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.
    King Mir likes this.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #38
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    > 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!
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 09-06-2011, 02:59 PM
  2. bool typedef and binary compatibility
    By BattlePanic in forum C Programming
    Replies: 2
    Last Post: 05-08-2008, 08:04 AM
  3. Recursion: base case returning 1, function returning 0
    By yougene in forum C Programming
    Replies: 5
    Last Post: 09-07-2007, 05:38 PM
  4. bool function
    By MB1 in forum C++ Programming
    Replies: 10
    Last Post: 04-22-2005, 05:31 PM
  5. Object returning a bool
    By PJYelton in forum C++ Programming
    Replies: 15
    Last Post: 11-27-2002, 10:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21