A [1000] [1000] program crash, 1000 program crash

Source: Internet
Author: User

A [1000] [1000] program crash, 1000 program crash

1000*1000 is greater than 65536. If not required, there is no need to open up so much space. Because these spaces are applied on the stack (if they are local variables), the stack space is limited and valuable, opening up too much space and not applying it is likely to cause memory leakage.

The array subscript is generally of the int or unsigned int type. on 32-bit machines, it is sufficient to define a subscript range of more than 2 billion. Generally, an error in a large array is not caused by it, but because the array consumes the stack space.

To define a large array, try to put it in the heap instead of the stack. Stack space is very limited and should not be wasted.

There are many ways to put it in the heap: Create with new, define it as a static array, define it as a global array, and so on.

Details:

Static int a [1000] [1000];
Add static before the local array declaration that occupies a large memory space to move it from the stack data segment to the global data segment.

Int (* p) [MAX] = new int [MAX] [MAX];

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.