What does POSIX mean under the “two-complement view” in stdint.h?

It is well known that signed integer overflow causes undefined behavior, and bitwise manipulation of whole characters is unreliable at best. So I found this line in the POSIX standard quite curious:

The name typedef int N_t denotes a signed integer type with a width of N, no padding bits, and a two-component representation. Thus, int8_t denotes an integer type with a sign exactly 8 bits wide.

This is a rather vague statement that can mean any combination of these things:

  • Range intN_tfrom INTN_MINto INTN_MAX.
  • sizeof(intN_t) == N/8
  • Bitwise operations intN_tdo not behave as expected to present two additions. -1 ^ x == ~xfor each x, after insertion intN_tis performed everywhere.
  • intN_t cannot have trap representations (and the optimizing compiler should not use possible traps).
  • Variable overflows are intN_tdetermined by behavior (and wrappers from INTN_MAXto INTN_MIN).

(1) and (2) both seem pretty obvious to me based on the rest of the document. (1) is explicitly defined by the definition of INTN_MIN/ MAX. (2) implies "no fill bits".

Which of (3), (4) and (5) are required by POSIX, if any?

+4
source share
1 answer

TL DR

1, 3, 4 C99, C11, intN_t . 2 C11, int8_t, int8_t , CHAR_BIT 8. 5, , C - undefined.

POSIX C, CHAR_BIT 8, - . C99/C11 POSIX int8_t, 1, 2, 3 4 true POSIX. POSIX , undefined, 5 .


C11 (C99). C11 7.20.1.1p1:

typedef intN_t N, . , int8_t 8 .

int8_t C, . C11 7.20.1.1p3:

. , 8, 16, 32 64 , ( ), , typedef.

  • , , . int INT_MIN INT_MAX . , INTN_MIN gif.latex? -2% 5E% 7BN-1% 7D.

  • . sizeof(intN_t) N / CHAR_BIT. POSIX , CHAR_BIT 8, sizeof(intN_t) N / 8

  • ,

  • , 2 .

  • C, POSIX.


int8_t POSIX, C99, C11, POSIX . POSIX : *, CHAR_BIT 8 , C *, , C

C99, C11 , , intN_t 2 . int8_t, sizeof 1, signed char, CHAR_BIT 8.

intN_t, , , . :

int32_t *foo = malloc(sizeof(int32_t));
printf(PRId32 "\n", *foo);
printf(PRId32 "\n", *foo);
free(foo);

malloc; puts("42\n666"); - int32_t. , malloc:

[...] , , .

;


undefined.

+8

Source: https://habr.com/ru/post/1683309/


All Articles