sizeof和strlen的区别

最近找实习,做到sizeof和strlen的选择题,考的很变态,今天总结一下知识点。

1 sizeof运算符

首先明确一点,sizeof是运算符(操作符),和【+、-、*、%】这些一样。

所以它在编译期就已经计算好了。

它的参数可以是数组、指针、类型、对象、函数等。

它的功能:获得保证能容纳实现所建立的最大对象的字节大小。

具体而言,当参数不同时,sizeof的返回值含义也不同:

(1)数组:编译时分配的数组空间的大小;

(2)指针:存储指针空间大小,和类型无关,只和机器有关(在32位机上站4个字节,在64位机上站8个字节);

(3)类型:int、char之类的,表示该类型所占空间大小;

(4)对象:对象的实际占用空间大小(类的大小);

(5)函数:函数返回类型所占的大小。返回值不能是void(我目前还没见到这种)。

2 strlen函数

strlen是c/c++的库函数,要在运行时才能计算。参数的类型必须是字符型指针(char*)。

当数组名作为参数传入时,实际数组就退化成指针了。

它的功能:返回字符串的长度。

该字符串可能是自己定义的,也可能是内存中随机的,该函数实际完成的功能是从代表该字符串的第一个地址开始遍历,直到遇到结束符’\0’停止。返回的长度大小不包括‘\0’。

3 总结区别

二者的区别主要是以下四点:

  1. sizeof()是运算符,strlen()是库函数
  2. sizeof()在编译时计算好了,strlen()在运行时计算
  3. sizeof()计算出对象使用的最大字节数,strlen()计算字符串的实际长度
  4. sizeof()的参数类型多样化(数组,指针,对象,函数都可以),strlen()的参数必须是字符型指针(传入数组时自动退化为指针)

阅读下面代码,写出运算结果

测试1

#include <iostream>
using namespace std;

void Test1()
{
	char p[] = "hello";
	cout << "p: " << p << "   " << strlen(p) << "   " << sizeof(p) << endl;

	char p1[] = "hello\0";
	cout << "p1: " << p1 << "   " << strlen(p1) << "   " << sizeof(p1) << endl;

	char p2[] = "hello\\0";
	cout << "p2: " << p2 << "   " << strlen(p2) << "   " << sizeof(p2) << endl;

	char p3[] = "hello\\\0";
	cout << "p3: " << p3 << "   " << strlen(p3) << "   " << sizeof(p3) << endl;

	char p4[] = "hel\0lo";
	cout << "p4: " << p4 << "   " << strlen(p4) << "   " << sizeof(p4) << endl;

	char p5[] = "hel\\0lo";
	cout << "p5: " << p5 << "   " << strlen(p5) << "   " << sizeof(p5) << endl;
}



int main(int argc, char* argv[]) {
	
	Test1();
	system("pause");
	return 0;
}

首先一定要记住:strlen 计算的是字符串的实际长度,遇到\0即停止;sizeof 计算整个字符串所占内存字节数的大小,当然\0也要+1计算。

《为了更清楚的解析,我将有停止计算作用的/0整体写为A,实际不被控制台输出,无作用的写做/0可被输出;即strlen(A)=0,sizeof(A)=1;strlen(/0)=2,sizeof(/0)=2》

这里数组退化成指针了。

1 char p[] = "hello"; p=hello,strlen=5,sizeof=6

字符串hello的最后隐藏着一个\0。故sizeof计算所占字节数是6。即strlen(helloA)=5,sizeof(helloA)=6

2 char p1[] = "hello\0";p1 的结果:p1=hello,strlen=5,sizeof=7

这里\0主动结束了字符串长度的计算,但隐藏的\0仍然有意义;即strlen(helloAA)=5,sizeof(helloAA)=7

3 char p2[] = "hello\\0";p2 的结果:p2=hello\0,strlen=7,sizeof=8

第一个\转义了紧挨着的\,使得后面0只是一个普通字符,不能与前面/成为整体。即strlen(hello\0A)=7,sizeof(hello\0A)=8

4  char p3[] = "hello\\\0";p3 的结果:p3=hello\,strlen=6,sizeof=8

第一个\只转义了第二个\,使得第二个\不能再转义后面,所以第三个/与0成为整体并有结束计算的作用。即p3=hello\AA

5 char p4[] = "hel\0lo";p4 的结果:p4=hel,strlen=3,sizeof=7

strlen碰到\0就停止输出和计算,所以结果是3;但sizeof不会停止而且两个\0都计算,即sizeof计算helAloA

