3

What is the difference between struct __declspec(align(16)) sse_t{}; and struct alignas(16) sse_t{};?

Link to an example of the alignment of structures in C++11: http://en.cppreference.com/w/cpp/language/alignas

// every object of type sse_t will be aligned to 16-byte boundary
struct alignas(16) sse_t
{
  float sse_data[4];
};

// the array "cacheline" will be aligned to 128-byte boundary
alignas(128) char cacheline[128];

However, there is not in MSVS2012 keyword alignas(16), but you can use __declspec(align(16)): Where can I use alignas() in C++11? What we can see in example:

#include <iostream>

#ifdef __GNUC__
struct __attribute__(align(16)) sse_t
#else
struct __declspec(align(16)) sse_t
#endif
{
  float sse_data[4];
};

/*
// Why doesn't it support in MSVS 2012 if it is the same as __declspec(align(16)) ?
struct alignas(16) sse_t
{
  float sse_data[4];
};
*/    

int main() {
    // aligned data
    sse_t ab;
    ab.sse_data[1] = 10;

    // what will happen?
    unsigned char *buff_ptr = new unsigned char [1000]; // allocate buffer
    sse_t * unaligned_ptr = new(buff_ptr + 3) sse_t;    // placement new
    unaligned_ptr->sse_data[1] = 20;    

    int b; std::cin >> b;
    return 0;
}
  1. These functionalities of alignment are equivalent?
  2. And if so - equivalent, why did not entered keyword alignas() in MSVS2012, because this functionality is already there __declspec(align(16))?
  3. And what will happen if such a structure to place on a misaligned address through "placement new": new(buff_ptr + 3)?
Matthias
  • 4,481
  • 12
  • 45
  • 84
Alex
  • 12,578
  • 15
  • 99
  • 195
  • `alignas` is the standard-conforming version of either `__attribute__(align(..))` or `__declspec(align(..))`. – Xeo Nov 13 '13 at 16:21

1 Answers1

4
  1. To my knowledge they are equivalent, the difference being that struct alignas(16) sse_t{}; is standard C++ and struct __declspec(align(16)) sse_t{}; is a pre-C++11 Microsoft extension.
  2. Microsoft hadn't gotten around to implementing alignas() in their compiler in MSVS2012, you'd have to ask them why. IIRC it is supported in MSVS2013. EDIT: I'm a liar, MSVS2013 still does not support alignas / alignof.
  3. Horrible things will likely happen if such a structure is misaligned. You don't even need placement new to do so, plain old new is oblivious to alignment requirements beyond those needed for primitive types as of C++11.
Casey
  • 41,449
  • 7
  • 95
  • 125
  • 1
    Point (3) there's a question exactly about that - http://stackoverflow.com/questions/11781724/do-i-really-have-to-worry-about-alignment-when-using-placement-new-operator – quetzalcoatl Nov 13 '13 at 16:27
  • Thanks! (2), but may be some difference which motivate to doesn't implement alignas() in MSVS2013? And (3), have said here about **Plain old new** http://stackoverflow.com/a/11782277/1558037 and in C++03-Standard "5.3.4 New 14 [Note: when the allocation function returns a value other than null, it must be a pointer to a block of storage in which space for the object has been reserved. The **block of storage is assumed to be appropriately aligned** and of the requested size. The address of the created object will not necessarily be the same as that of the block if the object is an array. ]". – Alex Nov 13 '13 at 19:00
  • @Casey Different between `__declspec(align())` and `alignas()` in that - `alignas()` in C++11-standard requires to throw an exception `std::bad_alloc()` when operator `new`/`new[]` can't place objects with this alignment, isn't it? http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2341.pdf – Alex Nov 16 '13 at 16:00