-3

I am neither using pointer nor using freed memory but i do not understand what is causing sigsegv error. For some test cases the algorithm is working without any error while for other test case it is showing SIGSEGV.

#include<iostream>
using namespace std;

int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int n,m;
  cin>>n>>m;
  int arr[n],arrh[m];

  for(int i=0;i<n;i++)
  {
    cin>>arr[i];
  }
  for(int i=0;i<m;i++)
  {
    cin>>arrh[i];
  }

  int arrcc[m][n]={0};                    //Precomputation Cumulative
  int val;
  for(int j=0;j<m;j++)
  { 
    val=0;
    for(int i=0;i<n;i++)
    {
      if(arr[i]==(j+1))
      {
        val++;
      }
      arrcc[j][i]=val;
    }
  }

  int q,l,r;
  cin>>q;
  int k;
  int arrc[m];
  while(q--)
  { 
    k=0;
    for(int i=0;i<m;i++)              
    {
      arrc[i]=0;
    }

    cin>>l>>r;

    // for(int j=0;j<m;j++)                      //Time limit exceed need precomputation  
    // {
    //  for(int i=(l-1);i<r;i++)
    //  { 
    //    if(arr[i]==(j+1))
    //    { 
    //      arrc[j]++;
    //    }
    //  }
    // }

    for(int j=0;j<m;j++)              //Calculating from cumulative
    {
      if(l!=1)
        arrc[j]=(arrcc[j][r-1]-arrcc[j][l-2]);
      else
        arrc[j]=(arrcc[j][r-1]-0);
    }

    for(int i=0;i<m;i++)
    {
      if(arrc[i]!=0)
      {
        if(arrc[i]!=arrh[i])
        {
          cout<<"0"<<"\n";
          k++;
          break;
        }
      }
    }
    if(k==0)
    {
      cout<<"1"<<"\n";
    }
  }
}

link to problem the problem-
https://drive.google.com/open?id=1sYEbtdFTT9ZE67y4Wygvv_AKJWGWgXiI https://www.hackerearth.com/challenges/competitive/april-circuits-20/algorithm/happy-segments-e290faa6/

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
randomUser
  • 653
  • 6
  • 23
  • 2
    `int arr[n],arrh[m];` are variable-length arrays and not supported by C++. Some compilers provide compiler extensions for this but the behavior dependents on the compiler and version. What compiler and version do you use? – Thomas Sablik Apr 21 '20 at 12:34
  • 2
    *I am neither using pointer nor using freed memory* -- But you are using non-legal syntax, such as variable-length-arrays, including `int arrcc[m][n]={0};` . If the value of `n` or `m` is sufficiently large, you are risking a stack memory error. Maybe that's the reason for the seg fault? Instead of the fake syntax, use `std::vector`. – PaulMcKenzie Apr 21 '20 at 12:36
  • you do not check the input was valid, if you do not enter a right value you will never read something more and all will stay unitialized. I also encourage you to print some message to indicate what input is expected – bruno Apr 21 '20 at 12:38
  • @ThomasSablik i was running the code on hackerearth page(online) using C++14(g++ 5.4.0) – randomUser Apr 21 '20 at 12:40
  • 1
    what are your input values ? – bruno Apr 21 '20 at 12:40
  • 1
    @YouKnowWho -- If you changed to `std::vector`, here are the two scenarios: 1) All your test cases will work, in terms of not crashing (maybe they will all work) or 2) You still get an error, but this time, you have a much better chance of finding the error, since vector has an `at()` method that automatically checks for out-of-bounds access. There is absolutely no advantage in taking the shortcut of using variable-length arrays. – PaulMcKenzie Apr 21 '20 at 12:42
  • @bruno all test cases will be realeased maybe after a week. – randomUser Apr 21 '20 at 12:43
  • @YouKnowWho I think the test cases also concern invalid inputs, and because you do not manage them that explain the possible crash. **Always** check the input success, `if (! (cin>>n>>m)) ...error` and for that case also check *n* and *m* are strictly positive etc. You have to do a robust code never crashing nor having undefined behavior whatever the inputs – bruno Apr 21 '20 at 12:46
  • `int arr[n],arrh[m];` --> `std::vector arr(n), arrh(m);` Then `int arrcc[m][n]={0};` --> `std::vector> arrcc(m, std::vector(n));`. Make similar changes to other places where you are declaring variable-length arrays. – PaulMcKenzie Apr 21 '20 at 12:47
  • 1
    You can find the documentation for GCC https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html The arrays are allocated on the stack. The stack size is very limited. Large arrays should be allocated on the heap. – Thomas Sablik Apr 21 '20 at 12:47
  • Related: [https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard) – drescherjm Apr 21 '20 at 13:03
  • 3
    I wished g++ and clang would turn off the VLA syntax, and only invoke it via compiler command switch. If they did that, many of the questions here would not need to be posted, since the programmer would be forced to use `std::vector`, and magically their program would work from the start. – PaulMcKenzie Apr 21 '20 at 13:14
  • @PaulMcKenzie on hackerearth and other online programming site sometime when i see other people solution they declare array like this (the solution i have seen until now) – randomUser Apr 21 '20 at 13:24
  • 2
    @YouKnowWho -- That's the issue. All of those solutions would look different (they would use standard C++) if the compiler's didn't have VLA syntax turned on by default. – PaulMcKenzie Apr 21 '20 at 13:28
  • @PaulMcKenzie on changing all the array with vector i am getting Memory Limit Exceeded . Memory Limit is 256 MB and time limit is 2 sec for each input file. m,n,q belong to 5*10^5. I should have noticed about memory. But now when i save memory time exceed and when i save time memory exceed. – randomUser Apr 21 '20 at 17:55

1 Answers1

4
cin>>n>>m;
int arr[n],arrh[m];

Firstly, the program is ill-formed in C++. The size of an automatic array must be compile time constant, which n and m are not.

Secondly, in case you intentionally use an extended C++ language: The storage for objects in automatic storage is typically strictly limited. The size of the execution stack is by default about 1-8 mega bytes. For large n or m, those arrays may overflow the stack which, if you are lucky, will cause the program to crash.

If you need an array of runtime size, then use dynamic storage. Simplest solution is to use std::vector. This removes the chance of the array overflowing the stack, and makes the program well-formed C++ in regard to the array size.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • On using vector instead of array Memory limit exceed . Memory Limit is 256 MB and time limit is 2 sec for each input file. m,n,q belong to 5*10^5. But i am stuck when i save memory time exceed and when i save time memory exceed. – randomUser Apr 21 '20 at 18:00
  • @YouKnowWho I assume that is the point of the exercise. You are supposed to find an algorithm that has sufficiently low asymptotic complexity both in time and space. – eerorika Apr 21 '20 at 18:05
  • Yes now i am thinking once again. – randomUser Apr 21 '20 at 18:06