4

I've been trying to get my head around the TR1 addition known as aligned_storage. Whilst reading the following documents N2165, N3190 and N2140 I can't for the life of me see a statement where it clearly describes stack or heap nature of the memory being used.

I've had a look at the implementation provided by msvc2010, boost and gcc they all provide a stack based solution centered around the use of a union.

In short:

  • Is the memory type (stack or heap) used by aligned_storage implementation defined or is it always meant to be stack based?

  • and, What the is the specific document that defines/determines that?

Note: In MSVC10, the following is the definition of the type of aligned_storage, in this case if the aligned_storage is an auto variable the data(_Val,_Pad) is created on the stack:

template<class _Ty, size_t _Len> 
union _Align_type
{   
   // union with size _Len bytes and alignment of _Ty
   _Ty _Val;
   char _Pad[_Len];
};

Note: This is NOT a trivial question. Please try and understand the question before posting an answer.

2 Answers2

12

std::aligned_storage<Len, Align> just declares a member typedef (type).

The member typedef type shall be a POD type suitable for use as uninitialized storage for any object whose size is at most Len and whose alignment is a divisor of Align

(This is from the latest C++0x draft, N3225, 20.7.6.6 Table 53, but the language in the TR1 specification, N1836, is effectively the same except that in C++0x the Align template parameter has as its default argument the maximum alignment value.)

std::aligned_storage doesn't allocate any memory itself. You can create an object of type std::aligned_storage<Len, Align>::type and reinterpret that object as an object of any type that meets the requirements stated above.

James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • 2
    I'm not sure an answer can get better than this. What would be the point if data was dynamically allocated in `type` ? How could that possibly be of any help regarding alignment ? – icecrime Dec 29 '10 at 09:29
  • 2
    @Zenikoder - just read the quote from the Standard again. And again, if needed. :) `std::aligned_storage<...>::type` defines a POD type with requested alignment. You can allocate storage for this type on stack or in the heap, it's not relevant. – atzz Dec 29 '10 at 11:09
  • @Zenikoder - it's not implementation-defined. It's user-defined. The statement quoted by James explains everything clearly enough. – atzz Dec 29 '10 at 11:57
  • 2
    @Zenikoder: You're welcome. On a more serious note, I suppose don't understand the question. `aligned_storage` doesn't allocate any memory; it defines a type that **you** can use to allocate a block of memory of sufficient size and alignment to hold certain types of objects. Just like with any other type, you can use whatever form of allocation you'd like: if you declare a static `aligned_storage` it will have static storage duration, if you declare it locally it will have automatic storage duration, and if you dynamically allocate it then it will have dynamic storage duration. – James McNellis Dec 29 '10 at 14:53
0

You typically don't need to align stuff on the heap since any allocation (new/malloc) returns memory at an address which is aligned to any type.

Motti
  • 110,860
  • 49
  • 189
  • 262
  • 3
    This is not quite true. If you use SIMD instruction, you sometimes need to have data aligned 16 bytes boundaries, while new / malloc does not provide such warranty. For exemple, libc malloc return memory aligned on 8 bytes boundary. See `posix_memalign` functions on Linux or `_aligned_malloc` on Windows. – Sylvain Defresne Dec 29 '10 at 10:17
  • 1
    I was only able to allocate aligned memory on the heap using std::align, which is not yet in gcc4.8, so I use this implementation: http://code.google.com/p/c-plus/source/browse/src/util.h#57 – Michal Fapso Oct 10 '13 at 09:12