Large local array causes compiler stack Overflow problem

Source: Internet
Author: User

In the beginning of the ACM Road, many times will encounter a large data range, and to use the array to store, you may encounter the following problems:

#include <stdio.h>int main () {    int n, a[10000005];   Local    while (~SCANF ("%d", &n)) {for        (int i=0; i<n; i++) scanf ("%d", &a[i]);        for (int i=0; i<n; i++) printf ("%d\n", A[i]);    }    return 0;}

compile and run when the error occurs immediately!!!!

But then did not delve into, next to the seniors just put the array mentioned in front of the main function, and then tell me that in the future to define a large array when you remember to define it as a global variable;

Just like that:

#include <stdio.h>int a[10000005];   Global int main () {    int n;    while (~SCANF ("%d", &n)) {for        (int i=0; i<n; i++) scanf ("%d", &a[i]);        for (int i=0; i<n; i++) printf ("%d\n", A[i]);    }    return 0;}

then it compiles and runs successfully! It was amazing!!

Today, Heilongjiang Province, Guangdong Chamber of Commerce carnival has come back tired, just think of a lot of knowledge to tidy up a bit:

To solve this problem, first you need to understand how all variables and local variables are stored in memory:


1, Stack sagment: The compiler automatically allocates the release, the value of the parameter that holds the function, the value of the local variable, and so on. Under Windows, the stack is the data structure that extends to the low address, which is a contiguous area of memory. This sentence means that the top of the stack address and the maximum capacity of the stack is the system pre-defined, in Windows, the size of the stack is 2M (also some 1M, in short, a compile-time determination of the constant), if the requested space exceeds the stack's remaining space, will prompt overflow. Therefore, the space available from the stack is small.

2, Heap area (heap sagment): Generally by the programmer allocation release, if the programmer does not release, the program ends may be collected by the system. It is different from the heap in the data structure. A heap is a data structure that extends to a high address and is a discontinuous area of memory. This is because the system is stored with a linked list of free memory address, is naturally discontinuous, and the chain of the list of traversal direction is from the low address to high address. The size of the heap is limited by the valid virtual memory in the computer system. Thus, the space of the heap is more flexible and relatively large.

3. Global zone (static zone) (data sagment): The storage area of global variables and static variables is together and released by the system after the program ends. The size of the data area is limited by the system and is generally large.

4, text constant area: the constant string is placed here, the program is released after the end of the system.

5, program code area: the binary code that holds the function body.


Based on the knowledge above, it is clear that the size of the area is so small that it cannot save large local variables, it will inevitably overflow, and the global variable storage area will have enough space to save the local number of storage area;


Large local array causes compiler stack Overflow problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.