Array length big case

How to deal with the array elements should be too much?

Stack memory space is automatically allocated and freed compiler keep some local variables it
stack is a contiguous memory area, the size of the operating system the scheduled, the stack size of the windows is 2M (there is 1M, operating systems, hardware, etc. For, the compiler section may be provided)

Heap more advanced, the user's own application, the release of space, such as dynamic applications new. I remember the release, do not blame my memory leaks
heap memory area is not continuous, the computer system is limited by the size of the effective virtual memory (32bit system theoretically 4G)
definition of reference
"Windows core programming fifth edition", windows thread stack according to memory default is 1MB, and for heap memory, although the default stack is 1MB, but "the system may increase the default stack process" (to use virtual memory)

Static storage area in the program compile time has been allocated is good, and there during the entire operation of the program, such as global variables. It is great.

Talk so much, it is not important. Important: heap memory is much larger than the stack memory.

Before we return to the question, why I can not define an array of great length?
First, do not take up memory that has opened up so space is not wasted. And wow small stack memory, you open up another big stack memory so p points, or does not meet your needs.
Then how can you do it? There are two ways:

1. The array is defined as a global variable
se is not on the stack in memory and static memory allocation space.
Before finally pave the way to use it! Since we generally use arrays in the main function, it is clearly a local variable, accounting for memory. Then we define the array outside the main function (all functions), then it is a global variable, stored in the static storage area. Static memory than the stack memory ah, naturally longer define the array.

2. The dynamically allocated arrays
per se is not in the stack memory and heap memory allocation space.
Using new dynamic application by the line of the new space, because it is not continuous in the heap memory space, empathy, more space, longer array. Different from the stack memory, automatically reclaim unused space, heap memory space will exist until the end of the program. So remember to use the delete key, but not out of the new space for recycling, to avoid memory leaks.

Guess you like

Origin www.cnblogs.com/jingjing1234/p/10699164.html