St.Antario
St.Antario

Reputation: 27385

Allocation function and pointer to returned

I not understood suitable pointer alignment concept:

There are no constraints on the contents of the allocated storage on return from the allocation function. The order, contiguity, and initial value of storage allocated by successive calls to an allocation function are unspecified. The pointer returned shall be suitably aligned so that it can be converted to a pointer of any complete object type with a fundamental alignment requirement (3.11) and then used to access the object or array in the storage allocated (until the storage is explicitly deallocated by a call to a corresponding deallocation function).

There is no definition of suitable alignment in the sec.3.11. You explain that should mean?

Upvotes: 2

Views: 240

Answers (3)

ecatmur
ecatmur

Reputation: 157354

This means that for any complete object type with a fundamental alignment, it should be possible to convert the pointer returned to a pointer to that object type, respecting the alignment requirement of that object type.

In practice, since alignments are powers of two, this means that an allocation function is required to return a pointer aligned to alignof(std::max_align_t).

There is no separate definition of "suitable alignment"; in this paragraph as elsewhere "suitably" just means that there is a requirement which the program is required to satisfy for the rest of the paragraph to hold.

Upvotes: 3

Brian Bi
Brian Bi

Reputation: 119174

§3.11/1 says,

Object types have alignment requirements (3.9.1, 3.9.2) which place restrictions on the addresses at which an object of that type may be allocated.

So if a pointer is "suitably aligned" it means that the address represented by the pointer satisfies these restrictions. What exactly this means for the numerical value of the address is implementation-defined.

Upvotes: 3

Khouri Giordano
Khouri Giordano

Reputation: 1461

Alignment is defined by the OS and platform. Usually it is the size of the largest basic type (a pointer or double), but it could be more. For instance, on Windows, x86 is 8 byte and x64 is 16 byte.

Upvotes: 2

Related Questions