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

Threaded View

Previous Post Previous Post   Next Post Next Post
  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

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, 12:44 PM
  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, 06:36 AM
  4. Replies: 8
    Last Post: 01-30-2011, 02: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