[Boost.Array] gcc 4.1.2 vs. boost alignment

I've just upgraded my compiler to gcc 4.1.2 and tried the latest implementation of tr1 included with gcc. I've discovered the gcc array implementation is not what I expect: #include <boost/array.hpp> #include <tr1/array> #include <iostream> int main(int , char **) { boost::array<char, 6> barray; std::cout << sizeof(barray) << std::endl; std::tr1::array<char, 6> tr1array; std::cout << sizeof(tr1array) << std::endl; } When run this code produces: g++ foo.cpp && ./a.out 6 16 That is, the boost array is exactly the size I require 6 bytes, but the tr1 array is larger, 16 bytes. I believe this is due to the following declaration in 4.1.2/tr1/array: // Support for zero-sized arrays mandatory. value_type _M_instance[_Nm ? _Nm : 1] __attribute__((__aligned__)); The boost version is not aligned, but the gcc version is. Does the tr1 implementation require the alignment? If so, it appears that tr1::array is definitely not a suitable wrapper for C-style arrays since they aren't the same size! What's the scoop? Thanks, -- Robert

On Dec 18, 2006, at 5:03 PM, Robert Mecklenburg wrote:
I've just upgraded my compiler to gcc 4.1.2 and tried the latest implementation of tr1 included with gcc. I've discovered the gcc array implementation is not what I expect: [snip code] That is, the boost array is exactly the size I require 6 bytes, but the tr1 array is larger, 16 bytes. I believe this is due to the following declaration in 4.1.2/tr1/array:
// Support for zero-sized arrays mandatory. value_type _M_instance[_Nm ? _Nm : 1] __attribute__ ((__aligned__));
The boost version is not aligned, but the gcc version is. Does the tr1 implementation require the alignment? If so, it appears that tr1::array is definitely not a suitable wrapper for C-style arrays since they aren't the same size!
Ick. I would report this to GCC as a bug. Cheers, Doug

Robert Mecklenburg napisał(a):
int main(int , char **) { boost::array<char, 6> barray; std::cout << sizeof(barray) << std::endl; std::tr1::array<char, 6> tr1array; std::cout << sizeof(tr1array) << std::endl; }
When run this code produces:
g++ foo.cpp && ./a.out 6 16
That is, the boost array is exactly the size I require 6 bytes, but the tr1 array is larger, 16 bytes.
the array is fine, just check the size() and use data(). implementation details are not our business. $ cat test.cpp #include <boost/array.hpp> #include <tr1/array> #include <cstdio> int main() { boost::array< char, 6 > barray; std::printf( "barray size = %Zd, raw ptr = %p\n", barray.size(), barray.data() ); std::tr1::array< char, 6 > tr1array; std::printf( "tr1array size = %Zd, raw ptr = %p\n", tr1array.size(), tr1array.data() ); return 0; } $ ./test barray size = 6, raw ptr = 0x7fffffd038c0 tr1array size = 6, raw ptr = 0x7fffffd038b0 -- to_be || !to_be == 1, to_be | ~to_be == -1
participants (3)
-
Douglas Gregor
-
Paweł Sikora
-
Robert Mecklenburg