C语言关键字 sizeof 是一个操作符,返回对象或类型所占内存字节数,类型为size_t(定义在<stddef.h>),有2种用法:

  • sizeof unary-expression
  • sizeof (type-name)

sizeof不能应用的场合:

  • an expression that has function type or an incomplete type
  • the parenthesized name of such a type
  • an expression that designates a bit-field member

如果操作数的类型是VLA (variable length array),要进行evaluate;否则不需要evaluate,结果是一个整形常量。

示例1 进程间通信 比如storage allocator 和 I/O system

extern void *alloc(size_t);
double *dp = alloc(sizeof *dp);

示例2 计算数组中元素个数

sizeof array / sizeof array[]

示例3 VLA大小计算

#include <stddef.h>
size_t fsize3(int n)
{
  char b[n+]; // variable length array
  return sizeof b; // execution time sizeof
}
int main()
{
  size_t size;
  size = fsize3(); // fsize3 returns 13
  return ;
}

ANSI C规定字符型1字节,其余sizeof的返回结果与编译器实现相关。在32位系统中:

1. 基本类型

_Bool 1

char 1

short 2

int 4

long 4

long long 8

float 4

double 8

long double 12

_Complex 16

void 1

2. 指针

4

3. 枚举

4

4. 数组

数组长度 * 数组成员size

5. 结构体

字节对齐值:ALIGN
基本类型成员:SIZE

每个成员相对于结构体首地址的偏移都是min(ALIGN, SIZE)的整数倍,可能在成员间加填充字节
结构体总大小为min(ALIGN, SIZEmax)的整数倍,可能在最后一个成员后加填充字节

6. 联合

规则类似struct

测试程序:

#include <stdio.h>
#include <complex.h> #define SIZEOF(x) printf("size of '%s' is %zd\n", #x, sizeof(x)); int main() {
SIZEOF(_Bool);
SIZEOF(char);
SIZEOF(short);
SIZEOF(int);
SIZEOF(long);
SIZEOF(long long);
SIZEOF(float);
SIZEOF(double);
SIZEOF(long double); // depends on -m128bit-long-double
SIZEOF(_Complex);
SIZEOF(void); SIZEOF(void*);
SIZEOF(void (*)(void)); // function pointer SIZEOF(enum {E}); SIZEOF(char []); // zero-size array
SIZEOF(int []); SIZEOF(struct {}); // empty struct
SIZEOF(struct {char c;});
SIZEOF(struct {char c; short s;}); // alignment
SIZEOF(struct {int i; char c;});
SIZEOF(struct {int i; char c;} __attribute__((packed)));
#pragma pack(push)
#pragma pack(1)
printf("With #pragma pack(1): ");
SIZEOF(struct {int i; char c;});
#pragma pack(pop)
SIZEOF(struct {double d; char c;}); // depends on -malign-double SIZEOF(struct {char c1:; char :; char c2:;}); // bit field
SIZEOF(struct {int i:; char c:;}); SIZEOF(struct {struct {int i; char c;}; short s;});
SIZEOF(struct {struct {short s1; char c;}; short s2;});
SIZEOF(struct {char c2; struct {short s1; char c1;}; short s2;}); SIZEOF(union {char c; int i;}); // union
return ;
}

