Is there a max array length limit in C++?

后端 未结 12 1719
深忆病人
深忆病人 2020-11-22 07:21

Is there a max length for an array in C++?

Is it a C++ limit or does it depend on my machine? Is it tweakable? Does it depend on the type the array is made of?

12条回答
  •  佛祖请我去吃肉
    2020-11-22 08:21

    To summarize the responses, extend them, and to answer your question directly:

    No, C++ does not impose any limits for the dimensions of an array.

    But as the array has to be stored somewhere in memory, so memory-related limits imposed by other parts of the computer system apply. Note that these limits do not directly relate to the dimensions (=number of elements) of the array, but rather to its size (=amount of memory taken). Dimensions (D) and in-memory size (S) of an array is not the same, as they are related by memory taken by a single element (E): S=D * E.

    Now E depends on:

    • the type of the array elements (elements can be smaller or bigger)
    • memory alignment (to increase performance, elements are placed at addresses which are multiplies of some value, which introduces
      ‘wasted space’ (padding) between elements
    • size of static parts of objects (in object-oriented programming static components of objects of the same type are only stored once, independent from the number of such same-type objects)

    Also note that you generally get different memory-related limitations by allocating the array data on stack (as an automatic variable: int t[N]), or on heap (dynamic alocation with malloc()/new or using STL mechanisms), or in the static part of process memory (as a static variable: static int t[N]). Even when allocating on heap, you still need some tiny amount of memory on stack to store references to the heap-allocated blocks of memory (but this is negligible, usually).

    The size of size_t type has no influence on the programmer (I assume programmer uses size_t type for indexing, as it is designed for it), as compiler provider has to typedef it to an integer type big enough to address maximal amount of memory possible for the given platform architecture.

    The sources of the memory-size limitations stem from

    • amount of memory available to the process (which is limited to 2^32 bytes for 32-bit applications, even on 64-bits OS kernels),
    • the division of process memory (e.g. amount of the process memory designed for stack or heap),
    • the fragmentation of physical memory (many scattered small free memory fragments are not applicable to storing one monolithic structure),
    • amount of physical memory,
    • and the amount of virtual memory.

    They can not be ‘tweaked’ at the application level, but you are free to use a different compiler (to change stack size limits), or port your application to 64-bits, or port it to another OS, or change the physical/virtual memory configuration of the (virtual? physical?) machine.

    It is not uncommon (and even advisable) to treat all the above factors as external disturbances and thus as possible sources of runtime errors, and to carefully check&react to memory-allocation related errors in your program code.

    So finally: while C++ does not impose any limits, you still have to check for adverse memory-related conditions when running your code... :-)

提交回复
热议问题