Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will sizeof always be a multiple of alignof?

Is sizeof(Type) always divisible by alignof(Type)

such that this statement will always be true? sizeof(Type) % alignof(Type) == 0

like image 445
Anne Quinn Avatar asked Jan 27 '19 02:01

Anne Quinn


People also ask

What is Alignof?

Alignof operator in C++ Operator is a symbol which is used to indicate the compiler to perform some operation in programming language. alignof operator is the operator that returns the alignment that is to be applied to the given type of variable. The returned value is in bytes.

How does sizeof work on arrays?

The sizeof() operator returns pointer size instead of array size. The 'sizeof' operator returns size of a pointer, not of an array, when the array was passed by value to a function. In this code, the A object is an array and the sizeof(A) expression will return value 100.

What does sizeof buffer return?

Description. The sizeof operator returns the number of bytes in a variable type, or the number of bytes occupied by an array.

What is the difference between sizeof and alignof in C++?

hence the sizeof value is the total size required for the given data type; and alignof value is the alignment requirement of the largest element in the structure. Use of alignof : allocate memory on a particular alignment boundary. The example is wrong, as sizeof always follows the constraint sizeof (Type) % alignof (Type) == 0.

What is the use of alignof?

Use of alignof : allocate memory on a particular alignment boundary. The example is wrong, as sizeof always follows the constraint sizeof (Type) % alignof (Type) == 0. If the alignment is indeed 8 bytes, then the size is at least 16 bytes. That padding is guaranteed to happen. What's the difference between sizeof and alignof? Both are operators.

What is the difference between sizeof and return alignment in bytes?

Returns alignment in bytes (an integer power of two) required for any instance of the given type - en.cppreference.com/w/cpp/language/alignof. sizeof just gives the size, in bytes, of course. Well, "memory" is basically a huge array of bytes.

What is the difference between sizeof_alignof and sizeof_alignof?

SIZEOF_ALIGNOF (double); SIZEOF_ALIGNOF (complex double); SIZEOF_ALIGNOF (div_t); SIZEOF_ALIGNOF (max_align_t); 8/8 16/8 8/4 32/16 The sizeof operator gives you the size in bytes of an actual type or instance of a type. The alignof operator gives you the alignment in bytes required for any instance of the given type.


1 Answers

Yes, sizeof(Type) % alignof(Type) == 0 is true for all class types.

The standard draft says:

[dcl.array] ... An object of array type contains a contiguously allocated non-empty set of N subobjects of type T.

[expr.sizeof] ... When applied to a class, the result is the number of bytes in an object of that class including any padding required for placing objects of that type in an array.

In order for every element of an array to be aligned, the distance between two adjacent elements must be a multiple of the alignment. sizeof is defined to be this distance.

Interestingly, for fundamental types other than narrow character type, sizeof is just implementation defined:

[expr.sizeof] ... The result of sizeof applied to any other fundamental type (6.7.1) is implementation-defined.

That said, I've never seen a system where the size of a fundamental type hasn't been a multiple of its alignment. They have to be aligned in an array as well after all.

like image 96
eerorika Avatar answered Sep 27 '22 23:09

eerorika