What is meant by the most restrictive type in C?

前端 未结 3 797
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-11 13:02

The book The C Programming Language talks about \"the most restrictive type\" in section 8.7, Example — A Storage Allocator:

A

相关标签:
3条回答
  • 2021-01-11 13:32

    The most restrictive type is defined by max_align_t, which is defined in stddef.h. According to the standard:

    A fundamental alignment is represented by an alignment less than or equal to the greatest alignment supported by the implementation in all contexts, which is equal to _Alignof (max_align_t).

    So max_align_t has an alignment that is at least as large as that of every scalar type, and in most implementations its alignment will be equal to the largest scalar type - but this equality is not required by the standard.

    The standard further requires (emphasis mine):

    The order and contiguity of storage allocated by successive calls to the aligned_alloc, calloc, malloc, and realloc functions is unspecified. The pointer returned if the allocation succeeds is suitably aligned so that it may be assigned to a pointer to any type of object with a fundamental alignment requirement and then used to access such an object or an array of such objects in the space allocated (until the space is explicitly deallocated).

    So any pointer returned by allocation functions is aligned at least as strictly as the alignment of max_align_t.

    0 讨论(0)
  • 2021-01-11 13:38

    I think that the quote means the most restrictive alignment of types. For example char is the least restructive type if to follow this logic. An object of type char does not impose a constraint on its alignment while for example type int has alignment requirement of usually 4 byte bounds. Thus int is a more restrictive type than char.

    0 讨论(0)
  • 2021-01-11 13:47

    CPUs often require that (or work more efficiently if) certain types of data are stored at addresses that are a multiple of some (power-of-two) value. This value is called the alignment of the data. For example, a CPU might require that four-byte integers are stored at addresses that are a multiple of four (that they have four-byte alignment, or are aligned on four bytes).

    By the most restrictive type, they mean the type that has the most restrictive requirements in this area. So if e.g. long double requires eight-byte alignment on some machine, and no other type requires greater alignment than that, then the most restrictive type on that machine would be long double.

    It makes sense for malloc(3) to return addresses that satisfy the alignment requirements of the most restrictive type, since that means the returned memory can be used to store any type. malloc() doesn't know how the memory will be used, so it can't adapt.

    It's not necessarily the case that larger data types require greater alignment, though alignment requirements tend to increase with increasing size.

    (Some types of data might require even greater alignment than malloc() provides. For example, many x86 SSE instructions use data vectors that are aligned on 16 bytes, while e.g. the malloc() in glibc only guarantees eight-byte alignment. posix_memalign(3) can be used to dynamically allocate memory with even greater alignment requirements on POSIX (*nix) systems.)

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