why SIGABRT error?

Please help. My program is running into a SIGABRT error when I submit it.

Question: Andrew and the Meatballs

My Code:

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main() {

	int t, n, m, c, temp;
	cin >> t;
	while (t--){
		vector<int> plates;
		cin >> n >> m;
		pair<int, int> * sort_plates = new pair<int, int>[n];
		for (int a = 0; a < n; a++){
			cin >> temp;
			plates.push_back(temp);
		}
		sort(plates.begin(), plates.end());
		n--;
		c = n;
		bool flag = false;
		while (c >= 0 && flag == false){
			for (int a = 0; a <= n - c; a++){
				if (sort_plates[a].first > 0){
					if (plates[c] <= sort_plates[a].first){
						sort_plates[a].first -= plates[c];
						sort_plates[a].second++;
					}
				}
				else {
					if (plates[c] <= m){
						sort_plates[a].first = m - plates[c];
					}
					else {
						sort_plates[a].first = m;
					}
					sort_plates[a].second = 1;
				}
				if (sort_plates[a].first == 0){
					flag = true;
					cout << sort_plates[a].second << endl;
					a = n;
				}
			}
			c--;
		}

		if (flag == false){
			cout << "-1" << endl;
		}

	}
	return 0;
}

SIGABRT is commonly used by libc and other libraries to abort the progamm in case of critical errors. For example, glibc sends an SIGABRT in case of a detected double-free or other heap corruptions.

Also, most “assert” implementaions make use of SIGABRT in case of a failed assert.

Furthermore, SIGABRT can be send from any other process like any other signal. Of course, the sending process needs to run as same user or root.

It usually happens when there is a problem with memory allocation.

It happened to me when I my program was trying to allocate an array with negative size.

abort() sends the calling process the SIGABRT signal, this is how abort() basically works.

abort() is usually called by library functions which detect an internal error or some seriously broken constraint. For example malloc() will call abort() if its interal structures are damaged by a heap overflow.

9 Likes

i had encountered SIGABRT while using “sort” as well

i ended up writing my own “compare” function for “sort”, which did the same thing as what i expected the default compare of “sort” to do, and that helped me get AC. i still don’t know why it worked. “sort” seems to act a bit weird while working with vectors…

3 Likes

I also encountered the same error and the main reason was:

  1.    vector<lli> score(n);
    
  2.    for(int i = 1 ; i<n+1; i++) {
    
  3.     cin>>score[i];
    
  4.   }
    

Here you can see the score vector has score[0] to score[n-1]. But i tried to access the score[n] in line 3, which is not allowed.

1 Like

I have encountered SIGABRT while using bitset.
On my computer it compiles for all edge cases.
```
#pragma GCC optimize ("-O2")
#include <bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)

const int inf = 1e9 +5;
#define M 100
void range_change(bitset& x, int lower, int upper, bool change){
if (change){
for (unsigned i = lower; i <= upper; ++i)
x.set(i);
}
else{
for (unsigned i = lower; i <= upper; ++i)
x.reset(i);
}
}
int count_Set(bitset & x){

}
bitset<100> houses;

int main(){
fastio;
int t;
cin>> t;
int m, x, y;
int reach{0};
int lower{0}, upper{0};
// int t = 1;
while(t–){

    houses.set();
    // cout<< houses;
    cin >> m >> x >> y;
    reach = x*y;
    vector<int> cat(m, 0);
    for(auto& u: cat){
        cin>> u;

        lower = u - reach -1;
        if(lower < 0) lower = 0;
        upper = u + reach-1;
        if(upper > 100) upper = 99;
        range_change(houses, lower, upper, false);
    }
    // cout<< houses << "\n";
    cout<< houses.count() << "\n";

}
return 0;
}

code breaks in for upper = 100, that case is not handled it should be
upper >=100 => upper = 99

you got any solution for this.In some cases it is compulsory to use cmp functin in sort but its shows run time error