Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which C++ standard header defines SIZE_MAX?

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.

like image 388
Greg Hewgill Avatar asked May 27 '15 03:05

Greg Hewgill


People also ask

What header file do you need to #include to get the declaration for the type Size_t?

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.


2 Answers

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

like image 178
Bill Lynch Avatar answered Sep 19 '22 23:09

Bill Lynch


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.

like image 22
vsoftco Avatar answered Sep 18 '22 23:09

vsoftco