std::array alignment

五迷三道 提交于 2019-11-28 12:09:22

The standard mandates that the elements "are stored contiguously, meaning that if a is an array, then it obeys the identity &a[n] == &a[0] + n for all 0 <= n < N." (23.3.2.1 [array.overview] paragraph 1)

As far as I know, there is no guarantee that sizeof(std::array) == sizeof(T)*N, but the contiguity statement asserts that the values are stored just like in a regular C array. If you only have one array of values that need to be contiguous, you can use sizeof(T)*std::array::size() as the size and std::array::data() as the starting address of the array.

It looks from what little data you've given like it allocates memory to the nearest power of two. Knowing very little CPU architecture details, I might guess that allocating power-of-two sizes is faster than non padded, at least for small amounts. Perhaps you should see what happens when you try to allocate something a much larger?

Is there any reason you absolutely positively need to skim those extra bytes off the top?

centaurian_slug

Since posting, turns I get what I want swapping the IDE setting from the default to..

LLVM compiler 3.0 Language:
  LLVM C++ standard library:
    =libc++ (LLVM standard library with c++0x support.)

( CLANG_CXX_LIBRARY = libc++ )l

Previously the setting was "libstdc++ (gcc c++ standard library)" which appears to have the padding, and that allowed me to include <array> instead of <tr1/array>; and now

sizeof(array<T,N>)==sizeof(T)*N

this is all in Xcode 4.2 on mac osx lion. I'm hoping one is simply deprecated and that this behavior is what i'll get on other platforms?

std::array is actually not an array, but a struct that contains an array. It's the struct that is padded, not the array. Compilers are allowed to add padding to the end of a struct whenever they want.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!