全面总结sizeof(字节对齐、数组、类(继承、虚函数)、结构体、strlen)*最全面*

本文详细总结了C++中sizeof操作符的使用,涵盖基本数据类型、指针、数组、结构体、类、联合体的情况,并对比了sizeof与strlen的区别。通过实例探讨了字节对齐的原则和特殊情况,同时指出sizeof在函数参数传递时的注意事项,以及其在内存分配和类型扩展中的应用。
摘要由CSDN通过智能技术生成

原地址:http://www.vckbase.com/index.php/wv/789点击打开链接

http://blog.csdn.net/freefalcon/article/details/54839

http://www.cnblogs.com/chengxin1982/archive/2009/01/13/1374575.html

http://blog.csdn.net/shimazhuge/article/details/8238038

结合了几篇博文,一起总结了sizeof的问题。

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


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



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

01. sizeof Operator
02.  
03. sizeof expression
04.  
05. The sizeof keyword gives the amount of storage, in bytes, associated with a variable or a type
06. (including aggregate types). This keyword returns a value of type size_t.
07.  
08. The expression is either an identifier or a type-cast expression (a type specifier enclosed in
09. parentheses).
10.  
11. When applied to a structure type or variable, sizeof returns the actual size, which may include
12. padding bytes inserted for alignment. When applied to a statically dimensioned array, sizeof
13. returns the size of the entire array. The sizeof operator cannot return the size of dynamically
14. allocated arrays or external arrays.


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

01. strlen
02.  
03. Get the length of a string.
04.  
05. Routine Required Header:
06. strlen <string.h>
07.  
08. size_t strlenconst char *string );
09.
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值