1、sizeof operator

sizeof是C语言的一种单目操作符,如C语言的其他操作符++、- - 等,它并不是函数.

Queries size of the object or type.

  1. returns size in bytes of the object representation of type.
  2. 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

参考自 cppreference.com


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.

参考自 cplusplus.com


以上两个都不怎么用到,面试的时候问起,自己不是十分了解,遂查资料。

sizeof与strlen的区别 浅谈的更多相关文章

  1. sizeof和strlen的区别

    一.sizeof    sizeof(...)是运算符,而不是一个函数.    sizeof操作符的结果类型是size_t,在头文件中typedef为unsigned int,其值在编译时即计算好了, ...

  2. Sizeof与Strlen的区别与联系

    转自:http://www.cnblogs.com/carekee/articles/1630789.html 一.sizeof    sizeof(...)是运算符,在头文件中typedef为uns ...

  3. Sizeof与Strlen的区别与联系(转)

    Sizeof与Strlen的区别与联系 一.sizeof     sizeof(...)是运算符,在头文件中typedef为unsigned int,其值在编译时即计算好了,参数可以是数组.指针.类型 ...

  4. C++-sizeof和strlen的区别

    一.sizeof    sizeof(...)是运算符,在头文件中typedef为unsigned int,其值在编译时即计算好了,参数可以是数组.指针.类型.对象.函数等.    它的功能是:获得保 ...

  5. sizeof和strlen的区别和联系总结

    link:http://blog.csdn.net/ghevinn/article/details/9974967    strlen所作的仅仅是一个计数器的工作,它从内存的某个位置(可以是字符串开头 ...

  6. 【转】Sizeof与Strlen的区别与联系

    原文地址:http://www.cnblogs.com/carekee/articles/1630789.html 1.sizeof  sizeof(...)是运算符,在头文件中typedef为uns ...

  7. C++Sizeof与Strlen的区别与联系

    一.sizeof    sizeof(...)是运算符,在头文件中typedef为unsigned int,其值在编译时即计算好了,参数可以是数组.指针.类型.对象.函数等.    它的功能是:获得保 ...

  8. Sizeof与Strlen的区别【转】

    本文转载自:http://www.cnblogs.com/carekee/articles/1630789.html Sizeof与Strlen的区别与联系 一.sizeof    sizeof(.. ...

  9. 我也介绍下sizeof与strlen的区别

    本节我也介绍下sizeof与strlen的区别,很简单,就几条: 1. sizeof是C++中的一个关键字,而strlen是C语言中的一个函数:2. sizeof求的是系统分配的内存总量,而strle ...

随机推荐

  1. javascript中处理引号编码&#034;

    1. <c:out value='${jxOrgJsonStr}' escapeXml="false"/> 2.或者使用innerText 直接接受${jxOrgJso ...

  2. 使用jodconverter和swftools实现文件在线预览

    参考:仿百度文库解决方案(四)——利用JODConverter调用OpenOffice.org服务转换文档为PDF 文档在线预览主要用到如下两个工具 1,安装openoffice(同时下载jodcon ...

  3. android api 中文 (74)—— AdapterView.AdapterContextMenuInfo

    前言 本章内容是android.widget.AdapterView.AdapterContextMenuInfo,版本为Android 2.3 r1,翻译来自"cnmahj",欢 ...

  4. javaweb学习路之一--web项目搭建

    概述: 工作闲暇时间想要自己搭建一个web项目玩玩,没想到大半天才弄了一个springMVC+mybatis的网站,简直菜的不行,以下记录所有的步骤加深印象 使用环境 1.jdk1.8 2.maven ...

  5. BZOJ 1823: [JSOI2010]满汉全席( 2-sat )

    2-sat...假如一个评委喜好的2样中..其中一样没做, 那另一样就一定要做, 这样去建图..然后跑tarjan. 时间复杂度O((n+m)*K) ------------------------- ...

  6. [LeetCode]题解(python):049-Group Anagrams

    题目来源: https://leetcode.com/problems/anagrams/ 题意分析: 给定一个字符串数组,将用相同字母(包括个数)组成的字符串放到一起.比如["eat&qu ...

  7. MVC3 Razor @RenderSection

    Mvc3的Razor视图引擎还提供了@RenderSection @RenderSection在母版页中占个位,然后让使用此母版页的子页自己去呈现他们的Section. 在母版页_Layout.csh ...

  8. 转: 如何实现jQuery的Ajax文件上传

    [PHP文件上传] 在开始之前,我觉得是有必要把通WEB上传文件的原理简单说一下的.实际上,在这里不管是PHP,JSP,还是ASP处理上传的文件,其实都是WEB早已把文件上传到服务器了,我们只是运用上 ...

  9. qt qml 利用xmlhttprequest 调用有赞api

    最近朋友在有赞商城上面开了一个店铺,因为有实体店,一般卖商品后送货上门,但是打票时候老是人工用world文档人工复制黏贴订单打印小票, 所以就找我帮忙做一个软件专门打印小票的,就研究起来调用有赞第三方 ...

  10. 隐藏APK在Launcher中的启动图标 android开发教程

    隐藏APK在Launcher中的启动图标: APK的AndroidManifest.xml文件的主Activity中删除 intent-filter 中的 <category android:n ...