0

I'm working with a library which has aligned types, and I have a structure that contains an array of one of these types. My compiler (MSVC) is complaining with the error message in the title (citing my structure when I try to dynamically allocate it).

Here's the class in question, from Embree (Raytracing software):

#  define RTCORE_ALIGN(...) __declspec(align(__VA_ARGS__))

#ifndef __RTCRay8__
#define __RTCRay8__
struct RTCORE_ALIGN(32) RTCRay8
{
  //data
}

My struct looks like this:

struct ContainerThing
{
  RTCRay8 ray_packets[SIZE_CONSTANT];
}

Would I be correct in saying that because the RTCRay8 array is inside of a dynamically allocated class, the alignment guarantees are lost? The array will therefore be potentially misaligned?

If that's what's happening, my question is: What is the solution? It seems like I will need to derive from RTCRay8 to fix this, but will overriding new and delete for that derived type work, or will I simply need to manually align the class (add padding to the end)?

user2345397
  • 301
  • 1
  • 11
  • 1
    Dynamic allocation can only guarantee alignment for fundamental types - but not custom/over-alignments - until C++17: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3396.htm Perhaps your compiler/library of choice (which is?) has a non-portable option to enforce this, though. – underscore_d Aug 17 '16 at 19:08
  • "Would I be correct in saying... the alignment guarantees are lost?" No. The alignment guarantees are not lost. That's why you get the error message. Deriving from RTCRay8 won't help, and neither will manually adding padding. – Mooing Duck Aug 17 '16 at 19:08
  • @MooingDuck I forgot to say that this was a warning, not an error, I just added it to the title. – user2345397 Aug 17 '16 at 19:20
  • 1
    @underscore_d My question is specifically about what to do if you dynamically allocate a class which contains an aligned type and you get this particular warning; the proposed duplicate does not answer this. Also, my question puts the problem into context and if answered provides a solution. – user2345397 Aug 17 '16 at 19:28
  • 1
    @user2345397: Hint: `RTCRay8` is "overaligned" because it has an alignment bigger than `std::max_align_t`, which isn't supported by your compiler. A class that contains this aligned type is itself _also_ aligned to the same number, and is thus _also_ overaligned. And the other question is about alignment in dynamic memory, which is what you're asking. And the top answer there says "As for allocating memory with an alignment greater than `alignof(std::max_align_t)`"... and proceeds to answer your question. This is indeed a duplicate. – Mooing Duck Aug 17 '16 at 19:57
  • That makes sense, I didn't consider that it might be an incompatibility between Embree and my compiler. Thanks for the explanation. I still personally think there is value in questions that put the problem into context, but if SO policy disagrees, I can respect that. – user2345397 Aug 17 '16 at 21:05
  • I think this question is not a duplicate because it is MSVC++ specific, while https://stackoverflow.com/questions/6973995/dynamic-aligned-memory-allocation-in-c11 is generic. It would be nice if the current question is once answered with "supported in MSVC++20XX", while such an answer seems less appropriate for that other question. – Serge Rogatch Jul 02 '17 at 06:35

0 Answers0