sizeof小览

http://blog.csdn.net/scythe666/article/details/47012347

一、文章来由—一道面试题迁出的探究

我发现我已经形成一种习惯写来由了,以后看博客的时候可以让我回忆起为什么出现这个问题,我用什么方法解决的,既然形成习惯就让这个习惯保持下去吧。今天实验室师姐在看书,一处不解,是关于sizeof的,大家讨论此问题后,我一向信服做了才知道答案,于是有了这篇文章。但是只能叫小览,因为不可能总结完sizeof的用法,欢迎补充和讨论。

二、从这道题目说起

我直接将问题的关键部分提出来:

<code class="language-c++ hljs cpp has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">string</span> strArr1[] = { <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Trend"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Micro"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Soft"</span> };
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span> << <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"sizeof(strArr1) == "</span> << <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">sizeof</span>(strArr1) << endl;</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

请问输出多少,书上的答案这样写道:

而字符串strArr1是由3段构成的,所 以sizeof(strArr1)大小是12。  首先要明确sizeof不是函数,也不是一元运算符,它是个类似宏定义的特殊关键字,特别是sizeof(string)=4。

这个不去试是不知道的,因为编译器那么多,编译器做什么事情不去试怎么可能知道

果然,在vs2013 release Win32模式(x64模式还要更大)下结果是

sizeof(string) == 24  sizeof(strArr1) == 72

debug模式

sizeof(string) == 28  sizeof(strArr1) == 84

也是3倍关系,因为这是一个string数组,里面有三个string对象

那么为什么string有不同的结果?

查阅了相关资料得出结论:我们知道char*肯定是4字节,string里面可能不止包含一个char*那么简单,还包含有长度信息等其他信息,string的实现在各库中可能有所不同,但是在同一库中相同一点是,无论你的string里放多长的字符串,它的sizeof()都是固定的,字符串所占的空间是从堆中动态分配的,与sizeof()无关。

  sizeof(string)=4可能是最典型的实现之一,不过也有sizeof()为12、32字节的库实现。 但是VC6.0测试后sizeof(string)=16.还是跟编译器有关

为了完全测试一些东西,我测试越写越多,然后我来了威力加强版,见代码:

<code class="language-c++ hljs cpp has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#include<iostream></span>

<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">using</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">namespace</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">std</span>;

<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> main()
{
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">char</span> p[] = { <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'a'</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'b'</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'c'</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'a'</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'b'</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'c'</span> };
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">char</span> *p1 = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"abcabc"</span>;
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">char</span> p2[] = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"abcabc"</span>;
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">char</span> p3[][<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>] = { { <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'a'</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'b'</span> }, { <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'c'</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'a'</span> }, { <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'b'</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'c'</span> } };
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">printf</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"p == %s\n"</span>, p);
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span> << p << endl;
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span> << <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"sizeof(p) == "</span> << <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">sizeof</span>(p) << endl;
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span> << <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"sizeof(p1) == "</span> << <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">sizeof</span>(p1) << endl;
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span> << <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"sizeof(p2) == "</span> << <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">sizeof</span>(p2) << endl;
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span> << <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"sizeof(p3) == "</span> << <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">sizeof</span>(p3) << endl;

    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span><<<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"sizeof(string) == "</span> << <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">sizeof</span>(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">string</span>) << endl;

    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">string</span> strArr1[] = { <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Trend"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Micro"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Soft"</span> };
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span> << <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"sizeof(strArr1) == "</span> << <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">sizeof</span>(strArr1) << endl;

    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> a = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>;
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span> <<<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"sizeof(a = 3) == "</span> << <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">sizeof</span>(a = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>) << endl;
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span> << <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"a == "</span> << a << endl;

    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span> << <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"sizeof(999999) == "</span> << <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">sizeof</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">999999</span>) << endl;
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span> << <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"sizeof(9999999999999999999) == "</span> << <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">sizeof</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">9999999999999999999</span>) << endl;
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span> << <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"sizeof(9 / 5) == "</span> << <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">sizeof</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">9</span> / <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>) << endl;
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span> << <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"sizeof((double)9 / 5) == "</span> << <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">sizeof</span>((<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">double</span>)<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">9</span> / <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>) << endl;

    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>;
}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li></ul>

运行结果如图所示: 

我来一一解释我的这些测试在做什么:  (1)首先p的这种初始化方式,在末尾不会加’\0’,所以 sizeof(p) == 6;而且一个有趣的问题是printf和cout直接输出p是不同的,printf是要碰到’\0’结束,我也看了printf和cout的汇编代码,但是没有细究,之后又空详细研究一下  (2)p1和p2的sizeof不同,因为一个是指针,一个是字符数组,指针在Win32编译环境下的sizeof都是4,因为是4字节的地址,32bits可寻址空间  (3)p3是另一个有趣的问题,p3不能写成p3[3][]来初始化,因为这样初始化要保证二维数组每一行的个数相同,也就是不能出现“参差不齐”的情况,那种情况要动态分配  (4)这是一个陷阱

