返回信息流以前需要指定结构中成员的对齐方式的时候都是通过
#pragma pack
__attribute__((packed,aligned))
这些非标准的东西来指定,听说c++11中有了对齐的支持,今天试了一下,好像没什么用。。。
比如说我定义一个类
class alignas(1) Test
{
int a;
char b;
};
sizeof(Test)还是8 (gcc 4.9.2)
不知道是我用法有问题,还是理解的有问题?
要是我理解有问题的话,想问一下c++11中有没有标准的方法实现类似于#pargma pack(1)这种效果的东西?
这是一条镜像帖。来源:北邮人论坛 / cpp / #87827同步于 2015/7/8
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖
问一个c++中的对齐问题
vnebula
2015/7/8镜像同步3 回复
订阅后,新回复会通过你的通知中心匿名送达。
3 条回复
你的理解不对
http://en.cppreference.com/w/cpp/language/alignas
#include <iostream>
using namespace std;
struct alignas(32) Test
{
int a;
alignas (8) char b;
};
int main() {
Test t[2];
cout << "Address of t[1] is " << &t[1] << "\n"
<< "Address of t[1].b is " << (void*)&t[1].b << "\n"
<< "Address of t[1].a is " << &t[1].a << "\n"
<< "Size of t is " << sizeof(t) << "\n"
<< endl;
cout << "Address of t[0] is " << &t[0] << "\n"
<< "Address of t[0].b is " << (void*)&t[0].b << "\n"
<< "Address of t[0].a is " << &t[0].a << "\n"
<< "Size of t is " << sizeof(t) << "\n"
<< endl;
return 0;
}
3Q, 是我误解了它的用法
【 在 iFadeToBlack 的大作中提到: 】
: 你的理解不对
: http://en.cppreference.com/w/cpp/language/alignas
: [code=c]
: ...................