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. 表格java代码的相关知识积累

    本文主要收集各大博客中的java表格 用JSP创建一个表格模板 . 项目中要用到一些展示信息的表格,表头不固定,表格内容是即时从后台取的:考虑到复用性,笔者用jsp编写了一个表格模板,可以从reque ...

  2. nginx自定义模块记录上游服务器特定响应头

    功能,服务器通过扩展自定义命令,记录上游的服务器返回的特定响应头内容,记录到本地文件中 代码如下: /* * Copyright (C) Ciaos */ #include <ngx_confi ...

  3. HDU2795 billboard【转化为线段树。】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2795 hhanger大神的题目,水题都得有点思维. 题意:h*w的木板,放进一些1*L的物品,求每次放 ...

  4. 一张图比較 Docker 和 Git:镜像管理设计理念

    Docker 的镜像管理设计中大量借鉴了 Git 的理念. 以下这张图将对两者的核心概念和操作进行比較,有助于大家高速掌握管理 Docker 镜像的正确方式. watermark/2/text/aHR ...

  5. Foundation Sorting: Shellsort

    /* Shell Sorting. * Implemention history:. * 2013-09-15, Mars Fu, first version. */ /* [Shell Sortin ...

  6. 【Linux操作系统分析】设备驱动处理流程

    1 驱动程序,操作系统,文件系统和应用程序之间的关系 字符设备和块设备映射到操作系统中的文件系统,由文件系统向上提供给应用程序统一的接口用以访问设备. Linux把设备视为文件,称为设备文件,通过对设 ...

  7. 编写可维护的JavaScript—语句和表达式&变量、函数和运算符

    语句和表达式 所有的块语句都应当使用花括号.包括: if for while do…while… try…catch…finally //不好的写法 if (condition) doSomethin ...

  8. HDU 1465 不容易系列之排错

    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u   Description 大家常常感 ...

  9. asp.net 开发注意的几点

    WIN7中组件服务中的DCOM配置找不到Microsoft Excel应用程序的解决办法: 这主要是64位系统的问题,excel是32位的组件,所以在正常的系统组件服务里是看不到的 可以通过在运行里面 ...

  10. Week14(12月11日):路由

    Part I:提问 =========================== 1.Visual Studio 2013自带ASP.NET MVC最高版本为(). A.3  B.4  C.5  D.6 2 ...