-1
#include <iostream>
using namespace std;

int main() {
    int T,X,N;
    cin>>T;
    for (int i=0;i<T;i++){
        cin>>N>>X;
        string S;
        cin>>S;
        int arr[N]={};
        arr[0]=X;
        for(int i=0;i<(S.length());i++){
            if(S[i]=='L'){
                arr[i+1]=arr[i]-1;
            }
            else{
                arr[i+1]=arr[i]+1;
            }
        }
        int count=0;
        for(int i=1;i<N+1;i++){
            for(int j=0;j<i;j++){
                if (arr[j]==arr[i]){
                    count++;
                    break;
                }
            }
        }
    
        cout<<((N+1)-count);

    }
    // your code goes here
    return 0;
}

Why am i getting a runtime error(SIGSEGV) in in it? I am not getting any error in vs code for it. could someone explain plzz

I know the error occurs if the program is trying to take the value of an out of bound array but i have given all the necessary conditions in the program.

EDIT:- I just changed the for loop of T to while loop and it solved it.Could someone explain what just happened.

  • 1
    At what line do you get the error? What does your debugger tell you? – HolyBlackCat Dec 28 '22 at 08:11
  • Your array has N values, they have indices from 0 to N-1. You iterate `for(int i=1;i – Alexey S. Larionov Dec 28 '22 at 08:15
  • 2
    What's your input? If you use gcc or clang you can add `-fsanitize=address -g` to your compiler flags and it will tell you the line where the out of bounds access happened. – mch Dec 28 '22 at 08:15
  • SIGSEGV means that you are attempting to read or write memory (via your array accesses) that doesn’t belong to you. I have no idea what this is supposed to do, or how it does it. I suspect you don’t have a clear idea of that either. You need to back up and design a solution _without_ code, then make sure to use code structure and good variable names to make things clear and readable. – Dúthomhas Dec 28 '22 at 08:15
  • Besides, you use the same variable `i` for outer loop (the one that goes up to `T`), and for both the inner loops. Expect trouble because of that – Alexey S. Larionov Dec 28 '22 at 08:16
  • @AlexeyLarionov Reusing the same variable name is a bad idea, but it's not incorrect. – john Dec 28 '22 at 08:33
  • Half your code is aware that arrays index from zero, and half thinks that they start at one. – john Dec 28 '22 at 08:34
  • [Why aren't variable-length arrays part of the C++ standard?](https://stackoverflow.com/q/1887097) – prapin Dec 28 '22 at 08:38
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Jesper Juhl Dec 28 '22 at 13:06

1 Answers1

2

you have allocated an Array size of N

int arr[N]={};

and in for loop you are looping till N ie i < N+1 or i<=N

for(int i=1;i< N+1 ;i++){

accessing index of arr[N] will throw an error as it goes out of boundry

gouravm
  • 301
  • 1
  • 12