Enormous input test(Array Scope)

a post in the enormous input test’s comment box:
“if i size my array to 10^7 the program crashes, is there another way to size it besides just typing in 10000000”

and the corresponding reply:
“Your code crashes because you are declaring the entire array inside your main function, and as a result on the stack. Declare it as a global variable. It won’t crash then.”

i did not get the point. does declaring something (in this case an array) global or in the main function effect the code efficiency… how? i was not able to get any material on internet about this

use linklist. Then your program won’t crash

thanks for the reply… I think you did not get my point… crash or no crash is not my issue… i want to know the soul purpose of admin, why he said this
“your code crashes because you are declaring the entire array inside your main function, and as a result on the stack. Declare it as a global variable. It won’t crash then.”

does declaring something (in this case an array) global or in the main function effect the code efficiency??

no, not the code efficiency. it affects the way of your system is handling the memory on your machine.

actually, when you are allocating memory for a variable in a function, the memory is taken from the current stack space. it’s a limited contiguous memory space, not shared between function calls, whose delimiters are pointed by ebp (base pointer) and esp (stack pointer) processor registers. the stack is used for a wide variety of tasks, like passing function parameters for example. but ! you have to be aware that this fast-accessible memory space is limited by the system itself, because of its properties. for instance, if there is no more space in the stack, your process can’t run anymore, because it cannot call functions. when a program tries to allocate too much memory on the stack, the system simply kills it (resulting in NZEC on codechef judge). to understand better the concept, you should look for internet pages talking about stack, recursive functions, terminal-recursive functions, calling conventions, etc.

when you are allocating memory globally, the memory is not taken from the stack anymore, but from the heap. thus, it is not restricted by the functions scopes, and can be accessed in the whole running process of your program (not outside, of course). the heap memory can be allocated freely until you have no more memory on your system. accessing it is not as fast as the stack, because the space is managed by a system called MMU (memory management unit) that maps the physical memory into addressable space. yeah, even if this memory space looks contiguous, the system is free to use the physical memory uncontiguously, by allocating random memory pages where they are available. it’s a really wider memory space than the stack. thus, it is a far better place for storing big structures (moreover if they can be shared between functions). i hope it’ll help you to understand. :slight_smile:

2 Likes

Thank you for the reply , you got my point and i have got the idea of what was this all about. I will google it and tell you if i have any problem.