sizeof与strlen的区别 浅谈
1、sizeof operator
sizeof是C语言的一种单目操作符,如C语言的其他操作符++、- - 等,它并不是函数.
Queries size of the object or type.
- returns size in bytes of the object representation of type.
- returns size in bytes of the object representation of the type, that would be returned by expression, if evaluated.
Syntax | |
---|---|
sizeof(type) | (1) |
sizeof expression | (2) |
struct Empty {};
struct Base { int a; };
struct Derived : Base { int b; };
struct Bit {unsigned bit:1; };
int main(){
Empty e;
Derived d;
Base& b = d;
Bit bit;
std::cout<< "size of empty class: " << sizeof e << '\n'
<< "size of pointer : " << sizeof &e << '\n'
// << "size of function: " << sizeof(void()) << '\n' // compile error
// << "size of incomplete type: " << sizeof(int[]) << '\n' // compile error
// << "size of bit field: " << sizeof bit.bit << '\n' // compile error
<< "size of array of 10 int: " << sizeof(int[10]) << '\n'
<< "size of the Derived: " << sizeof d << '\n'
<< "size of the Derived through Base: " << sizeof b << '\n';
}
size of empty class: 1
size of pointer : 8
size of array of 10 int: 40
size of the Derived: 8
size of the Derived through Base: 4
2、strlen
size_t strlen ( const char * str );
Get string length
Returns the length of the C string str.
以"\0"结束,不包含"\0"
mystr[100]="test string";
sizeof(mystr) evaluates to 100, strlen(mystr) returns 11.
以上两个都不怎么用到,面试的时候问起,自己不是十分了解,遂查资料。
sizeof与strlen的区别 浅谈的更多相关文章
- sizeof和strlen的区别
一.sizeof sizeof(...)是运算符,而不是一个函数. sizeof操作符的结果类型是size_t,在头文件中typedef为unsigned int,其值在编译时即计算好了, ...
- Sizeof与Strlen的区别与联系
转自:http://www.cnblogs.com/carekee/articles/1630789.html 一.sizeof sizeof(...)是运算符,在头文件中typedef为uns ...
- Sizeof与Strlen的区别与联系(转)
Sizeof与Strlen的区别与联系 一.sizeof sizeof(...)是运算符,在头文件中typedef为unsigned int,其值在编译时即计算好了,参数可以是数组.指针.类型 ...
- C++-sizeof和strlen的区别
一.sizeof sizeof(...)是运算符,在头文件中typedef为unsigned int,其值在编译时即计算好了,参数可以是数组.指针.类型.对象.函数等. 它的功能是:获得保 ...
- sizeof和strlen的区别和联系总结
link:http://blog.csdn.net/ghevinn/article/details/9974967 strlen所作的仅仅是一个计数器的工作,它从内存的某个位置(可以是字符串开头 ...
- 【转】Sizeof与Strlen的区别与联系
原文地址:http://www.cnblogs.com/carekee/articles/1630789.html 1.sizeof sizeof(...)是运算符,在头文件中typedef为uns ...
- C++Sizeof与Strlen的区别与联系
一.sizeof sizeof(...)是运算符,在头文件中typedef为unsigned int,其值在编译时即计算好了,参数可以是数组.指针.类型.对象.函数等. 它的功能是:获得保 ...
- Sizeof与Strlen的区别【转】
本文转载自:http://www.cnblogs.com/carekee/articles/1630789.html Sizeof与Strlen的区别与联系 一.sizeof sizeof(.. ...
- 我也介绍下sizeof与strlen的区别
本节我也介绍下sizeof与strlen的区别,很简单,就几条: 1. sizeof是C++中的一个关键字,而strlen是C语言中的一个函数:2. sizeof求的是系统分配的内存总量,而strle ...
随机推荐
- HDU Computer Transformation1041 题解
Problem Description A sequence consisting of one digit, the number 1 is initially written into a com ...
- C#中Cache用法
C#中Cache用法 Cache 是分配在服务器上的一个公共的内存片,所谓公共指的cache只要一创建是任何一个客户端浏览器都可以通过后台代码访问到它,它面向的是所有用户,相对而言sessio ...
- 用户登录session_id观看
通过使用浏览器firefox或者google看cookie id, 这样就知道登录状态怎么样了
- Android中的一些基础知识(一)
翻译自这里,并做了部分修改. 什么是Android? Android是为移动设备提供的软件,它包括操作系统.中间件.和一些关键的应用程序.应用程序执行它自己的进程和在Dalvik虚拟机中的实例. An ...
- Asp.Net MVC4.0 官方教程 入门指南之四--添加一个模型
Asp.Net MVC4.0 官方教程 入门指南之四--添加一个模型 在这一节中,你将添加用于管理数据库中电影的类.这些类是ASP.NET MVC应用程序的模型部分. 你将使用.NET Framewo ...
- 猪猪的机器学习笔记(十四)EM算法
EM算法 作者:樱花猪 摘要: 本文为七月算法(julyedu.com)12月机器学习第十次次课在线笔记.EM算法全称为Expectation Maximization Algorithm,既最大 ...
- JavaSE学习总结第07天_面向对象2
07.01 成员变量和局部变量的区别 1.在类中的位置不同 成员变量 类中方法外 局部变量 方法内或者方法声明上 2.在内存中的位置不同 成员变量 堆内存 局部变量 栈内存 3 ...
- ThinkPHP第十一天(关联模型使用,独立分组配置,MySQL concat用法)
1.关联模型的使用 定义方式:新建一个类文件UserRelationModel.class.php Class UserRelationModel extends RelationModel{ pro ...
- 无法更新 EntitySet“GuigeInfo”,因为它有一个 DefiningQuery,而 <ModificationFunctionMapping> 元素中没有支持当前操作的 <InsertFunction> 元素。
1:实体中必须有主键 2:删除创建的模型重新创建
- 「操作系统」: Conditional Move Instructions(trap)
Not all conditional expressions can be compiled using conditional moves. Most significantly, the abs ...