1

I can declare an array of size 10^6 globally in c, but when I declared it inside the main() function it doesn't work.Why is it so? this statement causes runtime error:

int main()
{
    long long arr[1000001];
    return 1;
}

but this runs perfectly:

static long long arr[1000001];
int main()
{
    return 1;
}

1 Answers1

1

Declaring anything with static means it gets allocated in static memory space. It gets allocated once per program execution, at the beginning, and is never deallocated. Declaring something inside a function (without the static keyword) means it gets allocated on the stack. The stack is much smaller than the static/global memory space.

You can visit the link below for details: http://www.gnu.org/software/libc/manual/html_node/Memory-Allocation-and-C.html

jzila
  • 724
  • 4
  • 11