I'm working on an existing C++ codebase that happens to use SIZE_MAX
in a couple of places. I did some refactoring and now SIZE_MAX
is not defined in one of the modules. This problem appeared when Travis-CI attempted to build the project on Linux. It worked fine before I refactored stuff, but tracing which exact header files were included is difficult.
In an attempt to replicate the problem locally, I installed an Ubuntu VM with the default gcc and was able to reproduce it. Here's the relevant source:
#include <stddef.h>
int main()
{
size_t a = SIZE_MAX;
}
The command line is simply:
g++ a.cpp
The error is:
a.cpp: In function ‘int main()’:
a.cpp:5:16: error: ‘SIZE_MAX’ was not declared in this scope
System info:
$ uname -a
Linux quartz 3.11.0-15-generic #25~precise1-Ubuntu SMP Thu Jan 30 17:39:31 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
$ gcc --version
gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
I have tried including cstdint
, stdint.h
, limits.h
, inttypes.h
, stdio.h
, stdlib.h
, and probably some others, and I can't figure out which specific header file I need for SIZE_MAX
.
It is important to note that the program I'm working on compiled fine, with SIZE_MAX
used in various places, before I made some changes. The changes I made caused it to become undefined in one .cpp
source file where it was used (the others continue to be fine). So there exists some header file on my system where it is correctly defined.
size_t is a base unsigned integer memsize-type defined in the standard library of C/C++ languages. This type is described in the header file stddef.
It's likely that some header defined __STDC_LIMIT_MACROS
and __STDC_CONSTANT_MACROS
before stdint.h
was included.
Compiling on Linux with g++ -D__STDC_LIMIT_MACROS -D__STDC_CONSTANT_MACROS a.cpp
should fix this issue on the older compilers.
If you'd like to learn more about these macros...
18.4.1 Header <cstdint> synopsis
The header also defines numerous macros of the form:
INT_[FAST LEAST]{8 16 32 64}_MIN
[U]INT_[FAST LEAST]{8 16 32 64}_MAX
INT{MAX PTR}_MIN
[U]INT{MAX PTR}_MAX
{PTRDIFF SIG_ATOMIC WCHAR WINT}{_MAX _MIN}
SIZE_MAX
EDIT
In the current C++11/14 standard, SIZE_MAX
is introduced and mentioned only in <cstdint>
. It is also part of C99
, of which specification C++11 fully includes via the <cxxx>
headers. So it seems it was not defined prior to C++11.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With