I have a complicated program with fixed-point arithmetic.
It seems that there are occasional overflows.
To find them I have set the flag -ftrapv
. This seems to work only with int32_t
. Is that correct?
Is there a way to achieve the same behavior with int16_t
and int8_t
?
Here is my test code:
#include <stdio.h>
#include <stdint.h>
int main(void)
{
int8_t int8 = 127;
int8 += 1;
printf("int8: %d\n",int8);
int16_t int16 = 32767;
int16 += 1;
printf("int16: %d\n",int16);
int32_t int32 = 2147483647;
int32 += 1;
printf("int32: %d\n",int32);
}
I compile with:
rm a.out; gcc -ftrapv main.c && ./a.out
and get:
int8: -128
int16: -32768
Aborted
My compiler version is gcc (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
.
Note: Some of the answers refer to a test program that I have incorrectly written before.