Like Tree2Likes
  • 1 Post By Elysia
  • 1 Post By iMalc

passing a vector to a function, getting negative length and size, aka segault

This is a discussion on passing a vector to a function, getting negative length and size, aka segault within the C++ Programming forums, part of the General Programming Boards category; Hello, I am writing a raytracer, and currently I'm working on creating a bounding volume hierarchy to accelerate the process. ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    44

    passing a vector to a function, getting negative length and size, aka segault

    Hello,

    I am writing a raytracer, and currently I'm working on creating a bounding volume hierarchy to accelerate the process. To do this, I am first creating a vector that holds each of the objects in the scene, and passing this vector to the constructor for my BVH.
    Code:
    //in header
    BVH_Node* bvh;
    //in main raytrace function
    bvh = new BVH_Node(objList,0);
    The BVH_Node constructor:
    Code:
    //here's how the beginning of it looks
    BVH_Node::BVH_Node(vector<GeomObj*> list, int axis){
      int size = list.size(); //number of elements in list
      this->isObj = false;
      if(size == 1){
        this->left = makeLeaf(list[0]);
        this->right = makeLeaf(NULL);
        this->bb = list[0]->bb;
        return;
      }
      if(size == 2){
        this->left = makeLeaf(list[0]);
        this->right = makeLeaf(list[1]);
        this->bb = combineBoxes(list[0]->bb,list[1]->bb);
        return;
      }
    //the rest of it
    }
    I am testing a scene that has only 2 objects, and so it goes to the size == 2 check. The first time it hits makeLeaf(), I segfault. I've used both gdb and valgrind, and of course it's a memory mapping error. gdb's backtrace tells me that the length of the vector I've passed in is -805305610 and the capacity is -21, and that it is inside my makeLeaf() function that the error occurs. Here's the function:
    Code:
    BVH_Node* BVH_Node::makeLeaf(GeomObj* v){
      BVH_Node* node;
      node->obj = v;
      node->isObj = true;
      return node;
    }
    The segfault happens at
    Code:
    node->obj = v;
    Could someone help me understand why this is happening? If I run my raytracer without a BVH, the objList works perfectly.

    Thanks
    Last edited by synhyborex; 05-22-2013 at 03:34 PM. Reason: formatting

  2. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,406
    its hard for me to say given the limited code, and my knowledge - but dont you want to pass a reference to a vector into your constructor
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    44
    If you would like, I could post more code. Just let me know what you need to see. Also, I don't believe I'm passing a reference of my vector into my constructor, but rather the object itself. Is this an incorrect understanding of what I'm doing?

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,147
    I don't believe I'm passing a reference of my vector into my constructor, but rather the object itself.
    O_o

    You misunderstood; that was the way rogster001 chose to tell you to pass the `std::vector' by reference.

    Also, if it crashes where you say, the problem isn't with this code; look into the function that populates the `std::vector' object.

    Soma

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    44
    Ok, I think I got it. Would it be beneficial to change the prototype of the constructor to take a const vector<GeomObj*>& rather than the way I've done it?

    Also, I just use the given std::vector.push_back() to populate the vector. As you can guess, I'm pushing back GeomObj* objects. Is there some way that the pointers or my data could have changed while being passed through the constructor or to makeLeaf() (I realize this is unlikely)? Like I said before, until I started to try and implement this BVH, my object list vector was giving me no problems at all.

    Sorry, I'm not the greatest with all this memory stuff. I appreciate the help.
    Last edited by synhyborex; 05-22-2013 at 06:44 PM.

  6. #6
    Registered User
    Join Date
    Feb 2011
    Posts
    44
    Reanalyzing my code, I've realized that I'm not properly allocating memory for my left and right nodes. Here's the class definition of a BVH_Node:
    Code:
    class BVH_Node{
      public:
        BVH_Node();
        BVH_Node(const vector<GeomObj*>&,int);
        ~BVH_Node();
        BVH_Node* left; //left child
        BVH_Node* right; //right child
        BBox bb; //bounding box
        bool isObj; //leaf of tree or not
        GeomObj* obj; //the object itself
    
        //functions
        BBox combineBoxes(BBox,BBox);
        BVH_Node* makeLeaf(GeomObj*);
        bool intersect(vec3,vec3,BVH_Node*);
        static bool compareByX(GeomObj*,GeomObj*);
        static bool compareByY(GeomObj*,GeomObj*);
        static bool compareByZ(GeomObj*,GeomObj*);
    };
    I malloc'd some space in my constructor, and everything is dandy. Don't know why this didn't occur to me earlier. Now I just need to work on the rest of the bugs with the tree itself

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    Do not use malloc (it will not call constructors)! Use new and delete, but even better is to not use it at all! Why do you need new/delete/malloc/free anyway?
    For good measurement, some good advice:
    - Do not remove parameter names - cpwiki

    Questions:
    - Do you need to implement your own tree? Won't std::set or std::map suffice?
    Last edited by Elysia; 05-22-2013 at 10:32 PM.
    phantomotap likes this.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,147
    Don't use malloc (it will not call constructors)!
    [Edit]
    O_o
    [/Edit]

    Indeed.

    Do you need to implement your own tree? Won't std::set or std::map suffice?
    The tree is specialized for scene/object relationships.

    The algorithms depends on navigating the tree not simply having access to the nodes.

    You could probably force `std::set' to do the job by using keys related to how the scene is arranged, but that would probably be more work than just implementing the tree.

    This may be one of those times when implementing a thing is the right to go.

    Soma

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,288
    Quote Originally Posted by synhyborex View Post
    Code:
    BVH_Node* BVH_Node::makeLeaf(GeomObj* v){
      BVH_Node* node;
      node->obj = v;
      node->isObj = true;
      return node;
    }
    The segfault happens at
    Code:
    node->obj = v;
    Could someone help me understand why this is happening?
    I would have thought there would be no mystery here.
    You're declaring a pointer, leaving it uninitialised, and then immediately dereferencing it. Of course that wont work.
    Elysia 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"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. size and length of the string doubt
    By Satya in forum C Programming
    Replies: 5
    Last Post: 02-23-2012, 11:44 AM
  2. passing a vector to a function
    By Farnaz in forum C++ Programming
    Replies: 4
    Last Post: 06-02-2011, 07:21 AM
  3. Error while passing a reference of a vector to a function.
    By manasij7479 in forum C++ Programming
    Replies: 3
    Last Post: 02-19-2011, 05:36 AM
  4. Replies: 8
    Last Post: 01-30-2011, 01:55 PM
  5. Passing Variable-Length Array to a Function
    By murjax in forum C Programming
    Replies: 4
    Last Post: 07-24-2009, 09:36 PM

Tags for this Thread


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