i was just solving THE LEAD GAME problem, but it results in SIGSEGV run time error.
#include
#include
#include
using namespace std;
int main() {
int N;
cin>>N;
int S[N],T[N];
int CS=0,CT=0;
vector leadS;
vector leadT;
for(int i=0;i<N;i++)
{
cin>>S[i]>>T[i];
CS=CS+S[i];
CT=CT+T[i];
if(CS>CT)
{
leadS.push_back(CS-CT);
}
else
{
leadT.push_back(CT-CS);
}
}
int max_leadS = *max_element(leadS.begin(), leadS.end());
int max_leadT = *max_element(leadT.begin(), leadT.end());
int W,L;
if(max_leadS>max_leadT)
{
W=1;
L=max_leadS;
}
else
{
W=2;
L=max_leadT;
}
cout<<W<<" "<<L;
return 0;
}
ssjgz
2
Your solution has an out-of-bounds access on this test input:
4
17 33
26 60
34 32
27 22
[simon@simon-laptop][11:55:23]
[~/devel/hackerrank/otherpeoples]>./compile-latest-cpp.sh
Compiling geek_abhi-TLG.cpp
+ g++ -std=c++14 geek_abhi-TLG.cpp -O3 -g3 -Wall -Wextra -Wconversion -DONLINE_JUDGE -D_GLIBCXX_DEBUG -fsanitize=undefined -ftrapv
+ set +x
Successful
[simon@simon-laptop][11:55:28]
[~/devel/hackerrank/otherpeoples]>echo "4
17 33
26 60
34 32
27 22
" | ./a.out
/usr/include/c++/7/debug/safe_iterator.h:270:
Error: attempt to dereference a past-the-end iterator.
Objects involved in the operation:
iterator "this" @ 0x0x7ffea1e08450 {
type = __gnu_debug::_Safe_iterator<__gnu_cxx::__normal_iterator<int*, std::__cxx1998::vector<int, std::allocator<int> > >, std::__debug::vector<int, std::allocator<int> > > (mutable iterator);
state = past-the-end;
references sequence with type 'std::__debug::vector<int, std::allocator<int> >' @ 0x0x7ffea1e084b0
}
Aborted (core dumped)
Edit:
This line, according to gdb:
int max_leadS = *max_element(leadS.begin(), leadS.end());
1 Like
i had encountered same error in c , using & got me the answer similary you can check in the user value process