选择显示字体大小

深入理解sizeof

  最近在论坛里总有人问关于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.h>size_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是数组,计算到\\\\\\\\\\\\\\\\0位置,因此是10+1sizeof(*ss) 结果 1 ===》*ss是第一个字符char ss[100] = "0123456789";sizeof(ss) 结果是100 ===》ss表示在内存中的大小 100×1strlen(ss) 结果是10 ===》strlen是个函数内部实现是用一个循环计算到\\\\\\\\\\\\\\\\0为止之前int ss[100] = "0123456789";sizeof(ss) 结果 400 ===》ss表示再内存中的大小 100×4strlen(ss) 错误 ===》strlen的参数只能是char* 且必须是以\\\\\\\\\\\\\\\'\\\\\\\\\\\\\\\'\\\\\\\\\\\\\\\\0\\\\\\\\\\\\\\\'\\\\\\\\\\\\\\\'结尾的char q[]="abc";char p[]="a\\\\\\\\\\\\\\\\n";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深入理解。

四、结束语

sizeof使用场合。

·本文关键字:程序设计 / c++ / 基础教程


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons