Reputation: 56567
While learning about alignment issues etc, I realized that my implementation of g++4.9 (macports OS X) does not have support for std::align
. If I try to compile (with -std=c++11
) this example code from http://www.cplusplus.com/reference/memory/align/
// align example
#include <iostream>
#include <memory>
int main() {
char buffer[] = "------------------------";
void * pt = buffer;
std::size_t space = sizeof(buffer) - 1;
while ( std::align(alignof(int), sizeof(char), pt, space) ) {
char* temp = static_cast<char*>(pt);
*temp = '*'; ++temp; space -= sizeof(char);
pt = temp;
}
std::cout << buffer << '\n';
return 0;
}
the compiler spits out the error
error: 'align' is not a member of 'std'
This seems strange as g++ seems to have implemented alignment support since g++4.8, https://gcc.gnu.org/projects/cxx0x.html (N2341)
The code compiles under clang++ without any problems.
Is this a well known issue of g++ that I am not aware of? The online compilers that I tested (ideone and coliru) reject the code also.
Upvotes: 9
Views: 3006
Reputation: 323
As an alternative you could write your own alignment code that matches the behavior of std::align
. The next piece of code is written by David Krauss in a post found here: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=57350
inline void *align( std::size_t alignment, std::size_t size,
void *&ptr, std::size_t &space ) {
std::uintptr_t pn = reinterpret_cast< std::uintptr_t >( ptr );
std::uintptr_t aligned = ( pn + alignment - 1 ) & - alignment;
std::size_t padding = aligned - pn;
if ( space < size + padding ) return nullptr;
space -= padding;
return ptr = reinterpret_cast< void * >( aligned );
}
Upvotes: 3