What's sizeof(size_t) on 32-bit vs the various 64-bit data models?

后端 未结 4 1530
我在风中等你
我在风中等你 2020-12-01 02:45

On a 64-bit system, sizeof(unsigned long) depends on the data model implemented by the system, for example, it is 4 bytes on LLP64 (Windows), 8 bytes on LP64 (L

相关标签:
4条回答
  • 2020-12-01 03:05

    EDIT: Thanks for the comments - I looked it up in the C99 standard, which says in section 6.5.3.4:

    The value of the result is implementation-defined, and its type (an unsigned integer type) is size_t, defined in <stddef.h> (and other headers)

    So, the size of size_t is not specified, only that it has to be an unsigned integer type. However, an interesting specification can be found in chapter 7.18.3 of the standard:

    limit of size_t

    SIZE_MAX 65535

    Which basically means that, irrespective of the size of size_t, the allowed value range is from 0-65535, the rest is implementation dependent.

    0 讨论(0)
  • 2020-12-01 03:16

    it should vary with the architecture because it represents the size of any object. So on a 32-bit system size_t will likely be at least 32-bits wide. On a 64-bit system it will likely be at least 64-bit wide.

    0 讨论(0)
  • 2020-12-01 03:20

    size_t is 64 bit normally on 64 bit machine

    0 讨论(0)
  • 2020-12-01 03:26

    size_t is defined by the C standard to be the unsigned integer return type of the sizeof operator (C99 6.3.5.4.4), and the argument of malloc and friends (C99 7.20.3.3 etc). The actual range is set such that the maximum (SIZE_MAX) is at least 65535 (C99 7.18.3.2).

    However, this doesn't let us determine sizeof(size_t). The implementation is free to use any representation it likes for size_t - so there is no upper bound on size - and the implementation is also free to define a byte as 16-bits, in which case size_t can be equivalent to unsigned char.

    Putting that aside, however, in general you'll have 32-bit size_t on 32-bit programs, and 64-bit on 64-bit programs, regardless of the data model. Generally the data model only affects static data; for example, in GCC:

    `-mcmodel=small'
         Generate code for the small code model: the program and its
         symbols must be linked in the lower 2 GB of the address space.
         Pointers are 64 bits.  Programs can be statically or dynamically
         linked.  This is the default code model.
    
    `-mcmodel=kernel'
         Generate code for the kernel code model.  The kernel runs in the
         negative 2 GB of the address space.  This model has to be used for
         Linux kernel code.
    
    `-mcmodel=medium'
         Generate code for the medium model: The program is linked in the
         lower 2 GB of the address space but symbols can be located
         anywhere in the address space.  Programs can be statically or
         dynamically linked, but building of shared libraries are not
         supported with the medium model.
    
    `-mcmodel=large'
         Generate code for the large model: This model makes no assumptions
         about addresses and sizes of sections.
    

    You'll note that pointers are 64-bit in all cases; and there's little point to having 64-bit pointers but not 64-bit sizes, after all.

    0 讨论(0)
提交回复
热议问题