Tutorial :bus error/segmentation fault for std::vector declaration


I have the following data structure declaration on which I make the following operation:

//QSweep.cpp   void QSweep:: createLoops(){      std:: vector< int > orientationUE;   if (test1)       orientationUE.push_back(1);   else       orientationUE.push_back(-1);  

Everything works fine so far. The problem is that I have several files and I want orientationUE to be a data of the class QSweep not only a local variable in the method createLoops(). So I added the declaration of the variable orientationUE in the file QSweep.h

 //QSweep.h   class QSweep{      ....      std::vector< int > orientationUE;      ....   }     //QSweep.cpp   void QSweep:: createLoops(){      if (test1)       orientationUE.push_back(1);      else       orientationUE.push_back(-1);   }  

In this case I obtain either a bus error message or a segmentation fault message depending on where I declared my variable orientationUE in the

  class QSwepp{    ....    }  

I do not understand exactly what happens.

ADD: Yes I have several memory allocation and several other declaration of variables of type vector (actually a lot). E.g.:

typedef Seq< vector<int> > MxInt2d;  class QSweep  {     public:    QSweep(){};    QSweep(const MxDouble2d& myPoints, const MxInt2d& myEdges);    void intersection();      //data and functions for inserting the missing edges in the graph    void completeGraph();         MxInt2d myEFCG;    std::vector< int > vNoClone;    std::vector< int > vEdge;      ....      int nbIntersections;    MxInt2d sweepEvents;    std::vector<double*> myIntersections;  };  

Still I have applied a lot of algorithms on these data types and I get the results without no segmentation fault or bus error message. So you think the error is somewhere in the other memory allocation jobs. Uff.. I was afraid of that. Still I do not understand the good results, they are misleading though? And how can I detect exactly the error in this case?


The vector myIntersections is created in the following way:

 double* QSweep::computeIntersection(double m1, double b1, double m2, double b2){  double* v=new double[3];    v[0]= (b2-b1)/(m1-m2);  v[1]= (m1*b2-m2*b1)/(m1-m2);  v[2]=0.00;    return v;   }       double* QSweepComplete::findIntersection(edge_t edge1, edge_t edge2){      double* vector=NULL;            if (test2){               return NULL;          }          else{            vector=computeIntersection(m1,b1,m2,b2);            return vector;          }              }     void QSweep:: intersection(){     double* vector=NULL;     vector=findIntersection(edge1,edge2);     if (vector!=NULL){       myIntersections.push_back(vector);     }   }  

When I display the vector myIntersections everything is okay, plus that I always get the right size: if I have 5 intersections, 5 elements in the vector and so on. For me it looks quite clean, but unfortunately probably it isn't :(

ADD_LAST (MAYBE SOLVED): Hey everybody!

It seems the problem was the data

    int nbIntersections;  

which I only initialized it locally in a function in QSweep.cpp; Still, I am using a lot of vectors of the same size with nbIntersections, so the variable in some case contained some nonexistent data. So I added to the constructor and initialized it with 0, and now it seems I have no problems anymore with the segmentation fault or bus error messages. I hope this was the mistake.


It's nothing to do with the code you have posted. The problem must lie elsewhere. Do you do any dynamic memory allocation? If so, it is probably screwed up, and use of the vector (which also does dynamic allocation) is merely exposing the problem.

As your code uses lots of vectors, another possible problem by be out-of-bounds access using vector's operator. These are not checked and can be hard to track down. The only thing I can suggest is code inspection and judicious use of the vector's at() access function, which will throw an error on bounds violation.

Depending on your platform, you may also want to consider using memory debugging tools such as Valgrind.

Edit: Your vector of pointers to double seems very suspicious to me - there aren't may cases where such a data structure is good soluition. I'd take a close look at how that is being used.


As it is not possible for us to determine the cause by looking at the code at this stage, you should start working with a debugger on it. I personally recommend valgrind. It is very good at often finding the cause earlier than when the segfault actually occurs.


There seems to be nothing obviously wrong with the code you have posted. How are you creating the QSweep object? Can you narrow down the code you have to a simple example and post that?


As others indicated there is nothing wrong with this piece of code. Only thing I can think of is since you are changing the size of the class (by adding an additional member variable) you need to build all the binaries (if there are multiple binaries) which are dependent on this. Otherwise, there will be inconsistency between the size of the object created.


Does QSweep's 2-parameter constructor call functions in its initialiser list, e.g.

QSweep(const MxDouble2d& myPoints, const MxInt2d& myEdges)  :   sweepEvents(MemberFunctionCall())  ...  

That's something that is dependent on the declaration order of the class members, and so might cause your problem. The member function might call the member vector before it has been constructed.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »