Why it is not good to declare huge arrays locally in C? ex: int a[1000000];
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.
The arrays can be declared and initialized globally as well as locally(i.e., in the particular scope of the program) in the program.
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…
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.
because they go onto the stack, and there is only a limited amount of space on the stack,
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With