博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
sizeof()与strlen()的区别与联系
阅读量:6839 次
发布时间:2019-06-26

本文共 2465 字,大约阅读时间需要 8 分钟。

从外部特性来看:

#include <stdio.h> #include <string> int main() { char array[] = "12345"; int n = sizeof(array); // n is 6 int m = strlen(array); // m is 5 array[3] = 0; n = sizeof(array); // n is 6 m = strlen(array); // m is 3 return 0; }

 

两者最本质的区别在于sizeof是操作符,而strlen是函数。

他们在MSDN中的定义分别为:

sizeof Operator

sizeof expression

 

The sizeof keyword gives the amount of storage, in bytes, associated with a variable or a type (including aggregate types). This keyword returns a value of type size_t.

The expression is either an identifier or a type-cast expression (a type specifier enclosed in

parentheses).

When applied to a structure type or variable, sizeof returns the actual size, which may include padding bytes inserted for alignment. When applied to a statically dimensioned array, sizeof returns the size of the entire array. The sizeof operator cannot return the size of dynamically allocated arrays or external arrays.

 -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

strlen

 

Get the length of a string.

 

Routine Required Header:

strlen <string.h>

 

size_t strlen( const char *string );

Parameter

string:Null-terminated string

Libraries

All versions of the C run-time libraries.

 

Return Value

Each of these functions returns the number of characters in string, excluding the terminal

NULL. No return value is reserved to indicate an error.

 

Remarks

Each of these functions returns the number of characters in string, not including the

terminating null character. wcslen is a wide-character version of strlen; the argument of

wcslen is a wide-character string. wcslen and strlen behave identically otherwise.

 

可见strlen是针对string的,而sizeof仅仅是一个keyword.

大部分编译器在编译时就计算了sizeof的值,而strlen则是在程序执行过程中计算的。

sizeof计算出的是该类型所占内存的大小,而strlen计算出的则是字符串的实际长度。

 

Evaluating sizeof expr does not evaluate the expression.

The result of applying sizeof depends in part on the type involved:

l  sizeof char or an expression of type char is guaranteed to be 1

l  sizeof a reference type returns the size of the memory necessary to contain an object of the referenced type

l  sizeof a pointer returns the size needed hold a pointer; to obtain the size of the object to which the pointer pointers, the pointer must be dereferenced

l  sizeof an array is equivalent to taking the sizeof the element type times the number of elements in the array

 

 

转载地址:http://tczul.baihongyu.com/

你可能感兴趣的文章
linux 系统管理之磁盘阵列RAID和压缩命令
查看>>
Widgets must be created in the GUI thread
查看>>
JQuery Highcharts图表控件使用说明
查看>>
python基础教程
查看>>
linux命令:function脚本编程之函数
查看>>
Linux性能监控之CPU利用率
查看>>
第九节 VMware View 6.0 菜鸟入门 连接服务器的安装和部署
查看>>
spring容器加载完毕做一件事情(利用ContextRefreshedEvent事件)
查看>>
C# 文件操作详解(二)---------FileInfo类
查看>>
Windows Server 2012系列---文件服务器资源管理器FSRM(2)
查看>>
JPA注解
查看>>
LogMiner详细讲解
查看>>
03.17基本控件的使用
查看>>
ElementaryOS 安装PhpStorm
查看>>
nutch与起点R3集成之笔记(二)
查看>>
ThinkPHP 统计查询
查看>>
厚黑学
查看>>
C++异常处理机制之一
查看>>
CentOS 5.2安装
查看>>
js prototype的理解
查看>>