0

In a codechef problem Maximise Function
My code for the problem is:

#include<iostream>
#include<math.h>
using namespace std;
void swap(long long int *x, long long int *y)
{
  long long int temp = *x;
  *x = *y;
  *y = temp;
}
void BubbleSort(long long int n, long long int a[])
{
//    long long int i, j;
  for(long long int i=0; i<n-1; i++)
  {
      for(long long int j=0; j<n-i-1; j++)
      {
          if(a[j]>a[j+1])
          {
              swap(a[j], a[j+1]);
          }
      }
  }
}
int main()
{
  long long int k;
  cin>>k;
  while(k--)
  {
      long long int n;
      long long int a[n];
      cin>>n;
      for(long long int i=0; i<n; i++)
      {
          cin>>a[i];
      }
      BubbleSort(n ,a);
      cout<<2*abs((a[n-1]-a[0]))<<"\n";
      
  }
  return 0;
}


The code is running fine with CodeChef IDE when using int as a data type for all the variables below, but when the datatype is changed to long long int (because of the given constraints), the CodeChef IDE is giving a runtime error. I am unable to understand the problem that is occurring. Please help me to spot that problem.
Runtime Error: SIGEMT

  • "the CodeChef IDE is giving a runtime error. " ***What is the error***? – Claies Feb 15 '21 at 06:04
  • You chose a poor approach just to obtain min and max. And it's made worse by the bubble sort's runtime complexity. – StoryTeller - Unslander Monica Feb 15 '21 at 06:04
  • `long long int a[n];` : [Variable length arrays are not standard C++](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard). And even if your compiler does support them, you certainly can't create the array before you initialized `n`. – Nate Eldredge Feb 15 '21 at 06:05
  • the **VERY FIRST** google reference for that error explains the problem: "SIGEMT It is the emulator trap. It results from certain some unimplemented instructions (i.e you are trying to give a instruction which is not implemented in GNU library) which might be emulated in software, or the operating system's failure to properly emulate them. This Error Can't be removed." basically, as @NateEldredge has stated, your implementation of a variable length array isn't supported. – Claies Feb 15 '21 at 06:08
  • a[n] is UB, n has no value. Crashes with this website's name. – Hans Passant Feb 15 '21 at 06:16

1 Answers1

2
  long long int n;
  long long int a[n];
  cin>>n;

You are attempting to create a variable length array of size n, where n is uninitialized. So the size of a will be some garbage value that may be too small for the input you end up putting in it, or so large that it immediately overflows the stack. Both are bad. It was sheer luck that this happened to not crash when you were using int.

At the very least, move the long long int a[n]; after the cin>>n;.

But variable length arrays are not standard C++. Consider rewriting this to use std::vector, or else dynamically allocate your array with new[]. Then add some error checking in case no integer can be read from the input.

Also, your swap(a[j], a[j+1]); isn't calling your swap function (which expects pointers whereas you are passing long long ints), but rather std::swap. That's probably a good thing as std::swap actually works here and will generally be more efficient, so you might as well drop yours. This is a downside of using namespace std; as things like this may happen without you noticing.

Nate Eldredge
  • 48,811
  • 6
  • 54
  • 82