0
  • I have some type T that I explicitly specify as x-aligned
  • x > sizeof(T)
  • x > any implementation fundamental alignment
  • (ex: x is page or cache aligned)

Suppose I now have: T arr[y], where arr is x-aligned (either by being allocated on the stack, or in data, or by an x-aligned heap allocation)

Then at least some of arr[1],...,arr[y-1] are not x-aligned.

Correct? (In fact, it must be correct if sizeof(T) does not change with extended alignment specification)

Note1: This is not the same question as How is an array aligned in C++ compared to a type contained?. This question asks about the alignment of the array itself, not of the individual elements inside.

Note2: This question: Does alignas affect the value of sizeof? is essentially what I'm asking - but for extended-alignment.

Note3: https://stackoverflow.com/a/4638295/7226419 Is an authoritative answer to the question (that sizeof(T) includes any padding necessary to satisfy alignment requirements for having all T's in an array of T's properly aligned.

Community
  • 1
  • 1
Kay
  • 745
  • 5
  • 15
  • Also maybe a duplicate of http://stackoverflow.com/questions/13284208/how-is-an-array-aligned-in-c-compared-to-a-type-contained ? – Aurel Bílý May 17 '17 at 11:31
  • @AurelBílý See note – Kay May 17 '17 at 11:38
  • usably this kind stuff standard do not defined by standard to let compiler to what is right for given platform. Also there are compiler setting which can change this behavior, so code should not assume how things are aligned. – Marek R May 17 '17 at 11:38
  • Array elements are always sizeof T apart. Sizeof T may be larger than the sum of the components of T. – stark May 17 '17 at 11:43
  • It is not clear what you are after. What is your actual problem? – too honest for this site May 17 '17 at 11:44
  • If `alignment > sizeof(T)` then of yes some elements will not be aligned as arrays must be contiguous. Do note that over aligned types are implementation supported and are not portable. – NathanOliver May 17 '17 at 11:45
  • @stark irreverent – Kay May 17 '17 at 11:46
  • @Olaf Specifically, I need an array of elements, where each element is aligned to a cache boundary – Kay May 17 '17 at 11:47
  • @Zed: how is that "irrelevant"? – You May 17 '17 at 11:48
  • I would hate to be accused of blasphemy. – stark May 17 '17 at 11:49
  • That's not guaranteed. As I suspected you have an XY problem. If you need a specific alignment, you should enforce it. You only care about cacheline alignment for the processing block; you most likely **do not want** ever element to be aligned to a cacheline, unless it is not a fundamental type, of course. But before all this: profile your code; often the cache is not the problem. – too honest for this site May 17 '17 at 11:50
  • @stark This has nothing to do with the components of T (assuming, of course, that *x* is larger then any alignment requirement of any component of `T`, recursively) – Kay May 17 '17 at 11:51
  • @Olaf This is a cache issue where I have thread-specific data on the same cache line. Thank you for your response – Kay May 17 '17 at 11:52
  • `...that I explicitly specify as x-aligned` Exactly how do you do that? – Support Ukraine May 17 '17 at 11:58
  • @4386427 [alignas](http://en.cppreference.com/w/cpp/language/alignas), or a host of older, compiler-specific directives (ex. _declspec(align(X)) on MSVC) – Kay May 17 '17 at 12:11
  • @Zed - and you end up with `sizeof(T)` being **less** than the alignment (aka *X*) ? – Support Ukraine May 17 '17 at 12:14
  • @4386427 Thats essentially what the question is about. Say you want `T` aligned on a 64 or 128 byte cache line boundary - does the alignment specifier change the size of T? – Kay May 18 '17 at 05:07
  • @zed - I'm sure it will. In other words, you can't get `sizeof(T)` being less than the alignment. – Support Ukraine May 18 '17 at 05:27

1 Answers1

9

If type T is x-aligned, every object of type T is x-aligned, including any array elements. In particular, this means x > sizeof(T) cannot possibly hold.

A quick test with a couple of modern compilers confirms:

#include <iostream>

struct alignas(16) overaligned {};
struct unaligned {};

template <class T> void sizes()
{
    T m, marr[2];
    std::cout << sizeof(m) << " " << sizeof(marr) << std::endl;
}

int main ()
{
    sizes<unaligned>();
    sizes<overaligned>();
}

Output:

1 2
16 32
n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
  • Thank you for your response. The statement "If type T is x-aligned, every object of type T is x-aligned, including any array elements" is what I'd like documentation for, as this means, as I phrased in the question, extended alignment can change the size of a structure – Kay May 18 '17 at 05:04
  • I should rephrase the whole question, really. It was badly formed. – Kay May 18 '17 at 05:13
  • "is what I'd like documentation for".I have a hard time imagining what alignment of types would otherwise mean. "extended alignment can change the size of a structure" Change in comparison to what? There is no same-but-unaligned type. – n. m. could be an AI May 18 '17 at 05:39
  • To your first question, I was looking for a source from the standard, ex (http://stackoverflow.com/a/4638295/7226419) – Kay May 18 '17 at 10:53
  • To your second question, your answer shows how a structure with the exact same members can different sizes based on the addition of `alignas(16)`. Again, badly worded, but the *change* is from `struct T {...}` to `struct alignas(x) T {...}` – Kay May 18 '17 at 10:55
  • "Exact same members" doesn't mean "same type" or "same size" or anything like that. There's not even a guarantee in the standard that given `struct A{}; struct B{};` sizes of A and B will be equal. – n. m. could be an AI May 18 '17 at 11:24