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. File类常用方法

    File类是IO中常用的类 先介绍几个常用的方法: public boolean canRead(),public boolean canWrite() 测试当前文件是否可读可写,若是则返回true ...

  2. hdu4336 Card Collector 状态压缩dp

    Card Collector Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...

  3. Zookeeper 在Hadoop中的应用

    Zookeeper 简单介绍 Zookeeper 分布式服务框架是 Apache Hadoop 的一个子项目.它主要是用来解决分布式应用中常常遇到的一些数据管理问题,如:统一命名服务.状态同步服务.集 ...

  4. 什么是DNS劫持和DNS污染?

    什么是DNS劫持和DNS污染? http://blogread.cn/it/article/7758?f=weekly 说明 我们知道,某些网络运营商为了某些目的,对 DNS 进行了某些操作,导致使用 ...

  5. tasklet和工作队列

    tasklet机制和工作队列 http://blog.chinaunix.net/uid-28236237-id-3450753.html tasklet原理 http://www.kuqin.com ...

  6. Java学习02

    Java学习02 1.导入内部的包 一.在包的下面加入下面一句话: import    java.util.Scanner; 二.在类中 Scanner input=new     Sanner(Sy ...

  7. apache基本配置

    apache基本配置               apache本地虚拟域名配置  

  8. nginx启动

    查看nginx的进程 ps -ef | grep nginx 重启nginx的3种办法1.service nginx restart2.改了配置文件让其生效办法 nginx -s reload3.到n ...

  9. QT通过IP地址定位地址(用get方法取数据)

    通过IP地址定位地址,是要通过查询数据库,如果自己做一个这样的数据库工作量就比较大,所以在网上找了一个查询IP地址的网址,通过调用这个网址查询来实现,但是这个有一定的弊端,如果没有网络或者这个网址不可 ...

  10. 设计模式总结5--命令模式 commend pattern

    命令模式把发出命令的责任和执行命令的责任分割开,委派给不同的对象.就像我们去餐厅,点菜是找服务员,然后服务员去让厨师做菜而不是我们直接找厨师做菜 public interface Commend { ...