关于在vc++中结构体所占内存大小

u010591305 2014-09-12 08:22:27
结构体1:
struct
{
int a;
char b;
double c;
} s1;
结构体2:
struct
{
int a;
double c;
char b;
} s2;
我用printf(“%d, %d”,sizeof(s1),sizeof(s2))输出,
为什么所分配的字节大小不一样;
可以说的详细点吗?
...全文
424 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
忘世麒麟 2014-09-18
  • 打赏
  • 举报
回复
搜索一下“字节对齐”!
卞爱华 2014-09-17
  • 打赏
  • 举报
回复
任何类型的起始地址要是类型长度的整数倍,这就是对齐问题
闲云阁 2014-09-15
  • 打赏
  • 举报
回复
结构体1: struct 最高字节double是8,8字节对齐 { int a; //4字节, 与下面的1 字节可共占8字节 char b; //1字节,和上面的int 共占5个字节,对齐需要补齐3个字节 double c; //8字节 } s1; 结构体2: struct 最高字节double是8,8字节对齐 { int a;//4字节, 下面是double 无法拼凑成小于8的空间,补齐4个字节 double c; //8字节,自身对齐 char b; //1字节 ,上面都已对齐,下面无数据,补齐7个字节 } s2;
边走边瞧 2014-09-15
  • 打赏
  • 举报
回复
对齐问题,老问题了。
赵4老师 2014-09-15
  • 打赏
  • 举报
回复
#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
//
//
  • 打赏
  • 举报
回复
原则,相邻层能合并补齐则合并(合并后不能超过最大类型字节数,若超过,则各层补)
  • 打赏
  • 举报
回复
结构体内分层,以最大字节为标准补齐.
ysnis 2014-09-13
  • 打赏
  • 举报
回复
为什么char b有时按4字节对齐有时8字节对齐 ---------------- 和前面一个类型有关,按上一个大小对齐
u010591305 2014-09-13
  • 打赏
  • 举报
回复
为什么char b有时按4字节对齐有时8字节对齐
707wk 2014-09-12
  • 打赏
  • 举报
回复
字节对齐。。。
sunShinezhuyg 2014-09-12
  • 打赏
  • 举报
回复
结构体1: struct { int a; char b; double c; } s1; 数据对齐如下图 |----------------int----------|char---char---char---char| 4+1+3(后三个char为填充) |---------------------------double---------------------------| 8 所以16B 结构体2: struct { int a; double c; char b; } s2; 数据对齐如下图 |----------------int-----------------|--------------int---------------| 4+4(后一个int为填充) |-------------------------------double-----------------------------| 8 |char---char---char---char---char---char---char--char--| 1+7(后7个char为填充) 所以占24B 可参看:http://pppboy.blog.163.com/blog/static/30203796201082494026399/
brookmill 2014-09-12
  • 打赏
  • 举报
回复
struct { int a; // 4字节 char b; // 1字节 // 3字节填充,按8对齐 double c; // 8字节 } s1; // 16字节 结构体2: struct { int a; // 4字节 // 4字节填充,按8对齐 double c; // 8字节 char b; // 1字节 // 7字节填充,按8对齐 } s2; // 24字节
mujiok2003 2014-09-12
  • 打赏
  • 举报
回复
搜索一下 alignment and padding
mujiok2003 2014-09-12
  • 打赏
  • 举报
回复

struct Foo
{
  int a;
  char b;
  double c;
} s1;

struct Bar
{
  int a;
  double c;
  char b;
} s2;

int main()
{
  return 0;
}
cl.exe /nologo /EHsc /d1reportAllClassLayout demo.cpp 

class Foo       size(16):
        +---
 0      | a
 4      | b
        | <alignment member> (size=3)
 8      | c
        +---



class Bar       size(24):
        +---
 0      | a
        | <alignment member> (size=4)
 8      | c
16      | b
        | <alignment member> (size=7)
        +---

70,037

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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