1

I've written the following code for a codechef problem. Everytime I submit I get a NZEC error. Any ideas?

       #include<stdio.h>
       int main()
       {

           int n=0,s=0;
           int a[2001][2001]={0},i=0,k=0,c=0;
           scanf("%d%d",&n,&s);
           for(i=0;i<s;i++)
           {
                scanf("%d",&c);
                for(k=0;k<c;k++)
                {
                    scanf("%d",&a[i][k]);
                }
           }
           printf("%d\n",s);
           for(i=0;i<s;i++)
                printf("%d\n",i); 
           return 0;
        }
4

2 回答 2

2

Your array is created in the stack, which might be too small for it.

Essentially in multi-threaded applications, each thread will have its own stack. But, all the different threads will share the heap.

So in your int main() function when you define the array you are actually creating it in the stack (it's the "main thread"). If you move it outside of the function and define it globally, it will be created in the heap.
(This is a bit more complicated than what I've just described, e.g. if you create an object using the new keyword inside a function it will be created in the heap etc. but I won't be explaining that now)

You can check the stack size by using ulimit -s.

Also note that the stack is generally faster than the heap because of the way memory is allocated.

于 2013-04-19T16:06:24.097 回答
2

int a[2001][2001]堆栈太大,将其声明为全局变量。

int a[2001][2001]={0};
int main()
{
    int n=0,s=0;
    int i=0,k=0,c=0;
    scanf("%d%d",&n,&s);
    /* etc. */
}
于 2013-04-19T15:49:13.270 回答