Thread: compare segmantation fault

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    8

    compare segmantation fault

    Hi everyone.

    I wrote my first useful program in C++ [maybe after hello world]. After hours of effort to make it survive the compilation [and it finally did] I can't make it work right. I get at runtime the message : Segmentation fault. using gdb I found that it is somehow related to the comparison functor I defined... still can't understand how can one create a segmentation fault without arrays.

    I have included the parts of the code which I think that are relevant. If it helps , I am trying to implement Kruskal's algorithm to find minimal spanning tree:

    Code:
    //this is the functor:
    struct lvert
    {
      bool operator()(const vert &v1,const vert &v2)
      {
            //comparing the graph's verticles so they can be ordered.
        	if(v1.x==v2.x)
    			return v1.y<v2.y;
    		return v1.x<v2.x;
      }
    };
    
    //gdb says that the problem comes from here:
    int unite(vert v1,vert v2,map<vert,set<vert,lvert> ,lvert > &dsets,vector<vector<vert> >& verts)
    {
    	cout << v1.x << ":" << v1.y << " " << v2.x << ":" << v2.y  << endl;
    	set<vert,lvert> *S1 = &dsets[verts[v1.x][v1.y]]; //I guess this is where something is wrong
    	set<vert,lvert> *S2 = &dsets[verts[v2.x][v2.y]]; //or maybe here?
    
    
    	set<vert,lvert>::iterator i;
    	for(i=S2->begin(); i!=S2->end(); i++)
    	{
    		
    		verts[i->x][i->y] = verts[v1.x][v1.y];
    		
    	}
    	S1->insert(S2->begin(),S2->end());
    	dsets.erase(v2);
    
    }
    
    
    //and this code is somewhere in main() , it calls unite()
    for(i=0; i<N; i++)
    	{
    		verts[i].resize(M);
    		for(j=0; j<M; j++)
    		{
    			verts[i][j] = vert(i,j);
    			dsets[vert(i,j)].insert(vert(i,j));
    			cin >> state;
    			vert v1(i,j);
    			vert v2(i+1,j);
    			vert v3(i,j+1);
    			if(state==3)
    			{
    				Sedges.erase(edge(i,j,i,j+1));
    				unite(v1,v3,dsets,verts);
    				Sedges.erase(edge(i,j,i+1,j));
    				unite(v1,v2,dsets,verts);
    			}
    			if(state==2)
    			{
    				Sedges.erase(edge(i,j,i,j+1));
    				unite(v1,v3,dsets,verts);
    			}
    			if(state==1)
    			{
    				Sedges.erase(edge(i,j,i+1,j));
    				unite(v1,v2,dsets,verts);
    			}
    
    			
    		}
    		
    	}
    bar.

  2. #2
    Registered User
    Join Date
    Mar 2006
    Posts
    8

    maybe this can help

    Code:
    Program received signal SIGSEGV, Segmentation fault.
    0x0804a0b0 in lvert::operator() (this=0xbffff5d8, v1=@0x8050470, v2=@0x8)
        at p2.cpp:62
    62              if(v1.x==v2.x)
    (gdb) list
    57
    58      struct lvert
    59      {
    60        bool operator()(const vert &v1,const vert &v2)
    61        {
    62              if(v1.x==v2.x)
    63                              return v1.y<v2.y;
    64                      return v1.x<v2.x;
    65        }
    66      };
    (gdb) back
    #0  0x0804a0b0 in lvert::operator() (this=0xbffff5d8, v1=@0x8050470, v2=@0x8)
        at p2.cpp:62
    #1  0x0804a532 in std::_Rb_tree<vert, std::pair<vert const, std::set<vert, lvert, std::allocator<vert> > >, std::_Select1st<std::pair<vert const, std::set<vert, lvert, std::allocator<vert> > > >, lvert, std::allocator<std::pair<vert const, std::set<vert, lvert, std::allocator<vert> > > > >::lower_bound (
        this=0xbffff5d0, __k=@0x8) at stl_tree.h:1323
    #2  0x0804a4ad in std::map<vert, std::set<vert, lvert, std::allocator<vert> >, lvert, std::allocator<std::pair<vert const, std::set<vert, lvert, std::allocator<vert> > > > >::lower_bound (this=0xbffff5d0, __x=@0x8) at stl_map.h:508
    #3  0x08049630 in std::map<vert, std::set<vert, lvert, std::allocator<vert> >, lvert, std::allocator<std::pair<vert const, std::set<vert, lvert, std::allocator<vert> > > > >::operator[] (this=0xbffff5d0, __k=@0x8) at stl_map.h:316
    #4  0x08048a9f in unite (v1={x = 0, y = 1}, v2={x = 1, y = 1},
        dsets=@0xbffff5d0, verts=@0xbffff5c0) at p2.cpp:82
    #5  0x080490b7 in main () at p2.cpp:155
    (gdb)
    That's what the debugger gives.

    bar.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcpy(), 2 strings, and segmantation fault
    By jigidyjensen in forum C Programming
    Replies: 4
    Last Post: 03-20-2009, 01:42 PM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Segmantation Fault (core dumped) error
    By Vinnie66 in forum C Programming
    Replies: 6
    Last Post: 03-25-2002, 01:34 PM