CKTIS / C
Limites e larguras de inteiros em <stdint.h>
As macros de <stdint.h> descrevem os intervalos e as larguras em bits dos tipos inteiros. Permitem verificar a portabilidade e rejeitar cálculos demasiado grandes antes de os efetuar. C23 acrescenta as macros terminadas em _WIDTH.
Versão: C23; repository compiler flag, production runtime not attested
Sintaxe
#include <stdint.h>
/* Macro name patterns (substitute 8, 16, 32, or 64 for N):
INTN_MIN, INTN_MAX, INTN_WIDTH
UINTN_MAX, UINTN_WIDTH
INT_LEASTN_MIN, INT_LEASTN_MAX, INT_LEASTN_WIDTH
UINT_LEASTN_MAX, UINT_LEASTN_WIDTH
INT_FASTN_MIN, INT_FASTN_MAX, INT_FASTN_WIDTH
UINT_FASTN_MAX, UINT_FASTN_WIDTH
Other macro names:
INTPTR_MIN, INTPTR_MAX, INTPTR_WIDTH
UINTPTR_MAX, UINTPTR_WIDTH
INTMAX_MIN, INTMAX_MAX, INTMAX_WIDTH
UINTMAX_MAX, UINTMAX_WIDTH
PTRDIFF_MIN, PTRDIFF_MAX, PTRDIFF_WIDTH
SIZE_MAX, SIZE_WIDTH
SIG_ATOMIC_MIN, SIG_ATOMIC_MAX, SIG_ATOMIC_WIDTH
WCHAR_MIN, WCHAR_MAX, WCHAR_WIDTH
WINT_MIN, WINT_MAX, WINT_WIDTH
*/<stdint.h> disponibiliza constantes utilizáveis em #if: _MIN e _MAX indicam limites inclusivos; C23 acrescenta _WIDTH para a largura em bits, incluindo o sinal mas excluindo os bits de preenchimento. Para uma largura W, o intervalo com sinal é −2^(W−1)…2^(W−1)−1 e o intervalo sem sinal é 0…2^W−1. As famílias UINT não têm macros de mínimo. No entanto, se SIG_ATOMIC_MIN, WCHAR_MIN ou WINT_MIN estiver definida para um tipo sem sinal, a macro correspondente vale 0.
Para cada N = 8, 16, 32, 64, os padrões de sintaxe abrangem todos os símbolos listados. INTN_MIN, INTN_MAX, INTN_WIDTH, UINTN_MAX e UINTN_WIDTH descrevem exatamente N bits sem preenchimento; estes tipos e macros são opcionais. As famílias INT_LEASTN_*/UINT_LEASTN_* descrevem os tipos mais pequenos com pelo menos N bits; INT_FASTN_*/UINT_FASTN_* descrevem os tipos normalmente mais rápidos com pelo menos N bits, mas não necessariamente os mais rápidos para todas as tarefas. Ambas as famílias são obrigatórias para estes valores de N e podem ser mais largas.
INTPTR_MIN, INTPTR_MAX, INTPTR_WIDTH e UINTPTR_MAX, UINTPTR_WIDTH descrevem os tipos opcionais intptr_t/uintptr_t, que permitem converter void * em inteiro e novamente, preservando a igualdade. INTMAX_MIN, INTMAX_MAX, INTMAX_WIDTH, UINTMAX_MAX e UINTMAX_WIDTH descrevem os tipos obrigatórios intmax_t/uintmax_t; a garantia do maior intervalo tem exceções para tipos de precisão em bits e certos tipos estendidos mais largos.
PTRDIFF_MIN, PTRDIFF_MAX, PTRDIFF_WIDTH descrevem ptrdiff_t (diferenças entre apontadores); SIZE_MAX, SIZE_WIDTH descrevem size_t (tamanhos). Os trios SIG_ATOMIC_MIN/SIG_ATOMIC_MAX/SIG_ATOMIC_WIDTH, WCHAR_MIN/WCHAR_MAX/WCHAR_WIDTH e WINT_MIN/WINT_MAX/WINT_WIDTH descrevem, respetivamente, sig_atomic_t, wchar_t e wint_t. Não presuma que têm sinal ou que são sem sinal. <stdint.h> não tem de declarar estes tipos de outros cabeçalhos; um ambiente freestanding pode omitir alguns.
Verifique as macros opcionais com #ifdef. O exemplo testa count <= SIZE_MAX / size antes da multiplicação, protegendo também contra a divisão por zero. Um resultado positivo significa apenas que o produto é representável, não que a alocação de memória terá sucesso.
Identificadores de referência abrangidos por esta entrada: INT8_MIN, INT8_MAX, INT8_WIDTH, UINT8_MAX, UINT8_WIDTH, INT_LEAST8_MIN, INT_LEAST8_MAX, INT_LEAST8_WIDTH, UINT_LEAST8_MAX, UINT_LEAST8_WIDTH, INT_FAST8_MIN, INT_FAST8_MAX, INT_FAST8_WIDTH, UINT_FAST8_MAX, UINT_FAST8_WIDTH, INT16_MIN, INT16_MAX, INT16_WIDTH, UINT16_MAX, UINT16_WIDTH, INT_LEAST16_MIN, INT_LEAST16_MAX, INT_LEAST16_WIDTH, UINT_LEAST16_MAX, UINT_LEAST16_WIDTH, INT_FAST16_MIN, INT_FAST16_MAX, INT_FAST16_WIDTH, UINT_FAST16_MAX, UINT_FAST16_WIDTH, INT32_MIN, INT32_MAX, INT32_WIDTH, UINT32_MAX, UINT32_WIDTH, INT_LEAST32_MIN, INT_LEAST32_MAX, INT_LEAST32_WIDTH, UINT_LEAST32_MAX, UINT_LEAST32_WIDTH, INT_FAST32_MIN, INT_FAST32_MAX, INT_FAST32_WIDTH, UINT_FAST32_MAX, UINT_FAST32_WIDTH, INT64_MIN, INT64_MAX, INT64_WIDTH, UINT64_MAX, UINT64_WIDTH, INT_LEAST64_MIN, INT_LEAST64_MAX, INT_LEAST64_WIDTH, UINT_LEAST64_MAX, UINT_LEAST64_WIDTH, INT_FAST64_MIN, INT_FAST64_MAX, INT_FAST64_WIDTH, UINT_FAST64_MAX, UINT_FAST64_WIDTH.
Exemplos
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
static int product_fits(size_t count, size_t size)
{
return size == 0 || count <= SIZE_MAX / size;
}
int main(void)
{
#ifdef UINT_LEAST8_WIDTH
_Static_assert(UINT_LEAST8_WIDTH >= 8, "width too small");
#else
_Static_assert(UINT_LEAST8_MAX >= 255, "range too small");
#endif
printf("small: %d\n", product_fits(3, 4));
printf("overflow: %d\n", product_fits(SIZE_MAX, 2));
return 0;
}
Resultado
small: 1 overflow: 0
O código apresentado e o resultado declarado foram verificados num ambiente isolado pelo método registado na prova de verificação desta versão exata.