Home  >  Q&A  >  body text

在C/C++中数组长度的最大值是多少,跟哪些因素有关系?

PHP中文网PHP中文网2713 days ago1031

reply all(4)I'll reply

  • PHP中文网

    PHP中文网2017-04-17 14:57:49

    After a long time, I managed to sum up an answer.

    • First of all, if we want to determine the maximum length of an array, we need to know its data type. Therefore, the data type of the array is one of the limiting factors. Because elements of different data types have different sizes. Obviously (we assume that int is 4 bytes and char is 1 byte), the maximum length of an array of data type char is 4 times that of an array of type int.

    • In addition, it is not difficult for us to think that the data type of size(size), that is, the data type of the array subscript, is actually a limiting factor. In C/C++, the type of array subscript is std::size_t, so the size of the array cannot exceed the size that can be represented by size_t. This data type is declared in the library file stdio.h via typedef, which is defined as unsighed int for 32-bit programs and unsigned long for 64-bit programs. The maximum size that the former can represent is 2^32-1, and the latter is 2^64-1.

    • Then, I thought about the size of physical memory. This goes without saying, because once the size of the physical memory is exceeded when the program is running, the program will crash immediately.

    The above points are relatively general restrictions. Let’s change to a higher-level expression - Macroscopic limiting factors. Below, we introduce several microscopic limiting factors.

    • Everyone should know that there are roughly two ways to allocate arrays: static allocation and dynamic allocation. To be more specific, according to the position of the array declaration, we can divide the array into local array and global array. If discussed in this way, it will be complicated. We can temporarily divide arrays into four categories (it’s just the author who divides them this way, I don’t know if there is such an official classification): statically allocated partial Array, dynamically allocated local array, statically allocated global array, dynamically allocated global array.

      • The first type of array allocation uses space on the stack, so the size of the local array obtained by static allocation is limited to the size of the stack. Specifically, it is the size of the stack frame of the function where the array is located. Of course, the size of the stack frame must not exceed the size of the stack. If you are familiar with using a compiler or have read the compiler documentation, you should know how to adjust the stack frame size limit. Under WINDOWS, the stack size is 2M (some say it is 1M, in short it is a constant determined at compile time). If the applied space exceeds the remaining space of the stack, an overflow will be prompted.

      • For the second type of array and the fourth type of array, I think they should be divided into the same category. Essentially they allocate space on the heap, so their size is limited by the size of the heap. The heap is a discontinuous memory area. The size of the heap is limited by the effective virtual memory in the computer system, so the size of the heap is generally relatively large.

      • As for the third array, we know that it allocates memory space in the static storage area, so the size is naturally limited by the size of the static storage area, also called BSS (Block Started by Symbol), in assembly language , we also call it the data segment. At present, I am not sure about the size limit of the static storage area. I have done experiments on my computer (Core i3-3110M, 8GB memory) and found that the maximum size I can allocate is about 1/2 of the remaining memory. There is a saying on the Internet: "Your constant is as big as it is", but it has yet to be verified.

    Note: For the concept of stack frames, you can refer to the book "In-depth Understanding of Computer Systems" or other information about computer architecture.

    Already organized into a blog: The maximum length of arrays in C++.

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-17 14:57:49

    If it is an array on the stack, the size is limited to the stack size (remember it is 4MB). If it is a heap, it is relatively large. In theory, you can use the maximum memory that the operating system allows the process to use, minus the space occupied by code, constants, etc. in the memory. But if you have to use them all, the program will not be far from crashing.

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 14:57:49

    It is related to the addressing bit width of the CPU and the addressing mode of the operating system

    In flat addressing mode of virtual memory

    The 32-bit pointer addressing range is 2^32 = 4GB

    reply
    0
  • ringa_lee

    ringa_lee2017-04-17 14:57:49

    Arrays can be opened in heap, stack, and static storage area respectively, depending on your needs, CPU support and operating system constraints.

    reply
    0
  • Cancelreply