关于C ++数据成员对齐的示例

时间:2010-09-02 12:26:12

标签: c++

我试图实现Stack Overflow问题 C++ Data Member Alignment and Array Packing 。这是代码:

#include <stdio.h>
#include <stddef.h>
typedef struct {
    unsigned char a;
    unsigned char b;
    unsigned char c;
}foo;

typedef struct{
    unsigned short i;
    unsigned char a;
    unsigned char b;
    unsigned char c;
} Bar;

typedef struct {foo f[5];} f_b;
typedef struct {Bar[5];} b_f;
#define ALIGNMENT_OF(t) offsetof( struct { char x; t test; }, test )

int main(void){
    printf("Foo:: Size: %d; Alignment: %d\n", sizeof(foo), ALIGNMENT_OF(foo));
    printf("Bar:: Size: %d; Alignment: %d\n", sizeof(Bar), ALIGNMENT_OF(Bar));
    printf("F_B:: Size: %d; Alignment: %d\n", sizeof(f_b), ALIGNMENT_OF(f_B));
    printf("B_F:: Size: %d; Alignment: %d\n", sizeof(b_f), ALIGNMENT_OF(b_f));
    return 0;
}

但这是错误:

1>c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(16): error C2059: syntax error : '['
1>c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(16): error C2238: unexpected token(s) preceding ';'
1>c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(19): error C2332: 'struct' : missing tag name
1>c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(19): error C2143: syntax error : missing ')' before '{'
1>c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(19): error C2143: syntax error : missing ')' before '{'
1>c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(19): error C2059: syntax error : ')'
1>c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(19): error C2143: syntax error : missing '(' before ')'
1>c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(19): error C2143: syntax error : missing ')' before '{'
1>c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(19): error C2143: syntax error : missing ')' before '{'
1>c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(19): error C2143: syntax error : missing ')' before '{'
1>c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(19): error C2143: syntax error : missing ';' before '{'
1>c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(19): error C2059: syntax error : ')'
1>c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(19): error C2059: syntax error : ')'
1>c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(19): error C2227: left of '->test' must point to class/struct/union/generic type
1>          type is 'int'
1>c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(19): error C2059: syntax error : ')'
1>c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(19): error C2059: syntax error : ')'
1>c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(19): error C2059: syntax error : ')'
1>c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(20): error C2332: 'struct' : missing tag name
1>c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(20): error C2143: syntax error : missing ')' before '{'
1>c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(20): error C2143: syntax error : missing ')' before '{'
1>c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(20): error C2059: syntax error : ')'
1>c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(20): error C2143: syntax error : missing '(' before ')'
1>c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(20): error C2143: syntax error : missing ')' before '{'
1>c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(20): error C2143: syntax error : missing ')' before '{'
1>c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(20): error C2143: syntax error : missing ')' before '{'
1>c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(20): error C2143: syntax error : missing ';' before '{'
1>c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(20): error C2059: syntax error : ')'
1>c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(20): error C2059: syntax error : ')'
1>c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(20): error C2227: left of '->test' must point to class/struct/union/generic type
1>          type is 'int'
1>c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(20): error C2059: syntax error : ')'
1>c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(20): error C2059: syntax error : ')'
1>c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(20): error C2059: syntax error : ')'
1>c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(21): error C2332: 'struct' : missing tag name
1>c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(21): error C2143: syntax error : missing ')' before '{'
1>c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(21): error C2143: syntax error : missing ')' before '{'
1>c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(21): error C2059: syntax error : ')'
1>c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(21): error C2143: syntax error : missing '(' before ')'
1>c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(21): error C2143: syntax error : missing ')' before '{'
1>c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(21): error C2143: syntax error : missing ')' before '{'
1>c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(21): error C2143: syntax error : missing ')' before '{'
1>c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(21): error C2143: syntax error : missing ';' before '{'
1>c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(21): error C2065: 'f_B' : undeclared identifier
1>c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(21): error C2146: syntax error : missing ';' before identifier 'test'
1>c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(21): error C2059: syntax error : ')'
1>c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(21): error C2059: syntax error : ')'
1>c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(21): error C2227: left of '->test' must point to class/struct/union/generic type
1>          type is 'int'
1>c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(21): error C2059: syntax error : ')'
1>c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(21): error C2059: syntax error : ')'
1>c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(21): error C2059: syntax error : ')'
1>c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(22): error C2332: 'struct' : missing tag name
1>c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(22): error C2143: syntax error : missing ')' before '{'
1>c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(22): error C2143: syntax error : missing ')' before '{'
1>c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(22): error C2059: syntax error : ')'
1>c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(22): error C2143: syntax error : missing '(' before ')'
1>c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(22): error C2143: syntax error : missing ')' before '{'
1>c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(22): error C2143: syntax error : missing ')' before '{'
1>c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(22): error C2143: syntax error : missing ')' before '{'
1>c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(22): error C2143: syntax error : missing ';' before '{'
1>c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(22): error C2059: syntax error : ')'
1>c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(22): error C2059: syntax error : ')'
1>c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(22): error C2227: left of '->test' must point to class/struct/union/generic type
1>          type is 'int'
1>c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(22): error C2059: syntax error : ')'
1>c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(22): error C2059: syntax error : ')'
1>c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(22): error C2059: syntax error : ')'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

但在代码中它没有显示红线表示错误。我该如何解决这个问题?

4 个答案:

答案 0 :(得分:5)

查看\data_packing.cpp(16):的位置?括号中的16是一个行号。

答案 1 :(得分:3)

在struct b_f中,您创建了一个没有名称的Bar数组。另外,在C ++中,你不需要所有的typedef东西。

答案 2 :(得分:2)

首发

Bar[5]

不是有效代码。它应该像

Bar f[5]

此外,该宏没有意义。您可以尝试这样做:

template< class T >
struct testStruct{ char x; T test; };

#define ALIGNMENT_OF(t) offsetof( testStruct< t >, test )

最后还有一个错字:

ALIGNMENT_OF(f_B) //should be f_b

答案 3 :(得分:0)

pragma pack和pop可能会有所帮助。下面的示例将在一个字节边界上对齐每个成员。在VS中,也可以通过更改项目设置C ++ / CodeGeneration / StructMemberAlignment在项目级别设置。

#pragma pack(push, 1)
struct test {
    int a;
    char b[3];
    double c;
};
#pragma pack(pop)