Talk:cpp/language/sizeof

No architecture requirement for sizeof to be multiple of alignment
I would like to point out that the alignment of a C++ type is only necessary for the memory location, the so called numeric value of the pointer but not the size of the object itself. It just so happens to occur that many native types of many CPU architectures (int, long, short) have the same sizeof as their alignof. Back in the C++0x days we devs actually required this for proper size calculation of arrays. Now it is a misconception. My statement is related to the following paragraph (10.02.2022):

QUOTE START:

When applied to a class type, the result is the number of bytes occupied by a complete object of that class, including any additional padding required to place such object in an array. The number of bytes occupied by a potentially-overlapping subobject may be less than the size of that object.

QOUTE END.

Consider an array of 5 type with data-size of 5 and alignas 2. Under the current C++ standard the size of type has to equal 6, the array size being a total of 30 bytes. But by relaxing the requirement of sizeof to neglect the padding at the end, the first four item of the array should have a size of 6 * 4 == 24 bytes, the last item a byte size of 5, resulting in an actually-required array data size of just 29 bytes. I believe this to be a good change to the C++ standard that would lead to decreased memory footprints and increased performance on all target architectures! Quiret (talk) 08:04, 10 February 2022 (PST)

Packed Structures
It would be nice to have some examples and explanation on this page of the behaviour of sizeof with packed and default alignment structures.

For example, recently I have encountered something like the following:

struct A   { uint32_t a;       int8_t b;    }

A* p = new A[3];

bzero(p, sizeof(A[3]);

I am reasonably confident that I know how the compiler interprets this, but not 100 % sure. Hence I found this page - but without relevant examples.

62.128.218.68 04:59, 31 May 2022 (PDT) Fin


 * It's not clear to me what the confusion is, could you be more specific? As an aside, I recommend, , or in decreasing order of preference, there's no reason to spare mental energy for . --Ybab321 (talk) 06:11, 31 May 2022 (PDT)


 * perhaps "packed and default alignment structures" refers to structs defined with and without cpp/preprocessor/impl? --Cubbi (talk) 11:45, 31 May 2022 (PDT)

Clarification on "Both versions are constant expressions of type std::size_t"
The way this is worded implies std::size_t is part of the core language and causes confusion (source: https://stackoverflow.com/questions/237370/does-stdsize-t-make-sense-in-c).

Although the type "decltype(sizeof(int))" is guaranteed to match the type of std::size_t, I'd argue it is not correct to say that "sizeof" is of type std::size_t because std::size_t is not part of the core language.

My suggestion is to change the sentence Both versions are constant expressions of type std::size_t to Both versions are constant expressions of a type equivalent to std::size_t

My exact wording of "a type equivalent to" might not be language used across cppreference. If that is the case, instead I'd argue it should be changed to something with equivalent meaning. 213.55.224.72 07:29, 18 October 2022 (PDT)


 * There is no "type equivalent to". The type of sizeof is std::size_t: https://eel.is/c++draft/expr.sizeof#5.sentence-1 (it is defined that way in C, too: https://port70.net/~nsz/c/c11/n1570.html#6.5.3.4p5) --Cubbi (talk) 09:59, 18 October 2022 (PDT)


 * FWIW, a lot of core C++ is expressed through the library. Initialiser lists, three-way comparison, placement new... --Ybab321 (talk) 14:18, 18 October 2022 (PDT)