C语言关键字之sizeof的更多相关文章

  1. C语言关键字—-sizeof 、typedef、const、static、register、extern、#define

    关键字:sizeof .#define.typedef.const.static.register.extern sizeof 1. 作用:求数据所占得内存空间大小 2. 本质:求数据得类型所占的内存 ...

  2. FILE不是C语言关键字

    FILE不是C语言关键字,只是标准C中的标准输入输出中定义的一个新的数据类型 stdio.htypedef struct _iobuf{ char* _ptr; int _cnt; char* _ba ...

  3. 李洪强漫谈iOS开发[C语言-009] - C语言关键字

    // //  main.m //  04 - C语言关键字 // //  Created by vic fan on 16/7/12. //  Copyright © 2016年 李洪强. All r ...

  4. c语言关键字总结

    1.关键字变更历史 1999年12月16日,ISO推出了C99标准,该标准新增了5个C语言关键字: inline restrict _Bool _Complex _Imaginary(注意bool 从 ...

  5. 1.C语言关键字(auto break case char const swtich)

    ANSI C标准C语言共有32个关键字,分别为: auto break case char const continue default do double else enum extern floa ...

  6. C/C++ 知识点---C语言关键字(32个)

    C/C++ 知识点 1.C语言关键字(32个) <1>.基本数据类型 [5] void :声明函数无返回值或无参数,声明空类型指针 char :声明字符型变量 int :声明整形变量 fl ...

  7. 继续学习:C语言关键字

    auto :声明自动变量 break:跳出当前循环 case:开关语句分支 char :声明字符型变量或函数 const :声明只读变量 continue:结束当前循环,开始下一轮循环 default ...

  8. 统计C语言关键字出现次数

    统计C语言关键字出现次数 <C程序设计语言>K&R版本第6章结构6.3结构数组内容 /* Name: 统计c语言关键字出现次数 Copyright: Author: lingr7 ...

  9. C语言关键字-volatile

    1.C语言关键字volatile     C 语言关键字volatile(注意它是用来修饰变量而不是上面介绍的__volatile__)表明某个变量的值可能在外部被改变,因此对这些变量的存取 不能缓存 ...

随机推荐

  1. POJ 2356 && POJ 3370 鸽巢原理

    POJ 2356: 题目大意: 给定n个数,希望在这n个数中找到一些数的和是n的倍数,输出任意一种数的序列,找不到则输出0 这里首先要确定这道题的解是必然存在的 利用一个 sum[i]保存前 i 个数 ...

  2. hrbust oj 1536 Leonardo's Notebook 置换群问题

    题目大意: 给出一个A~Z的置换G,问能否找到一个A~Z的置换G' 能够用来表示为 G = G'*G' 由定理: 任意一个长为 L 的置换的k次幂,都会把自己的每一个循环节分裂成gcd(L, K)份, ...

  3. Mutual Training for Wannafly Union #5

    A(UVA12336) 题意:给一个n*m(n,m<=1e5)的棋盘,棋盘上有一些障碍点不能放棋子,现在要在棋盘上放4个棋子,满足A->B->C->D->A,其中走的规则 ...

  4. 洛谷(cogs 1293/bzoj 1212) P2292 [HNOI2004]L语言

    1293. [HNOI2004] L语言 ★★★   输入文件:language.in   输出文件:language.out   简单对比时间限制:1 s   内存限制:162 MB [题目描述] ...

  5. Ubuntu 16.04修复PDF默认使用ImageMagick打开无法设置其它默认的问题(默认打开程序设置)

    打开:~/.config/mimeapps.list 去掉以下几项: image/pdf=display-im6.desktop image/pdf=display-im6.q16.desktop;d ...

  6. systemtap --diskio

    http://blog.163.com/digoal@126/blog/static/1638770402013101993142404

  7. 好纠结啊,JEECG 商业版本号和开源版本号有什么差别呢?

    好纠结啊.JEECG 商业版本号和开源版本号有什么差别呢? 2014-12-18 jeecg JEECG tp=webp" style="width: 698px;" a ...

  8. 【翻译自mos文章】将expdp的dmp文件从asm磁盘组里边放到本地文件系统里边

    将expdp的dmp文件从asm磁盘组里边放到本地文件系统里边 參考原文: How To Extract Datapump File From ASM Diskgroup To Local Files ...

  9. 模块化开发(二)--- seaJs入门学习

    SeaJS是一个基于CMD模块定义规范实现一个模块系统加载器   [CMD规范](https://github.com/cmdjs/specification/blob/master/draft/mo ...

  10. splay专题复习——bzoj 3224 &amp; 1862 &amp; 1503 题解

    [前言]快要省选二试了.上次去被虐出翔了~~这次即便是打酱油.也要打出风採!于是暂停新东西的学习.然后開始复习曾经的知识,为骗分做准备.PS:区间翻转的临时跳过,就算学了也来不及巩固了. [BZOJ3 ...