how to read large inputs in c

,

i mean if a problem needs to scan 10^6 integer values.how am i supposed to store them ?if i declare array of size 10^6 i’m getting error.please help me with this.thanks in advance

Store the array outside main() because if the array is stored inside main(), the stack memory is used up. Stack memory is usually limited by the user.
When the array is stored outside main(), data will get mapped to either Data Segment or BSS.
You can see the memory layout in gcc using:
->gcc memory-layout.c -o memory-layout
->size memory-layout

Check these out:

  1. Stack and Heap memory.

  2. Click here to see an image that might give you a high-level understanding of Stack and Heap memory.

1 Like

You can create an array of size 10^6. that wont lead to any error but if u are using more memory during the process , that might lead to segmentation fault.

In situations where we MUST read all the 10^6 integers , we have to take care of the memory usage of our program and try to reduce it accordingly.

1 Like

This is incorrect. Global (or static) variables in C are stored in the data segment or BSS segement, not in the heap! The heap must be manually allocated and freed.