Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - How to define an integer array with a length of 1000000 in C?
How to define an integer array with a length of 1000000 in C?

int n[1000000]; This is definitely not possible, because the array defined in this way uses stack memory. The system default value is a maximum of 1Mb. An int type occupies 4 bytes, so the maximum can be applied for is 1024* 1024/4=264144. If the system's own occupied maximum is taken into account, the maximum value is about 25,000.

int *p=(int *)malloc(1000000*sizeof(int));, this uses heap memory, as long as you have that much continuous space in the memory; the example is as follows:

#include

#include

int main()

{

int *p=( int *)malloc(1000000*sizeof(int));

//int p[1000000];

int i=0;

for(; i<1000000;i++)

printf("%d\n",p[i]=i);

free(p);

return 0;

}