I'm using MSVC++.
If I define an std::array
with a size bigger than 2^31 - 1
I get this error:
C2148 total size of array must not exceed
0x7fffffff
bytes
That makes me deduce that the type for the size is a signed 32 bit integer. But why using a signed integer for a size? since there is no negative size, wouldn't it be better if the size can go up to 0xffffffff
?
Here is an interesting thing that happens to me:
If I declare an std::array of size 10000000024
, I get the error above. But if I let the size be 10000000024 * 64
the program compiles without any problme.
That raises two questions in my mind:
1 - why is this happening?
2 - I don't think the stack is that big to hold an array with that size. Is the array allocated on the heap if it's big for example? and if not, what happens if the size of the array does exceed the size of the stack? does it throw an exception?
What is the type of the size of std::array
It is std::size_t
which is an implementation-defined unsigned integer type that is large enough to contain the size in bytes of any object.
1 - why is this happening?
According to the message, this is happening because "total size of array must not exceed 0x7fffffff bytes". This appears to be a limitation of that particular language implementation.
For what it's worth, the C++ standard suggests 0x40000 as a guideline recommendation for minimum limit for the maximum size of an object. 0x7fffffff exceeds that recommendation by a large margin. On a x86-64 Linux system, the maximum size limit for an object is 0x7fffffffffffffff instead.
Is the array allocated on the heap if it's big for example?
Where memory for an object is allocated depends on the type of storage you use, and on the implementation of the language. The size of the object typically does not affect this. std::array
never allocates any dynamic memory.
what happens if the size of the array does exceed the size of the stack?
It does not matter how the size of execution stack is exceeded. The language doesn't specify what happens, nor does it acknowledge the existence of a "stack". It is an implementation specific concept. Thus, what happens depends on the implementation. If you're lucky, the program crashes. If you're unlucky, your secrets leak to the hackers.
User contributions licensed under CC BY-SA 3.0