6 char p5[] = "hel\\0lo";p5 的结果:p5=hel\0lo,strlen=7,sizeof=8

第一个\转义了紧接的\,使得后面0只是普通字符,而sizeof比strlen多计算一个\0。即计算的p5=hel\0loA

实际运行结果,测试环境VS2015(debug+x86)

测试2

void Test2() {
	char* p = "hello";
	cout << "p: " << p << "   " << strlen(p) << "   " << sizeof(p) << endl;

	char* p1 = "hello\0";
	cout << "p1: " << p1 << "   " << strlen(p1) << "   " << sizeof(p1) << endl;

	char* p2 = "hello\\0";
	cout << "p2: " << p2 << "   " << strlen(p2) << "   " << sizeof(p2) << endl;

	char* p3 = "hello\\\0";
	cout << "p3: " << p3 << "   " << strlen(p3) << "   " << sizeof(p3) << endl;

	char* p4 = "hel\0lo";
	cout << "p4: " << p4 << "   " << strlen(p4) << "   " << sizeof(p4) << endl;

	char* p5 = "hel\\0lo";
	cout << "p5: " << p5 << "   " << strlen(p5) << "   " << sizeof(p5) << endl;
}

strlen的分析上面一样。sizeof指针的大小就是4。

char* p = "hello";       p=hello,strlen=5,sizeof=4

char* p1 = "hello\0";     p1 的结果:p1=hello,strlen=5,sizeof=4

char* p2 = "hello\\0";    p2 的结果:p2=hello\0,strlen=7,sizeof=4

char* p3 = "hello\\\0";    p3 的结果:p3=hello\,strlen=6,sizeof=4

char* p4 = "hel\0lo";     p4 的结果:p4=hel,strlen=3,sizeof=4

char* p5 = "hel\\0lo";    p5 的结果:p5=hel\0lo,strlen=7,sizeof=4

结果

测试3

void Test2()
{
	char p[8] = "hello";
	cout << "p: " << p << "   " << strlen(p) << "   " << sizeof(p) << endl;

	char p1[8] = "hello\0";
	cout << "p1: " << p1 << "   " << strlen(p1) << "   " << sizeof(p1) << endl;

	char p2[8] = "hello\\0";
	cout << "p2: " << p2 << "   " << strlen(p2) << "   " << sizeof(p2) << endl;

	char p3[8] = "hello\\\0";
	cout << "p3: " << p3 << "   " << strlen(p3) << "   " << sizeof(p3) << endl;

	char p4[8] = "hel\0lo";
	cout << "p4: " << p4 << "   " << strlen(p4) << "   " << sizeof(p4) << endl;

	char p5[8] = "hel\\0lo";
	cout << "p5: " << p5 << "   " << strlen(p5) << "   " << sizeof(p5) << endl;
}

数组长度要大于sizeof的长度。

char p[8] = "hello";       p=hello,strlen=5,sizeof=8

char p1[8] = "hello\0";     p1 的结果:p1=hello,strlen=5,sizeof=8

char p2[8] = "hello\\0";    p2 的结果:p2=hello\0,strlen=7,sizeof=8

char p3[8] = "hello\\\0";    p3 的结果:p3=hello\,strlen=6,sizeof=8

char p4[8] = "hel\0lo";     p4 的结果:p4=hel,strlen=3,sizeof=8

char p5[8] = "hel\\0lo";    p5 的结果:p5=hel\0lo,strlen=7,sizeof=8

结果

测试4

#include <iostream>
#include <string.h>
using namespace std;

void Test1()
{
    char p[] = {'h', 'e', 'l', 'l', 'o'};
    cout << "p: " << p << "   " << strlen(p) << "   " << sizeof(p) << endl;

    char p1[] = {'h', 'e', 'l', 'l', 'o', '\0'};
    cout << "p1: " << p1 << "   " << strlen(p1) << "   " << sizeof(p1) << endl;

    char p2[] = {'h', 'e', 'l', '\0', 'l', 'o'};
    cout << "p2: " << p2 << "   " << strlen(p2) << "   " << sizeof(p2) << endl;
}

int main(int argc, char* argv[]) {
    Test1();
	system("pause");
    return 0;
}

结果

char str[] = {'1', '2'};

sizeof(str)就是数组的大小;

如果有'\0',则strlen(str)就是遇到'\0'截至,如果没有'\0',则strlen(str)数组的长度

参考:【C++】strlen 和sizeof 的区别(小结)_ly_1115的博客-CSDN博客_sizeof和strlen在c++的区别

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值