What is meant by the most restrictive type in C?
Asked Answered
A

3

14

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

Although machines vary, for each machine there is a most restrictive type: if the most restrictive type can be stored at a particular address, all other types may be also. On some machines, the most restrictive type is a double; on others, int or long suffices.

In their code, the union header is aligned using the type long.

What is meant by the most restrictive type? Is it perhaps the largest type (e.g., double), or is there another approach?

Andromeda answered 7/3, 2015 at 14:46 Comment(2)
define an union of all types and see the size of the union to find out the most restrictive typeSubminiaturize
@BsD - that gives you the largest type. It may or may not be the most restrictive.Tigress
P
16

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.)

Petrina answered 7/3, 2015 at 15:11 Comment(0)
I
5

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.

Illbred answered 7/3, 2015 at 15:13 Comment(3)
I'm not the downvoter, but maybe they wanted a higher-level overview of the concept of alignment requirements of types. Digging into max_align_t and passages from the standard might be a bit overkill before you get that part.Petrina
@Petrina Fair enough, but that doesn't really justify a downvote.. The question wasn't entirely clear to me, and my answer was specifically geared towards the "What means the most restrictive type, maybe the largest type size (f.e. double) or there is a another approach" part of the question.Illbred
Yeah, I personally only downvote spam and answers with obviously horrible advice. I agree that it's crappy to not give a reason.Petrina
S
2

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.

Springwood answered 7/3, 2015 at 14:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.