<code class="language-c++ hljs cpp has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> a = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>;
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span><<<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">sizeof</span>(a=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>)<<endl;
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span><<a<<endl;</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>

输出为什么是4,0 而不是期望中的4,3???就在于sizeof在编译阶段处理的特性。由于sizeof不能被编译成机器码,所以sizeof作用范围内,也就是()里面的内容不能被编译,而是被替换成类型。=操作符返回左操作数的类型,所以a=3相当于int,而代码也被替换为:

<code class="language-c++ hljs cpp has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span><<<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span><<endl;
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span><<a<<endl;</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

所以,sizeof是不可能支持链式表达式的,这也是和一元操作符不一样的地方。

不要把sizeof当成函数,也不要看作一元操作符,把他当成一个特殊的编译预处理。

(5)这样的原因是999999是一个编译器int型可以搞定的数,所以按int来处理,太大的数不能用int搞定,但是8个字节一定可以搞定的

sizeof(999999) == 4  sizeof(9999999999999999999) == 8

(6)最后是看了(9 / 5)编译器是作为int看待的,强制转换后才是double

三、sizeof 能总结多少是多少

sizeof博大精深,即使看了很多资料,一口气总结完也是不可能了,总结常用的就好。

1、什么是sizeof

首先看一下sizeof在msdn上的定义: The sizeof keyword gives the amount of storage, in bytes, associated with a variable or a type (including aggregate types). This keyword returns a value of type size_t. 看到return这个字眼,是不是想到了函数?错了,sizeof不是一个函数,你见过给一个函数传参数,而不加括号的吗?sizeof可以,所以sizeof不是函数。网上有人说sizeof是一元操作符,但是我并不这么认为,因为sizeof更像一个特殊的宏,它是在编译阶段求值的。举个例子:

<code class="language-c++ hljs cpp has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span><<<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">sizeof</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>)<<endl; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 32位机上int长度为4</span>
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span><<<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">sizeof</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>==<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>)<<endl; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// == 操作符返回bool类型,相当于 cout<<sizeof(bool)<<endl;</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>
在编译阶段已经被翻译为:
<code class="language-c++ hljs cpp has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span><<<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span><<endl;
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span><<<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span><<endl;</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

2、语法

sizeof有三种语法形式,如下:

1) sizeof( object ); // sizeof( 对象 );  2) sizeof( type_name ); // sizeof( 类型 );  3) sizeof object; // sizeof 对象;  所以,

int i;  sizeof( i ); // ok  sizeof i; // ok  sizeof( int ); // ok  sizeof int; // error  既然写法1可以完全代替写法3,为求形式统一以及减少我们大脑的负担,第3种写法,忘掉它吧!

实际上,sizeof计算对象的大小也是转换成对对象类型的计算,也就是说,同种类型的不同对象其sizeof值都是一致的。这里,对象可以进一步延伸至表达式,即sizeof可以对一个表达式求值,编译器根据表达式的最终结果类型来确定大小,一般不会对表达式进行计算。如:

sizeof( 2 ); // 2的类型为int,所以等价于 sizeof( int );  sizeof( 2 + 3.14 ); // 3.14的类型为double,2也会被提升成double类型,所以等价于 sizeof( double );

3、函数的sizeof

函数类型

考虑下面的问题:
<code class="language-c++ hljs cpp has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> f1(){<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>;};
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">double</span> f2(){<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0.0</span>;}
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> f3(){}

<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span><<<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">sizeof</span>(f1())<<endl; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// f1()返回值为int,因此被认为是int</span>
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span><<<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">sizeof</span>(f2())<<endl; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// f2()返回值为double,因此被认为是double</span>
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span><<<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">sizeof</span>(f3())<<endl; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 错误!无法对void类型使用sizeof</span>
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span><<<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">sizeof</span>(f1)<<endl;   <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 错误!无法对函数指针使用sizeof   </span>
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span><<<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">sizeof</span>*f2<<endl;   <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// *f2,和f2()等价,因为可以看作object,所以括号不是必要的。被认为是double</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li></ul>

结论:对函数使用sizeof,在编译阶段会被函数返回值的类型取代

4、数组的sizeof

