What is the correct definition of size_t?

First of all, what do I mean by “right definition”?

For example, K & R in "C Programming Language" 2nd ed. , in section 2.2 Data Types and Sizes , make very clear statements about integers:

  • For integer types exist short, intand long. They are needed to display the values ​​of different boundaries.
  • int - This is the "natural" size for certain equipment, and also probably the fastest.
  • Sizes for integer types short, intand longimplementation dependent.
  • But they have limitations.
  • shortand intmust contain at least 16 bits.
  • long must contain at least 32 bits.
  • short> = int> = long.

This is very clear and unambiguous. And this is not a type size_t. In K & R 5.4 Address Arithmetic, they say:

  • ... size_tis the unsigned integer type returned by the operator sizeof.
  • The operator sizeofgives the number of bytes needed to store an object such as its operand.

In the standard draft of C99 in 6.5.3.4 the sizeof operator they say:

  • The value of the result is determined by the implementation, and its type (unsigned integer type) is equal to size_tthat defined in <stddef.h>(and other headers).

B 7.17 General definitions :

  • size_t, which is the unsigned integer type of the result of the sizeof operator;

7.18.3 :

  • size_t SIZE_MAX 65535

- size_t . :

  • , , , size_t.
  • , void *memcpy(void *s1, void const *s2, size_t n); <string.h>
  • int size_t n.
  • , unsigned int.
  • , , size_t.
  • unsigned int - , - , , unsigned int, ?
  • , unsigned long, ?
  • , , unsigned long , long "", long s.
  • , size_t - , . int, - long, , .

, size_t sizeof. size_t . , .

:

  • "" C?
  • 65535, , 16 ? embedded.com , size_t 32 .
  • K & R , int "" int long. size_t, ""?

UPDATE

:

size_t C?

( ).

, size_t, size_t, .

+2
4

"" C?

"" - . C99 : " , " ( 3.14). " ". , . , char int, , . , , .

65535, , 16 ? embedded.com , size_t 32 .

. 7.18.3. SIZE_MAX size_t, . , , , . .

K & R , int "" int long. size_t, ""?

, , ( " " ). , int long , , size_t, . size_t , .

:

, size_t, size_t, .

size_t sizeof. , .

size_t , . . : size_t , , (, strlen()) (, sizeof).

size_t , , - (, ).

+3

65535, , 16 ?

16 .

ISO C 1999 (C99) size_t 16 (. 7.17 7.18.3).

size_t?

size_t - , .

size_t ( , ) unsigned char, unsigned short, unsigned int, unsigned long unsigned long long, .

unsigned int unsigned long size_t, , .

- , .

unsigned int ; , unsigned long unsigned long long.

, size_t , , , .

+2

() " ". , .

, size_t,

, sizeof() malloc(). .

size_t,

( ) .

.

, .

+1

size_t

size_t , sizeof.

size_t

, , , . , . ptrdiff_t.

, . , .

, , struct, struct s, struct s - . .

The type is also convenient for use with non-negative indices, since it can represent an index into a structure of any size with a maximum degree of detail (i.e., an index into the maximum possible array chars, since the standard one requires the charsmallest possible size 1).

+1
source

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


All Articles