-1

I am getting a 255 error in codeblocks while compiling this code....its giving me filename.exe stopped working...but I'm getting the output in ubuntu

my code...

#include<stdio.h>
int main()
{
  int t;
  scanf("%d",&t);
  while(t--)
  {
  long long int n,k=0;
  scanf("%lld",&n);
  long long int a[n],b[1000000]={0};
  int i;
  for(i=0;i<n;i++)
    {
      scanf("%lld",&a[i]);
      b[a[i]]++;
    }
  long long int count=0;
  for(i=1;i<=1000000;i++)
    { 

       if(b[i]>0)
       count+=(b[i]*(b[i]+1))/2;
    }
  printf("%lld\n",count);
  }
  return 0;
}

i/p 2 4 1 2 3 4 3 1 2 1 o/p 4 4

Mousey
  • 1,855
  • 19
  • 34
cnulenka
  • 1
  • 2
  • 1
    Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a [mcve]. – too honest for this site Aug 26 '15 at 16:51
  • your code might actually [be returning -1 as the error](http://stackoverflow.com/questions/6459023/exec-returning-255). maybe you can use this to help you out – Leroy Aug 26 '15 at 16:52
  • 1
    Not including the `a[n]` VLA, you have *at least* a **7.62 MB** automatic variable storage requirement, assuming `long long` is 8 bytes. My crystal ball tells me you blowing out your runtime stack and puking to a bitter end. – WhozCraig Aug 26 '15 at 16:55
  • 2
    `for(i=1;i<=1000000;i++)` will index out of the range of `b[1000000]`. – Weather Vane Aug 26 '15 at 16:56
  • Assuming by "while *compiling* this code" means "after compiling, when I *run* this code", it appears from the filename you're doing this on a Windows box, and the same code *appears* to work when run on a Ubuntu box. You need to fix the bug(s) below, and read this: ["Getting a stack overflow exception when declaring a large array"](http://stackoverflow.com/questions/571945/getting-a-stack-overflow-exception-when-declaring-a-large-array) – WhozCraig Aug 26 '15 at 17:07

2 Answers2

1

You have defined

long long int b[1000000]={0};

But you go on to

for(i=1;i<=1000000;i++)
  { 
    if(b[i]>0)
    count+=(b[i]*(b[i]+1))/2;
  }

This will index out of the range of b[]. In C, arrays are indexed from b[0] to b[length-1].

EDIT

Also, you are putting at least 8000000 bytes on the stack, which might break it.

Weather Vane
  • 33,872
  • 7
  • 36
  • 56
  • I am not sure that's the reason he is getting the error.. However, that is one of the problems in the program.. – Haris Aug 26 '15 at 17:01
0
  long long int a[n],b[1000000]={0};

Due to large size of b[1000000] you may have exceeded the run time stack size .Thus your program crashes.

Also the loop condition should be i<100000 (but that may or may not be reason here for crash).

Your program should work fine if you reduce size of b.

ameyCU
  • 16,489
  • 2
  • 26
  • 41