-
#include <asm/types.h>
-
#include <stdio.h>
-
#include <sstream>
-
#include <string>
-
using namespace std;
-
-
typedef __s8 Int8;
-
typedef __u8 Uint8;
-
typedef __s16 Int16;
-
typedef __u16 Uint16;
-
typedef __s32 Int32;
-
typedef __u32 Uint32;
-
typedef __s64 Int64;
-
typedef __u64 Uint64;
-
-
typedef struct S1
-
{
-
Uint8 a;
-
Int8 a2;
-
Int32 b;
-
Uint16 c;
-
}S1;
-
-
#pragma pack(1)
-
typedef struct S2
-
{
-
Uint8 a;
-
Int8 a2;
-
Int32 b;
-
Uint16 c;
-
}S2;
-
#pragma pack()
-
-
int main(int argc, char** argv)
-
{
-
S1 data1;
-
memset(&data1,0,sizeof(data1));
-
S2 data2;
-
memset(&data2,0,sizeof(data2));
-
printf("sizeof(S1):%d\n",sizeof(S1));
-
printf("sizeof(S2):%d\n",sizeof(S2));
-
-
ostringstream oss1,oss2,oss3,oss4;
-
oss1 << (int)data1.a << data1.a2 << data1.b << data1.c;
-
oss2 << data2.a << data2.a2 << data2.b << data2.c;
-
oss3 << (int)data1.a << (int)data1.a2 << data1.b << data1.c;
-
oss4 << data2.a << (int)data2.a2 << data2.b << data2.c;
-
printf("%s\n",oss1.str().c_str());
-
printf("%s\n",oss2.str().c_str());
-
printf("%s\n",oss3.str().c_str());
-
printf("%s\n",oss4.str().c_str());
-
-
return 0;
-
}
输出结果:
-
sizeof(S1):12
-
sizeof(S2):8
-
0
-
-
0000
-
gdb调试:
-
(gdb) p data1
-
$1 = {a = 0 '\000', a2 = 0 '\000', b = 0, c = 0}
-
(gdb) p data2
-
$2 = {a = 0 '\000', a2 = 0 '\000', b = 0, c = 0}
结构体的成员变量a,虽然我们可以把它当作整型使用,但是ostringstream应该是当作字符处理了,ascii码表的值为0的元素实际上就是空。
gdb调试里,显示为
a = 0 '\000',可能是系统对于a做了两种显示而已。
阅读(872) | 评论(0) | 转发(0) |