What type is used in C++ to define an array size?

size_t is considered as the type to use, despite not being formally ratified by either the C or C++ standards.

The rationale for this is that the sizeof(values) will be that type (that is mandatated by the C and C++ standards), and the number of elements will be necessarily not greater than this since sizeof for an object is at least 1.


So it seems that, at an array size cannot exceed the size of an unsigned int.

That seems to be the case in your particular C[++] implementation.

Am I correct? Is this gcc-specific or is it part of some C or C++ standard?

It is not a characteristic of GCC in general, nor is it specified by either the C or C++ standard. It is a characteristic of your particular implementation: a version of GCC for your specific computing platform.

The C standard requires the expression designating the number of elements of an array to have an integer type, but it does not specify a particular one. I do think it's strange that your GCC seems to claim it's giving you an array with a different number of elements than you specified. I don't think that conforms to the standard, and I don't think it makes much sense as an extension. I would prefer to see it reject the code instead.


In your implementation size_t is defined as unsigned int and uint32_t is defined as a long unsigned int. When you create a C array the argument for the array size gets implicitly converted to size_t by the compiler.

This is why you're getting a warning. You're specifying the array size argument with an uint32_t that gets converted to size_t and these types don't match.

This is probably not what you want. Use size_t instead.

Tags:

C++

Avr