Search code examples
c++avr

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


Compiling some test code in avr-gcc for an 8-bit micro-controller, the line

const uint32_t N = 65537;
uint8_t values[N]; 

I got the following compilation warning (by default should be an error, really)

warning: conversion from 'long unsigned int' to 'unsigned int' changes value from '65537' to '1' [-Woverflow]
uint8_t values[N];

Note that when compiling for this target, sizeof(int) is 2.

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

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

Before somebody remarks that an 8-bit microcontroller generally does not have enough memory for an array so large, let me just anticipate saying that this is beside the point.


Solution

  • 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.