<code class="language-c++ hljs cpp has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">char</span> a[] = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"abcdef"</span>;
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> b[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">20</span>] = {<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span>};
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">char</span> c[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>][<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>] = {<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"aa"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"bb"</span>};

<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span><<<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">sizeof</span>(a)<<endl; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 7</span>
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span><<<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">sizeof</span>(b)<<endl; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 20*4=80</span>
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span><<<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">sizeof</span>(c)<<endl; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 6</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li></ul>

数组a的大小在定义时未指定,编译时给它分配的空间是按照初始化的值确定的,也就是7。c是多维数组,占用的空间大小是各维数的乘积,也就是6。可以看出,数组的大小就是他在编译时被分配的空间,也就是各维数的乘积*数组元素的大小。

结论:数组的大小是各维数的乘积*数组元素的大小。

这里有一个陷阱:

<code class="language-c++ hljs cpp has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> *d = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>];

<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span><<<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">sizeof</span>(d)<<endl; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 4</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>

d是我们常说的动态数组,但是他实质上还是一个指针,所以sizeof(d)的值是4。  再考虑下面的问题:

<code class="language-c++ hljs cpp has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">double</span>* (*a)[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>][<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">6</span>];
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span><<<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">sizeof</span>(a)<<endl;   <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 4</span>
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span><<<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">sizeof</span>(*a)<<endl;   <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 72</span>
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span><<<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">sizeof</span>(**a)<<endl; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 24</span>
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span><<<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">sizeof</span>(***a)<<endl; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 4</span>
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span><<<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">sizeof</span>(****a)<<endl; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 8</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li></ul>

a是一个很奇怪的定义,他表示一个指向 double*[3][6]类型数组的指针。既然是指针,所以sizeof(a)就是4。

既然a是执行double*[3][6]类型的指针,a就表示一个double[3][6]的多维数组类型,因此sizeof(a)=3*6*sizeof(double)=72。同样的,*a表示一个double[6]类型的数组,所以sizeof(*a)=6*sizeof(double)=24。**a就表示其中的一个元素,也就是double了,所以sizeof(***a)=4。至于****a,就是一个double了,所以sizeof(****a)=sizeof(double)=8。

差不多也要结束了,如果更进一步了解,需要查阅更多的资料~~~

—END—


参考文献

[1] http://blog.csdn.net/freefalcon/article/details/54839 [2] http://www.cnblogs.com/wanghetao/archive/2012/04/04/2431760.html

版权声明:欢迎转载,注明出处就好!如果不喜欢请留言说明原因再踩哦,谢谢,我也可以知道原因,不断进步!!

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏轮子工厂

8. 一花一世界,while for 循环?

wo这周有点懒啊,这才第 2 篇,个人有点事情,本来预计可以更新……1 篇的,︿( ̄︶ ̄)︿

972
来自专栏SHERlocked93的前端小站

JS 原型模式

原型模式(Prototype pattern),用原型实例指向创建对象的类,使用于创建新的对象的类的共享原型的属性与方法。

1551
来自专栏编程

您真的会用switch吗?

C语言的理念,程序员应该知道自己正在干什么,而且保证自己的所作所为是正确的。 switch知多少 各个case和default的顺序可以是任意的,但习惯上总是d...

2047
来自专栏逸鹏说道

Python3 与 C# 基础语法对比(String专栏)

Python3 与 C# 基础语法对比:https://www.cnblogs.com/dotnetcrazy/p/9102030.html

1193
来自专栏恰同学骚年

你必须知道的指针基础-3.指针的移动及指针的危险

  指针每次加一就是指针向前移动指针类型对应的字节数。下面通过一个int指针来指向一个int数组,看看指针的加法运算到底是个什么鬼?

832
来自专栏企鹅号快讯

给初学者:JavaScript 的常见注意点

作者: CarterLi 原文:https://segmentfault.com/a/1190000012730162 上篇说了一些 JS 中数组操作的常见误区...

2276
来自专栏绿巨人专栏

学习Scala: 初学者应该了解的知识

2814
来自专栏java一日一条

java中i=i++问题分析

程序的执行顺序是这样的:因为++在后面,所以先使用i,“使用”的含义就是i++这个表达式的值是0,但是并没有做赋值操作,它在整个语句的最后才做赋值,也就是说在做...

661
来自专栏微信公众号:Java团长

JAVA之旅(一)——基本常识,JAVA概念,开发工具,关键字/标识符,变量/常量,进制/进制转换,运算符,三元运算

比如6:6/2 = 3 余 0 3 / 2 = 1 余 1 那就是从个位数开始011,读起来就是110了

1641
来自专栏mathor

导入:什么是数据结构,为什么要学习数据结构,约瑟夫环的数组实现

1785

扫码关注云+社区