Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declare huge arrays locally in C

Tags:

arrays

c

Why it is not good to declare huge arrays locally in C? ex: int a[1000000];

like image 679
hmdb Avatar asked Mar 17 '13 22:03

hmdb


People also ask

How to declare large array in C?

Usually you need to create such an array dynamically on the heap. int *integer_array = (int*)malloc(2000000 * sizeof(int)); float *float_array = (float*)malloc(2000000 * sizeof(float)); The array might be too large for stack allocation, e.g. if used not globally, but inside a function.

Can we declare array globally?

The arrays can be declared and initialized globally as well as locally(i.e., in the particular scope of the program) in the program.

What is the maximum size of a global array?

array size 10^7 to 10^8 declared globally(i.e. on heap) is possible…if u declare nething in the main or in ne fxn…it goes into the stack which has a smaller size hence ur 10^7 array did not work out…

What is local array in C?

Local Arrays: The arrays which get initialized inside a function or block are known as local arrays. These types of arrays get memory allocated on the stack segment.


1 Answers

because they go onto the stack, and there is only a limited amount of space on the stack,

like image 136
Keith Nicholas Avatar answered Sep 22 '22 23:09

Keith Nicholas