I've been working on a code which gives a segmentation fault when I run it, I tried using gdb but I am unable to parse the messages.
1 2 3 4 5 6 7 8
Program received signal SIGSEGV, Segmentation fault.
0x00000000004051c3 in std::deque<int, std::allocator<int> >::_M_push_back_aux(intconst&) ()
(gdb) backtrace
#0 0x00000000004051c3 in std::deque<int, std::allocator<int> >::_M_push_back_aux(int const&) ()
#1 0x0000000000405272 in std::deque<int, std::allocator<int> >::push_back(int const&) ()
#2 0x000000000040646a in buses::set_ub_dominance(ub_dominances*, int) ()
#3 0x00000000004038fb in main ()
(gdb)
I tried looking at the function set_ub_dominance but did not find anything wrong in particular, the function is called from main and essentially modifies members of the a class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
void buses::set_ub_dominance(ub_dominances* p, int j)
{
int temp;
temp = j+1;
if (state.size() != 0)
{
for (int i=0;i<state.size();i++)
{
if (state[i].dominance_flag == 1)
{
(*(p+state[i].uid-1)).index.push_back(temp);
(*(p+state[i].uid-1)).time.push_back(state[i].time);
(*(p+state[i].uid-1)).state_id.push_back(i+1);
}
}
}
}
It would be a lot easier to read if you used the subscript operator [] for p instead of adding to it and dereferencing.
Does uid start with 0? If it does, you will be accessing an element before the array at p begins. Are you sure p points to an array big enough to hold all the uids? Also, what is the point of temp?