vendredi 22 février 2019

sort() on a vector of class objects gives segmentation fault

I am facing this wierd problem which has left me with no clue and I am not able to figure out the issue. Below is the problem statement.

There are 100 teams participating (numbered 1 to 100) in a competition trying to solve 9 problems. A team may not be able to solve any problem, in which case the total_solved problems and total_time will be zero. For the sake of easiness, I am maintaining a static vector with size as 100. The name stores the team number (1 to 100) . I am using active flag to know that a team submitted at least 1 solution (even wrong).

Here is the class team :

class team
{
public:
        int total_solved;
        int time[9];
        int total_time;
        bool solved[9];
        bool active;
        int name;

        team()
        {
                total_solved = total_time = 0;
                active = false;
                name = -1;
                for(int i=0;i<9;i++)
                {
                        solved[i] = false;
                        time[i] = 0;
                }
        }
};

Here is the vector:

for(int i=0;i<100;i++)
{
    record.push_back(new team());
}

Somewhere later, I fill the data about the teams. Here is the dump of the data corresponding to these teams :

                cout << "Dumping the data\n";
                for(auto it=record.begin();it!=record.end();it++)
                {
                        cout << (*it)->name << " " << (*it)->total_solved << " " << (*it)->total_time << " " << ((*it)->active?'Y':'N') << endl;
                }
                cout << "That's all\n";

Dumping the data
-1 0 0 N
2 0 0 Y
-1 0 0 N
-1 0 0 N
5 0 0 Y
-1 0 0 N
-1 0 0 N
-1 0 0 N
-1 0 0 N
-1 0 0 N
-1 0 0 N
-1 0 0 N
-1 0 0 N
-1 0 0 N
-1 0 0 N
-1 0 0 N
-1 0 0 N
-1 0 0 N
-1 0 0 N
-1 0 0 N
-1 0 0 N
-1 0 0 N
-1 0 0 N
24 0 0 Y
-1 0 0 N
-1 0 0 N
-1 0 0 N
-1 0 0 N
-1 0 0 N
-1 0 0 N
-1 0 0 N
-1 0 0 N
-1 0 0 N
34 0 0 Y
-1 0 0 N
-1 0 0 N
-1 0 0 N
-1 0 0 N
-1 0 0 N
-1 0 0 N
41 0 0 Y
-1 0 0 N
-1 0 0 N
-1 0 0 N
45 0 0 Y
-1 0 0 N
-1 0 0 N
-1 0 0 N
-1 0 0 N
-1 0 0 N
-1 0 0 N
-1 0 0 N
-1 0 0 N
-1 0 0 N
-1 0 0 N
-1 0 0 N
-1 0 0 N
58 0 0 Y
-1 0 0 N
-1 0 0 N
-1 0 0 N
62 0 0 Y
-1 0 0 N
64 0 0 Y
-1 0 0 N
-1 0 0 N
67 0 0 Y
-1 0 0 N
69 0 0 Y
-1 0 0 N
-1 0 0 N
-1 0 0 N
-1 0 0 N
-1 0 0 N
-1 0 0 N
-1 0 0 N
-1 0 0 N
78 0 0 Y
-1 0 0 N
-1 0 0 N
-1 0 0 N
-1 0 0 N
-1 0 0 N
-1 0 0 N
-1 0 0 N
-1 0 0 N
-1 0 0 N
-1 0 0 N
-1 0 0 N
-1 0 0 N
-1 0 0 N
-1 0 0 N
-1 0 0 N
-1 0 0 N
-1 0 0 N
-1 0 0 N
-1 0 0 N
-1 0 0 N
-1 0 0 N
-1 0 0 N
That's all

You can see that no team has solved any problem in this specific case. And some teams are not active (did not submit any solution, name as -1 or active as false denotes that). The crash happens when I try to sort this 100 team data. The sorting criteria is that teams must solve max problems in min time. If there is a tie, we will sort as per team number ignoring the inactive ones.

bool compare(team *t1, team *t2)
{
        if(t1->total_solved != t2->total_solved)
                return t1->total_solved > t2->total_solved;
        if(t1->total_time != t2->total_time)
                return t1->total_time < t2->total_time;
        return t1->active;
}

sort(record.begin(),record.end(),compare);

I analysed through gdb, I get the following :

Program received signal SIGSEGV, Segmentation fault.
0x00005555555552d0 in compare (t1=0x55555576fec0, t2=0x411) at 10258.cpp:33
33              if(t1->total_solved != t2->total_solved)

t2 is definitely getting invalid pointer, but I wonder why?

Aucun commentaire:

Enregistrer un commentaire