Why do I get a SIGABRT?

question-: Contest Page | CodeChef

my answer-:
https://www.codechef.com/viewsolution/45814516

why i am getting SIGABRT error here?

Pay attention to compiler warnings!

[simon@simon-laptop][20:43:06]
[~/devel/hackerrank/otherpeoples]>./compile-latest-cpp.sh 
Compiling satyabrata_6gb-GCDQ.cpp
+ g++ -std=c++14 satyabrata_6gb-GCDQ.cpp -O3 -g3 -Wall -Wextra -Wconversion -DONLINE_JUDGE -D_GLIBCXX_DEBUG -fsanitize=undefined -ftrapv
satyabrata_6gb-GCDQ.cpp: In function β€˜int main()’:
satyabrata_6gb-GCDQ.cpp:36:13: warning: β€˜l’ may be used uninitialized in this function [-Wmaybe-uninitialized]
    assert(l <= r);
             ^~
satyabrata_6gb-GCDQ.cpp:36:13: warning: β€˜r’ may be used uninitialized in this function [-Wmaybe-uninitialized]
+ set +x
Successful
[simon@simon-laptop][20:43:11]
[~/devel/hackerrank/otherpeoples]>echo "1                     
3 3
2 6 9
1 1
2 2
2 3" | ./a.out
satyabrata_6gb-GCDQ.cpp:21:4: runtime error: index 3 out of bounds for type 'int [*]'
a.out: satyabrata_6gb-GCDQ.cpp:36: int main(): Assertion `l <= r' failed.
Aborted (core dumped)

@ssjgz may i know what compiler you use? these error diagnosis seems handy

All the information you need is there :slight_smile:

WHY MY CODE IS GETTING SIGABRT ERROR

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
	int n;
	cin>>n;
	vector<int> v(n);
	vector<pair<int,int>> rel(n);
	for(auto &i : v)
	{
		cin>>i;
	}
	vector<int> dup(v);
	sort(dup.begin(),dup.end());
	set<int> s(v.begin(),v.begin()+n);
	if(s.size() == 1)
	{
		for(int i = 1;i<= n ;i++)
		{
			cout<<v[0]+i<<" ";
		}
		cout<<"\n";
	}
	else
	{
		rel[0].first = dup[0];
		rel[0].second = 0;
		for(int i = 1;i<=n;i++){
			rel[i].first = dup[i];
			if( (i>1) and (dup[i]==dup[i-1])){
			//rel[i].first = dup[i];
			rel[i].second = rel[i-1].second-1;
			}
			else
			rel[i].second = dup[i]-1;
		}
		// for(auto i : rel)
		// {
		// cout<<i.first<<" "<<i.second<<" ";
		// cout<<"\n";
		// }
		for(int i = 0;i< n;i++)
		{
			cout<<rel[i].second<<" ";
		}
		cout<<"\n";
	}
}
}

I don’t know - what Problem are you trying to solve?

question-: Practice problem | MINVOTE

my answer-:
https://www.codechef.com/viewsolution/54439250

why my answer is getting a SIGABRT error?

Why am I getting SIGBART error in this code

Link:Solution: 56360155 | CodeChef

CODE:

using namespace std; 
typedef long long int ll;
int main() { 
    ios_base::sync_with_stdio(0); 
    int t;
    cin>>t;
    while(t--)
    {
     int m,x,pos;
     cin>>m>>x;
     vector<int>dp;
     cout<<"1 ";
     dp.push_back(0);
     dp.push_back(0);
     for(int j=2;j<=x;j++)
     { 
         vector<int> v;
     for(int i=0;i<j;i++)
     {
       v.push_back(i+1);
     } 
     pos=(m-1)%j;
     auto it=v.begin()+pos;
     v.erase(it);  
     cout<<v[dp[j-1]]<<" ";
     dp[j]=v[dp[j-1]]-1; 
     v.clear();
     }
     
     cout<<endl;
    }
    return 0; 
} ```

Out-of- bounds access with sample input:

[simon@simon-laptop][11:00:25]
[~/devel/hackerrank/otherpeoples]>./compile-latest-cpp.sh 
Compiling parth_2811-CIRCLEGAME.cpp
Executing command:
  g++ -std=c++17 parth_2811-CIRCLEGAME.cpp -O3 -g3 -Wall -Wextra -Wconversion -DONLINE_JUDGE -D_GLIBCXX_DEBUG    -fsanitize=undefined -ftrapv
Successful
[simon@simon-laptop][11:00:39]
[~/devel/hackerrank/otherpeoples]>echo "1                 
2 3
" | ./a.out
/usr/include/c++/9/debug/vector:427:
In function:
    std::__debug::vector<_Tp, _Allocator>::reference 
    std::__debug::vector<_Tp, 
    _Allocator>::operator[](std::__debug::vector<_Tp, 
    _Allocator>::size_type) [with _Tp = int; _Allocator = 
    std::allocator<int>; std::__debug::vector<_Tp, _Allocator>::reference = 
    int&; std::__debug::vector<_Tp, _Allocator>::size_type = long unsigned 
    int]

Error: attempt to subscript container with out-of-bounds index 2, but 
container only holds 2 elements.

Objects involved in the operation:
    sequence "this" @ 0x0x7ffd7b4ae240 {
      type = std::__debug::vector<int, std::allocator<int> >;
    }
Aborted (core dumped)