怪异的一个问题,完全不在知识范围内。今天弄DLL的,求解。

zdrone 2015-11-04 04:43:50
代码:
#include "stdafx.h"

typedef struct tag_tg1
{
//unsigned char f1;
//unsigned char f2;
//unsigned char f3;
//unsigned char f4;
//unsigned char f5;
//int f6;
double f7;
}tg1;

typedef struct tag_tg2
{
//unsigned char f1;
//unsigned char f2;
//unsigned char f3;
//unsigned char f4;
//unsigned char f5;
int f6;
double f7;
}tg2;

typedef struct tag_tg3
{
unsigned char f1;
//unsigned char f2;
//unsigned char f3;
//unsigned char f4;
//unsigned char f5;
int f6;
double f7;
}tg3;

typedef struct tag_tg4
{
unsigned char f1;
unsigned char f2;
//unsigned char f3;
//unsigned char f4;
unsigned char f5;
int f6;
double f7;
}tg4;

typedef struct tag_tg5
{
unsigned char f1;
unsigned char f2;
unsigned char f3;
//unsigned char f4;
unsigned char f5;
int f6;
double f7;
}tg5;

typedef struct tag_tg6
{
unsigned char f1;
unsigned char f2;
unsigned char f3;
unsigned char f4;
unsigned char f5;
int f6;
double f7;
}tg6;

typedef struct tag_tg7
{
unsigned char f1;
unsigned char f2;
unsigned char f3;
unsigned char f4;
unsigned char f5;
int f6;
int f7;
double f8;
}tg7;

typedef struct tag_tg8
{
unsigned char f1;
unsigned char f2;
unsigned char f3;
unsigned char f4;
unsigned char f5;
int f6;
int f7;
double f8;
double f9;
}tg8;

typedef struct tag_tg9
{
unsigned char f1;
unsigned char f2;
unsigned char f3;
unsigned char f4;
unsigned char f5;
int f6;
int f7;
double f8;
double f9;
char f10[20];
}tg9;


int main(int argc, char* argv[])
{
printf("sizeof(unsigned char)=%d,sizeof(int)=%d,sizeof(double)=%d\n",sizeof(unsigned char),sizeof(int),sizeof(double));
printf("sizeof(tg1)=%d,sizeof(tg2)=%d,sizeof(tg3)=%d,sizeof(tg4)=%d,sizeof(tg5)=%d,sizeof(tg6)=%d,sizeof(tg7)=%d,sizeof(tg8)=%d,sizeof(tg9)=%d\n",sizeof(tg1),sizeof(tg2),sizeof(tg3),sizeof(tg4),sizeof(tg5),sizeof(tg6),sizeof(tg7),sizeof(tg8),sizeof(tg9));

return 0;
}



输出居然是:
sizeof(unsigned char)=1,sizeof(int)=4,sizeof(double)=8
sizeof(tg1)=8,sizeof(tg2)=16,sizeof(tg3)=16,sizeof(tg4)=16,sizeof(tg5)=16,sizeof(tg6)=24,sizeof(tg7)=24,sizeof(tg8)=32,sizeof(tg9)=56

完全无法理解,不在只是范围内,能不能有哪位大哥帮忙解释一下。
...全文
133 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2015-11-05
  • 打赏
  • 举报
回复
仅供参考:
#include <stdio.h>
#define field_offset(s,f) (int)(&(((struct s *)(0))->f))
struct AD  { int a; char b[13]; double c;};
#pragma pack(push)
#pragma pack(1)
struct A1  { int a; char b[13]; double c;};
#pragma pack(2)
struct A2  { int a; char b[13]; double c;};
#pragma pack(4)
struct A4  { int a; char b[13]; double c;};
#pragma pack(8)
struct A8  { int a; char b[13]; double c;};
#pragma pack(16)
struct A16 { int a; char b[13]; double c;};
#pragma pack(pop)
int main() {
    printf("AD.a %d\n",field_offset(AD,a));
    printf("AD.b %d\n",field_offset(AD,b));
    printf("AD.c %d\n",field_offset(AD,c));
    printf("\n");
    printf("A1.a %d\n",field_offset(A1,a));
    printf("A1.b %d\n",field_offset(A1,b));
    printf("A1.c %d\n",field_offset(A1,c));
    printf("\n");
    printf("A2.a %d\n",field_offset(A2,a));
    printf("A2.b %d\n",field_offset(A2,b));
    printf("A2.c %d\n",field_offset(A2,c));
    printf("\n");
    printf("A4.a %d\n",field_offset(A4,a));
    printf("A4.b %d\n",field_offset(A4,b));
    printf("A4.c %d\n",field_offset(A4,c));
    printf("\n");
    printf("A8.a %d\n",field_offset(A8,a));
    printf("A8.b %d\n",field_offset(A8,b));
    printf("A8.c %d\n",field_offset(A8,c));
    printf("\n");
    printf("A16.a %d\n",field_offset(A16,a));
    printf("A16.b %d\n",field_offset(A16,b));
    printf("A16.c %d\n",field_offset(A16,c));
    printf("\n");
    return 0;
}
//AD.a 0
//AD.b 4
//AD.c 24
//
//A1.a 0
//A1.b 4
//A1.c 17
//
//A2.a 0
//A2.b 4
//A2.c 18
//
//A4.a 0
//A4.b 4
//A4.c 20
//
//A8.a 0
//A8.b 4
//A8.c 24
//
//A16.a 0
//A16.b 4
//A16.c 24
//
//
cs_qd 2015-11-04
  • 打赏
  • 举报
回复
结构体字节对其嘛,网上搜一大片,都讲解得很清楚
大大大白 2015-11-04
  • 打赏
  • 举报
回复
这个涉及到内存的对齐与补齐的知识,你可以查一下
paschen 版主 2015-11-04
  • 打赏
  • 举报
回复
fefe82 2015-11-04
  • 打赏
  • 举报
回复
https://en.wikipedia.org/wiki/Data_structure_alignment
fly_dragon_fly 2015-11-04
  • 打赏
  • 举报
回复
是8字节对齐不明白吗

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