0

I'm doing some integer overflow checking in C++ using the Android NDK. For space reasons, I can't use the full C++ stdlib, so I can't use std::numeric_limits<int>::max(), as suggested here.

What is my best alternative?

CalumMcCall
  • 1,665
  • 4
  • 24
  • 46
  • Have you tried copying just the `numeric_limits` code and adding it to a custom header file? That way you don't have to `include` all of `cstdlib`. – Major Feb 15 '18 at 17:12
  • Is that safe considering the code might run on both ARM and x86? – CalumMcCall Feb 15 '18 at 17:13
  • I'm looking at the limits header file, and I don't see anything specific to ARM vs x86. However, my machine doesn't appear to have a c++config.h header file, so I can't tell for sure. How much extra space does the limits header take up anyway? Have you compared with vs without? – Major Feb 15 '18 at 17:39
  • 2
    Is there any reason why you can't used a fixed size type (e.g. `int32_t`)? – Michael Feb 15 '18 at 18:31
  • @Michael Can you write up your comment as an answer and I'll accept it? – CalumMcCall Feb 19 '18 at 15:16
  • "Have you tried copying just the numeric_limits code and adding it to a custom header file? That way you don't have to include all of cstdlib". The OP is trying to avoid *the* stdlib, i.e. libc++_shared.so. This is very different from trying to avoid the header file cstdlib, which is basically an empty file (it's just `using ::blah;` definitions). – Dan Albert May 14 '18 at 21:00

2 Answers2

1

Use the static STL instead of the shared one. Only the pieces you use will be included in your app and the linker will strip the rest.

Dan Albert
  • 10,079
  • 2
  • 36
  • 79
0

I fixed the issue by copying and hardcoding the limit values from limits.h.

CalumMcCall
  • 1,665
  • 4
  • 24
  • 46