栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C语言结构体,共用体所占字节数计算

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C语言结构体,共用体所占字节数计算

字节数:

环境charshortintlonglong longfloatdouble指针
windows(64)12444884

(无符号型在原来的基础上不变,例如 undigned int依旧为4个字节)

计算字节数规律:
1、结构体所占总字节数是其最大成员类型的整数倍,不足的后面补齐;
2、short类型数据从偶数地址开始存储;
3、变量存储起始位置是该变量大小的整数倍;
4、int数据对齐在4字节地址边界;


结构体

结构体所占内存的长度是各成员占得内存长度之和。

例1:

#include
int main(){
  typedef struct c1
  {
    char a[10];
  } S2;
  printf("%d", sizeof(S2));
  return 0;
}

输出结果:10

例2:

typedef struct node2
  {
    char a;
    int b;
    short c;
    double r;
  } S2;
  printf("%d", sizeof(S2));

输出结果:24
分析:
如果按如下方法计算不仅浪费空间,和运行结果也存在不一致的情况。

|char|----|----|----|----|----|----|----| 8字节
|--------int--------|-------------------| 8字节
|--short--|---------|---------|---------| 8字节
|-----------------double----------------| 8字节

所以有了如下结构:

|char|----|----|----|--------int--------| 8字节
|--short--|---------|---------|---------| 8字节
|-----------------double----------------| 8字节

例3:对比

typedef struct node
  {
    char a;
    int b;
    short c;
  } S2;
  printf("%d", sizeof(S2));
//输出结果:12

typedef struct node
  {
    char a;
    short c;
    int b;
  } S2;
  printf("%d", sizeof(S2));
 //输出结果:8

typedef struct node2
  {
    char a[3];
    int b;
    short c[5];
    double e;
  } S2;
  printf("%d", sizeof(S2));
  //输出结果:32

typedef struct node
  {
    char a[4];
    int b;
    short c[2];
    double e;
  } S2;
  printf("%d", sizeof(S2));
  //输出结果:24

typedef struct node
  {
    int b;
    short c[2];
    double e;
  } S2;
  printf("%d", sizeof(S2));
  //输出结果:16

根据以上规律,结构体所占总字节数是其最大成员类型的整数倍,不足的后面补齐;注意 变量存储起始位置是该变量大小的整数倍;即short从偶数位开始,int对齐四字节。(参见分析部分)

根据以上规律来试一试
例4:

typedef struct c1
  {
    char a[13];
    int b;	
    double c[1];
    char d;
    int e[9];
  }T1;
  printf("%d",sizeof(T1));

输出结果:72

分析:

|char|char|char|char|char|char|char|char| 8字节
|char|char|char|char|char|----|----|----| 8字节
|--------int--------|-------------------| 8字节
|-----------------double----------------| 8字节
|char|----|----|----|--------int--------| 8字节    
|--------int--------|--------int--------| 8字节
|--------int--------|--------int--------| 8字节
|--------int--------|--------int--------| 8字节
|--------int--------|--------int--------| 8字节

计算:8*9=72

例5:

typedef struct c1
  {
    char a[5];
    int b[3];
    double c[1];
    char d;
    int e[9];
  }T1;
  printf("%d",sizeof(T1));

输出结果:72

共用体

共用体所占的内存长度等于最长的内存长度。

例1:

typedef union c1
  {
    char a[5];
    int b[3];
    double c[1];
    char d;
    int e[9];
  }T1;
  printf("%d",sizeof(T1));

输出结果:40
分析:

|--------int--------|--------int--------| 8字节
|--------int--------|--------int--------| 8字节
|--------int--------|--------int--------| 8字节
|--------int--------|--------int--------| 8字节
|--------int--------|-------------------| 8字节

共用体所占的内存长度等于最长的内存长度。最大长度应该为5*8=40。

补充类型之间隐式转化规则:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/783943.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号