Is portable size?

GCC 4.4.1, C99

I use size_t, and size_t- unsigned int. However, this depends on whether the 32-bit or 64-bit version is running.

I will use size_tto store the size of the buffer.

Thus, I do not think that it would be very portable if you would use all architectures.

Just a question using size_t32 or 64 bit. What situations can cause the most serious problem?

+3
source share
8 answers

size_tguaranteed to be able to hold the number of bytes of any object in your implementation. Therefore, the return type sizeofis equal size_t.

, .

+6

, size_t sizeof() . :

  • size_t - , . , , unsigned int, unsigned long ..
  • sizeof(size_t) - , , memcpy ' , uintmax_t, . , , , uintmax_t.
  • size_t size_t , .
  • size_t sizeof(size_t) .

, unsigned char. , size_t , .

+4

malloc() read() size_t ssize_t . SIZE_MAX, SSIZE_MAX, sizeof (type-in-your-buffer) % zd % zu printf().

+1

off_t ptrdiff_t/ssize_t, .

, . 32- 32 , 64- 64 . , - 32- size_t 32- , 64- .

ints, longs - . , (32 32- , 64- 64- Unix-, 32- 64- Windows).

+1

, "" . "" .

size_t . . , sizeof(). size_t , 100%, , , .

"", , .

+1

, size_t int (. ), , .

0

, size_t .

, , size_t , . , , , .

, , , 32- ( 64- ).

0

size_t.

, "int" 64-, 32-, int 32- .

-2
source

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


All Articles