深入理解sizeof

阳光的小飞鱼91

阳光的小飞鱼91

2016-01-29 12:24

深入理解sizeof,深入理解sizeof

深入理解sizeof


作者:房秉毅

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/cyuyanjiaocheng/)


  最近在论坛里总有人问关于sizeof的问题,并且本人对这个问题也一直没有得到很好的解决,索性今天对它来个较为详细的总结,同时结合strlen进行比较,如果能对大家有点点帮助,这是我最大的欣慰了。

一、好首先看看sizeof和strlen在MSDN上的定义:

首先看一MSDN上如何对sizeof进行定义的:

sizeof Operatorsizeof expressionThe 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.The expression is either an identifier or a type-cast expression (a type specifier enclosed in parentheses).When applied to a structure type or variable, sizeof returns the actual size, which may include padding bytes inserted for alignment. When applied to a statically dimensioned array, sizeof returns the size of the entire array. The sizeof operator cannot return the size of dynamically allocated arrays or external arrays.

然后再看一下对strlen是如何定义的:

strlenGet the length of a string.Routine Required Header:strlen <string.hsize_t strlen( const char *string );Parameterstring:Null-terminated string LibrariesAll versions of the C run-time libraries.Return ValueEach of these functions returns the number of characters in string, excluding the terminal NULL. No return value is reserved to indicate an error.RemarksEach of these functions returns the number of characters in string, not including the terminating null character. wcslen is a wide-character version of strlen; the argument of wcslen is a wide-character string. wcslen and strlen behave identically otherwise.
二、由几个例子说开去。

第一个例子:
char* ss = "0123456789";sizeof(ss) 结果 4 ===》ss是指向字符串常量的字符指针sizeof(*ss) 结果 1 ===》*ss是第一个字符char ss[] = "0123456789";sizeof(ss) 结果 11 ===》ss是数组,计算到位置,因此是10+1sizeof(*ss) 结果 1 ===》*ss是第一个字符char ss[100] = "0123456789";sizeof(ss) 结果是100 ===》ss表示在内存中的大小 100×1strlen(ss) 结果是10 ===》strlen是个函数内部实现是用一个循环计算到为止之前int ss[100] = "0123456789";sizeof(ss) 结果 400 ===》ss表示再内存中的大小 100×4strlen(ss) 错误 ===》strlen的参数只能是char* 且必须是以''''结尾的char q[]="abc";char p[]="an";sizeof(q),sizeof(p),strlen(q),strlen(p);结果是 4 3 3 2      
第二个例子:
class X{int i;int j;char k;};X x;cout<<sizeof(X)<<endl; 结果 12 ===》内存补齐cout<<sizeof(x)<<endl; 结果 12 同上
第三个例子:
char szPath[MAX_PATH]
  如果在函数内这样定义,那么sizeof(szPath)将会是MAX_PATH,但是将szPath作为虚参声明时(void fun(char szPath[MAX_PATH])),sizeof(szPath)却会是4(指针大小)

三、sizeof深入理解。1.sizeof操作符的结果类型是size_t,它在头文件中typedef为unsigned int类型。该类型保证能容纳实现所建立的最大对象的字节大小。2.sizeof是算符,strlen是函数。3.sizeof可以用类型做参数,strlen只能用char*做参数,且必须是以''''结尾的。sizeof还可以用函数做参数,比如:
short f();printf("%dn", sizeof(f()));
输出的结果是sizeof(short),即2。4.数组做sizeof的参数不退化,传递给strlen就退化为指针了。5.大部分编译程序 在编译的时候就把sizeof计算过了 是类型或是变量的长度这就是sizeof(x)可以用来定义数组维数的原因
char str[20]="0123456789";int a=strlen(str); //a=10;int b=sizeof(str); //而b=20;
6.strlen的结果要在运行的时候才能计算出来,时用来计算字符串的长度,不是类型占内存的大小。7.sizeof后如果是类型必须加括弧,如果是变量名可以不加括弧。这是因为sizeof是个操作符不是个函数。8.当适用了于一个结构类型时或变量, sizeof 返回实际的大小, 当适用一静态地空间数组, sizeof 归还全部数组的尺 寸。 sizeof 操作符不能返回动态地被分派了的数组或外部的数组的尺寸9.数组作为参数传给函数时传的是指针而不是数组,传递的是数组的首地址,如:
fun(char [8])fun(char [])
都等价于fun(char *)在C++里传递数组永远都是
展开更多 50%)
分享

猜你喜欢

深入理解sizeof

C语言教程 C语言函数
深入理解sizeof

深入理解 ViewState

Web开发
深入理解 ViewState

s8lol主宰符文怎么配

英雄联盟 网络游戏
s8lol主宰符文怎么配

android ListView深入理解

编程语言 网络编程
android ListView深入理解

java 递归深入理解

编程语言 网络编程
java 递归深入理解

lol偷钱流符文搭配推荐

英雄联盟 网络游戏
lol偷钱流符文搭配推荐

Linux系统进程深入理解

服务器
Linux系统进程深入理解

深入理解Linux文本流

电脑入门
深入理解Linux文本流

lolAD刺客新符文搭配推荐

英雄联盟
lolAD刺客新符文搭配推荐

Flash鼠绘新手入门教程:绘制娇艳郁金香技法

Flash鼠绘新手入门教程:绘制娇艳郁金香技法

客户端回调实现gridView无刷新分页

客户端回调实现gridView无刷新分页
下拉加载更多内容 ↓