Thread: How to silence integer overflow warning

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,735

    How to silence integer overflow warning

    Managed to get a working macro that can calculate the size of an integer type based on the unsigned max given (up to supported size anyway), the problem I'm having is silencing the preprocessor warnings, I know in VC this could be circumvented by using hard integers (because it for some reason supports using them in the preprocessor, at least in the cases I tried anyways) but how would I go about silencing them in various other compilers (like gcc & clang)

    Incidentally I have -Wall and -Wextra set at the moment, also here's the snippet with an example usage:
    Code:
    #define MAX_FOR_SIZE(PRV) ((PRV * PRV) + PRV + PRV)
    #define MAX_FOR_1BYTE UCHAR_MAX
    #define MAX_FOR_2BYTE MAX_FOR_SIZE(MAX_FOR_1BYTE)
    #define MAX_FOR_4BYTE MAX_FOR_SIZE(MAX_FOR_2BYTE)
    #define MAX_FOR_8BYTE MAX_FOR_SIZE(MAX_FOR_4BYTE)
    
    #define _SIZEOF(UMAX,SIZE,TRUE) \
    	(((UMAX) > (MAX_FOR_##SIZE##BYTE)) ? (TRUE) : SIZE)
    #define SIZEOF(UMAX) \
    	_SIZEOF(UMAX,1,_SIZEOF(UMAX,2,_SIZEOF(UMAX,4,_SIZEOF(UMAX,8,16))))
    
    #ifndef SIZEOF_INTEGRAL
    #if 0
    #if MAX_FOR_2BYTE <= MAX_FOR_1BYTE
    #define SIZEOF_INTEGRAL 1
    #elif MAX_FOR_4BYTE <= MAX_FOR_2BYTE
    #define SIZEOF_INTEGRAL 2
    #elif MAX_FOR_8BYTE <= MAX_FOR_4BYTE
    #define SIZEOF_INTEGRAL 4
    #elif MAX_FOR_8BYTE <= ~0u
    #define SIZEOF_INTEGRAL 8
    #else
    #define SIZEOF_INTEGRAL 16
    #endif /* RESULT <= ORIGINAL */
    #else
    #define SIZEOF_INTEGRAL SIZEOF(~0u)
    #endif
    #endif /* SIZEOF_INTEGRAL */
    Edit: Also here's some output using those very values:
    Code:
    ...
    ./alu.AppImage
    test.c:257: main() MAX_FOR_1BYTE = FF
    test.c:258: main() MAX_FOR_2BYTE = FFFF
    test.c:259: main() MAX_FOR_4BYTE = FFFFFFFF
    test.c:260: main() MAX_FOR_8BYTE = FFFFFFFF
    test.c:261: main() '==========================================='
    test.c:262: main() SIZEOF_INTEGRAL = 4
    test.c:263: main() SIZEOF_SHRT = 2
    test.c:264: main() SIZEOF_INT = 4
    test.c:265: main() SIZEOF_LONG = 8
    test.c:266: main() '==========================================='
    test.c:267: main() UCHAR_MAX = FF
    test.c:268: main() USHRT_MAX = FFFF
    test.c:269: main() UINT_MAX = FFFFFFFF
    test.c:270: main() ULONG_MAX = FFFFFFFFFFFFFFFF
    test.c:271: main() '==========================================='
    test.c:273: main() 'Initiating ALU to 0...'
    test.c:276: main() 'Pre-allocating 16 ALU registers...'
    Compilation finished successfully.
    Last edited by awsdert; 08-04-2020 at 10:44 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Definitive Guide To Integer Overflow.
    By Dren in forum C Programming
    Replies: 4
    Last Post: 11-08-2019, 01:56 PM
  2. Replies: 6
    Last Post: 09-30-2015, 08:49 AM
  3. integer overflow
    By John Connor in forum C Programming
    Replies: 11
    Last Post: 02-11-2008, 05:30 PM
  4. silence warning when assigning pointers
    By eth0 in forum C Programming
    Replies: 5
    Last Post: 10-27-2005, 11:18 AM
  5. how to handle integer overflow in C
    By kate1234 in forum C Programming
    Replies: 8
    Last Post: 04-23-2003, 12:20 PM

Tags for this Thread