Questions tagged [alignof]

21 questions
72
votes
8 answers

What's the difference between sizeof and alignof?

What's the difference between sizeof and alignof? #include #define SIZEOF_ALIGNOF(T) std::cout<< sizeof(T) << '/' << alignof(T) << std::endl int main(int, char**) { SIZEOF_ALIGNOF(unsigned char); SIZEOF_ALIGNOF(char); …
user1494506
  • 1,283
  • 1
  • 12
  • 9
49
votes
4 answers

Is it always the case that sizeof(T) >= alignof(T) for all object types T?

For any object type T is it always the case that sizeof(T) is at least as large as alignof(T)? Intuitively it seems so, since even when you adjust the alignment of objects like: struct small { char c; }; above what it would normally be, their…
BeeOnRope
  • 60,350
  • 16
  • 207
  • 386
19
votes
3 answers

`std::alignment_of` versus `alignof`

I've just stumbled upon the std::alignment_of type trait, and its soon-to-be friend std::alignment_of_v. They seem to have been specifically designed to be equivalent to a plain call to alignof, and the future addition of the _v helper indicates…
Quentin
  • 62,093
  • 7
  • 131
  • 191
8
votes
1 answer

Alignment and size of C++ primitive types

In C++, it seems that for all integer types (int, long long int, std::uint16_t, ...) and for the floating point types, it is always sizeof(T) == alignof(T). Is this compiler/platform-specific, or guaranteed to be true? Could there be a platform…
tmlen
  • 8,533
  • 5
  • 31
  • 84
5
votes
1 answer

How to align a struct member in D?

I tried this struct Foo(T) { align(8) void[T.sizeof] data; } but static assert(Foo!(int).data.alignof == 8); fails, telling me the alignment is still 1 instead of 8. Why is this, and how do I fix it, so that it works for any arbitrary…
user541686
  • 205,094
  • 128
  • 528
  • 886
4
votes
1 answer

Writing an alignof(T) method in C#?

Tried implementing alignof(T) formula from this web page but it doesn't match with Unity's: template struct alignof { enum { s = sizeof (T), value = s ^ (s & (s - 1)) }; }; The actual result, the first sizeof/alignof come from…
aybe
  • 15,516
  • 9
  • 57
  • 105
4
votes
1 answer

Is alignof(T*) is the same for all possible types? What about sizeof(T*)?

Is alignof(T*) is the same value for all possible types T? What about sizeof(T*)? Please answer based on what is allowed/specified by the standard and not what is the current situation in different compilers.
Koosha
  • 1,492
  • 7
  • 19
4
votes
1 answer

Why is alignof(max_align_t) 16?

I'm running Debian on an x86_64 Intel processor. gcc (Debian 8.3.0) compiles the following program #include #include #include int main(){ printf("%zd\n",alignof(max_align_t)); } and outputs 16 What datatype…
Willis Hershey
  • 1,520
  • 4
  • 22
4
votes
1 answer

Why doesn't gcc honor alignment requirements?

Consider the following C++ code that prints the alignment requirement for a double and the effective alignment of a structure member of type double: #include struct S { short x; double y; }; int main() { S s; std::cout <<…
François Beaune
  • 4,270
  • 7
  • 41
  • 65
3
votes
1 answer

sizeof and types, guarantees

I cannot find a proof/disproof that the following code snippet has no design flaws, speaking about the correctness. template class MyDirtyPool { public: template std::size_t add(Args &&... args) { if…
varnie
  • 2,523
  • 3
  • 35
  • 42
2
votes
1 answer

Effective type in packed raw data

In order to avoid memory fragmentation on very large datasets, I have implemented a doubly linked list that avoid calling malloc twice: one malloc for the data and another one for the prev and next nodes. Instead, it allocates the required space in…
David Ranieri
  • 39,972
  • 7
  • 52
  • 94
2
votes
0 answers

Does `T` always have the same size and alignment as `std::aligned_storage`

Under what circumstances does the standard require that T has exactly the same size and alignment as std::aligned_storage ? I thought the answer was, always, but from other SO answers today, I learned that for some obscure T,…
Chris Beck
  • 15,614
  • 4
  • 51
  • 87
2
votes
2 answers

How to tell the maximum data alignment requirement in C++

Referencing this question and answer Memory alignment : how to use alignof / alignas? "Alignment is a restriction on which memory positions a value's first byte can be stored." Is there a portable way in C++ to find out the highest granularity of…
Curious
  • 20,870
  • 8
  • 61
  • 146
2
votes
0 answers

When did Clang gain support for C++11's alignof keyword?

Clang first claimed support, via __has_feature(cxx_alignof), for C++11's alignof keyword in r223186 (December 2014). This corresponds to Clang 3.6 by the Clang project's marketing version number. But that revision only implements the feature-test:…
Jeff Walden
  • 7,008
  • 2
  • 38
  • 55
2
votes
1 answer

How do I fix "The program issued a command but the command length is incorrect." error when calling Process32First()?

GetLastError tells me I'm getting the "The program issued a command but the command length is incorrect." error when calling Process32First() (see code below). I found one post that looked helpful…
Jorge Peach
  • 157
  • 3
  • 9
1
2