CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  VC/MFC >  基础类

请问这个结构体在32位系统中占几个字节?

楼主igogo(gogo)2002-10-12 10:54:47 在 VC/MFC / 基础类 提问

typedef   struct   _A  
  {  
  int   a;  
  char   b;  
  DWORD   c;  
  _A   *   next;  
  }abc;  
   
  看它占几个字节怎么看?  
  问题点数:50、回复次数:8Top

1 楼99263()回复于 2002-10-12 10:59:09 得分 10

int   len=0;  
  len   =   sizeof(abc);  
   
  另外,你在编译时字节对齐方式不同,这个len的结果也会不同,有1字节对齐的,也有2、4、8字节对齐的。Top

2 楼igogo(gogo)回复于 2002-10-12 11:10:55 得分 0

谢谢99263,1、2、4、8字节对齐是什么意思?Top

3 楼sjsj(虚怀若谷)回复于 2002-10-12 11:45:43 得分 10

使用sizeof可以得到单个变量的存储大小  
   
  字节对齐简单的讲:  
  在内存中有四个字节,为[0],[1],[2],[3]  
  你定义了两个char类型的变量,c1,c2  
  常规下是c1,c2分别对应[0],[1]  
  如果是2字节对齐,那么c1,c2分别对应[0],[2]Top

4 楼everandforever(Forever)回复于 2002-10-12 13:12:16 得分 10

默认的情况:  
   
  typedef   struct   _A  
  {  
  int   a;           //4字节  
  char   b;         //4字节  
  DWORD   c;       //4字节  
  _A   *   next;   //4字节  
  }abc;Top

5 楼qing_li73(Vincent Lee)回复于 2002-10-12 13:18:26 得分 10

Just   test   via   ur   computer.....:)  
   
  Storage   and   Alignment   of   Structures  
  Microsoft   Specific   —>  
   
  Structure   members   are   stored   sequentially   in   the   order   in   which   they   are   declared:   the   first   member   has   the   lowest   memory   address   and   the   last   member   the   highest.    
   
  Every   data   object   has   an   alignment-requirement.   For   structures,   the   requirement   is   the   largest   of   its   members.   Every   object   is   allocated   an   offset   so   that  
   
  offset   %   alignment-requirement   ==   0  
   
  Adjacent   bit   fields   are   packed   into   the   same   1-,   2-,   or   4-byte   allocation   unit   if   the   integral   types   are   the   same   size   and   if   the   next   bit   field   fits   into   the   current   allocation   unit   without   crossing   the   boundary   imposed   by   the   common   alignment   requirements   of   the   bit   fields.    
   
  To   conserve   space   or   to   conform   to   existing   data   structures,   you   may   want   to   store   structures   more   or   less   compactly.   The/Zp[n]   compiler   option   and   the#pragma   pack   control   how   structure   data   is   “packed”   into   memory.   When   you   use   the   /Zp[n]   option,   where   n   is   1,   2,   4,   8,   or   16,   each   structure   member   after   the   first   is   stored   on   byte   boundaries   that   are   either   the   alignment   requirement   of   the   field   or   the   packing   size   (n),   whichever   is   smaller.   Expressed   as   a   formula,   the   byte   boundaries   are   the    
   
  min(   n,   sizeof(   item   )   )  
   
  where   n   is   the   packing   size   expressed   with   the   /Zp[n]   option   and   item   is   the   structure   member.   The   default   packing   size   is   /Zp8.  
   
  To   use   the   pack   pragma   to   specify   packing   other   than   the   packing   specified   on   the   command   line   for   a   particular   structure,   give   the   pack   pragma,   where   the   packing   size   is   1,   2,   4,   8,   or   16,   before   the   structure.   To   reinstate   the   packing   given   on   the   command   line,   specify   the   pack   pragma   with   no   arguments.    
   
  Bit   fields   default   to   size   long   for   the   Microsoft   C   compiler.   Structure   members   are   aligned   on   the   size   of   the   type   or   the   /Zp[n]   size,   whichever   is   smaller.   The   default   size   is   4.  
   
  END   Microsoft   Specific  
   
  Top

6 楼igogo(gogo)回复于 2002-10-12 21:21:47 得分 0

everandforever(Forever)   为什么char型和next指针也是4个字节?Top

7 楼banalman(IT解放者)回复于 2002-10-12 21:59:54 得分 0

gzTop

8 楼everandforever(Forever)回复于 2002-10-12 22:12:52 得分 10

WIN32下指针都是4字节啊?   有问题么?  
  char是4字节是因为数据对齐。Top

相关问题

  • 这个结构体在VC6下为什么是16个字节???????????????
  • 请问,c#里面的结构体,可以像C++的结构体一样做字节对齐吗
  • 按字节读取一个结构
  • 结构的字节大小问题
  • 结构变量的字节数问题
  • 奇怪!真是奇怪!明明这个结构体只有13个字节,怎么用Sizeof计算却有16个字节呢?
  • oa系统的体系结构问题??????
  • 两个求结构体和共用体所占字节数的问题!还有一个另外的
  • 高手来一下!vc6中定义结构体时有偏移吗?一般是多少个字节?
  • 关于struct结构中字节对齐的问题

关键词

  • members
  • abc
  • alignment
  • requirement
  • len
  • next
  • are
  • char

得分解答快速导航

  • 帖主:igogo
  • 99263
  • sjsj
  • everandforever
  • qing_li73
  • everandforever

相关链接

  • Visual C++类图书
  • Visual C++类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo