STACK SMASHING ERROR + COPS + Dangerous LOGIC

Hello!
PROBLEM LINK

3
4 7 8
12 52 56 8
2 10 2
21 75
2 5 8
10 51
0
18
9
*** stack smashing detected ***: terminated
Aborted (core dumped)

Process returned 134 (0x86)   execution time : 1.402 s
Press ENTER to continue.

I am having above error in this question code goes here:-

#include <bits/stdc++.h>
using namespace std;

/*
[1,2,3,4,5,6,7]
1 2 3 4 5 6 7 */
/*
void color(int arr[],int start,int endd){
    for(int i = start-1;i<endd;++i)
    {
        arr[i]=1; // 1 means covered and not safe
    }
}*/

int main()
{
    long t;
    cin>>t;
    for(int k = 0;k<t;++k)
    {   int A[100] = {0};
        int M,x,y;
        cin>>M>>x>>y;
        int cpos=0;
        for(int i=0;i<M;++i)
        {
            cin>>cpos;
            int st = cpos-(x*y);
            if(st<1){
                st =1;
            }
            int en = cpos+(x*y);
            //color(A,st,en);
        for(int i = st-1;i<en;++i)
            {
                A[i]=1; // 1 means covered and not safe
            }
        }
        cout<<count(A,A+100,0)<<"\n";

    }

}

Explanation:- for possible ranges of cops made the corresponding locations = 1 and counted the positions where safe houses are present.

Solution Logic : 100% correct
Problem: STACK SMASHING ERROR ie runtime error SIGSEGV

Answer is correct but i cant figure out reason for STACK SMASHING ERROR

Here’s more information, from using better diagnostics compilation flags:

[simon@simon-laptop][13:03:30]
[~/devel/hackerrank/otherpeoples]>./compile-latest-cpp.sh 
Compiling arnav_COPS.cpp
Executing command:
  g++ -std=c++17 arnav_COPS.cpp -O3 -g3 -Wall -Wextra -Wconversion -DONLINE_JUDGE -D_GLIBCXX_DEBUG    -fsanitize=undefined -ftrapv
Successful
[simon@simon-laptop][13:03:34]
[~/devel/hackerrank/otherpeoples]>echo "3
4 7 8
12 52 56 8
2 10 2
21 75
2 5 8
10 51
0
18
9" | ./a.out
arnav_COPS.cpp:35:20: runtime error: index 100 out of bounds for type 'int [100]'
arnav_COPS.cpp:35:21: runtime error: store to address 0x7ffc01cd7af0 with insufficient space for an object of type 'int'
0x7ffc01cd7af0: note: pointer points here
 01 00 00 00  02 00 00 00 00 00 00 00  00 98 3e 35 4d a4 37 1b  c8 ef 23 fe 01 7f 00 00  c0 cb 00 a5
              ^ 
0
18
9
*** stack smashing detected ***: terminated
Aborted (core dumped)

What do you mean sir?
I think the problem is initializing the array again and again

It’s got nothing to do with that, and everything to do with the out-of-bounds access on line 35:

arnav_COPS.cpp:35:20: runtime error: index 100 out of bounds for type 'int [100]'

solved it TIME TAKEN 0.1 s

#include <bits/stdc++.h>
using namespace std;
int A[100];
int main()
{   cin.tie(NULL);
    cout.sync_with_stdio(false);
    long t;
    long long M,x,y,st,en;
    long long cpos=0;
    cin>>t;
    try{
    for(int k = 0;k<t;++k)
    {   for(int i =0;i<100;++i){A[i]=0;}

        cin>>M>>x>>y;
        cpos=0;


        for(int i=0;i<M;++i)
        {
            cin>>cpos;
            st = cpos-(x*y);
            if(st<1){
                st =1;
            }
            en = cpos+(x*y);
           

        for(int i = st-1;i<en;++i)
            {
                A[i]=1; // 1 means covered and not safe
            }
        }

        cout<<count(A,A+100,0)<<"\n";

    }
    }
    catch(int x){
        }

}

SIR is this work around correct. I initialized array outside and this time looped to make all zero

How to solve then? Please help . I am not that good YET

Still exactly the same error; you just got lucky when you got AC (there’s Undefined Behaviour for you):

[simon@simon-laptop][13:22:00]
[~/devel/hackerrank/otherpeoples]>./compile-latest-cpp.sh 
Compiling arnav_mehta-COPS-2.cpp
Executing command:
  g++ -std=c++17 arnav_mehta-COPS-2.cpp -O3 -g3 -Wall -Wextra -Wconversion -DONLINE_JUDGE -D_GLIBCXX_DEBUG    -fsanitize=undefined -ftrapv
arnav_mehta-COPS-2.cpp: In function ‘int main()’:
arnav_mehta-COPS-2.cpp:29:31: warning: conversion from ‘long long int’ to ‘int’ may change value [-Wconversion]
   29 |                 for(int i = st-1;i<en;++i)
      |                             ~~^~
Successful
[simon@simon-laptop][13:22:05]
[~/devel/hackerrank/otherpeoples]>echo "3                 
4 7 8
12 52 56 8
2 10 2
21 75
2 5 8
10 51
0
18
9" | ./a.out
arnav_mehta-COPS-2.cpp:31:24: runtime error: index 100 out of bounds for type 'int [100]'
arnav_mehta-COPS-2.cpp:31:25: runtime error: store to address 0x557cf27b7fd0 with insufficient space for an object of type 'int'
0x557cf27b7fd0: note: pointer points here
 01 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00
              ^ 
0
18
9

Just add a guard to ensure you’re not going out of bounds e.g. for your original code:

 for(int i = st-1;i<en;++i)
            {
                if (i >= 0 && i < 100)
                {
                    A[i]=1; // 1 means covered and not safe
                }
            }
1 Like

Can you see the above message , its working?.
I am not getting any error while running
Please elaborate

How is error hiding by initializing it outside??

Umm… Tried this but getting 1 1 1 as result now which is WRONG.
I dont know much i’ve been working with c++ for just about a week

I’d Be Grateful By your Further assistance
Thanks
Sincerely
Arnav