sizeof与strlen的比较(C++)

版权声明:本文为博主原创文章,商业转载请联系作者获得授权,非商业转载请注明出处。 https://blog.csdn.net/liitdar/article/details/81777539

本文主要介绍在C++语言中,sizeof 与 strlen 两种用法的比较。

1 sizeof与strlen的比较

  • sizeof 是C++的一个运算符,而 strlen 是一个函数(头文件为string.h);
  • sizeof 的操作对象(即操作数operand)可以是数组、指针、类型、对象、函数等,而 strlen 的参数为字符型指针“const char * str”,当数组名作为 strlen 函数参数时,实际上在函数执行时该数组就退化成指针了;
  • sizeof 与 strlen 的返回值类型均为 size_t(即 unsigned int);
  • sizeof 的值在编译的时候已经计算好了,而 strlen 是在运行时才计算的。由于是在编译时计算,sizeof 不能用来计算动态分配的内存空间的大小,实际上,使用 sizeof 计算操作数(operand)的大小,其返回值跟操作数存储的内容无关;
  • sizeof 的作用是:It is a compile time unary operator which can be used to compute the size of its operand. sizeof 可用来获得保证能容纳所建立的最大对象的字节大小,而 strlen 的作用是:Returns the length of the C string str.;该 str 字符串可能是用户自己定义的,也可能是内存中随机获取的,strlen 函数实际完成的功能是从 str 字符串的第一个地址开始遍历,直到遇到结束符 NULL('\0')。需要注意,strlen 返回的字符串长度大小不包括 NULL('\0');
  • sizeof 对于常见的操作数的计算规则如下:
    • 数组 -- 编译时为该数组分配的空间大小;
    • 指针 -- 存储指针所用的空间大小,一般都是固定值,为整型 int 的对应字节数 8;
    • 类型 -- 指定类型所占的空间大小,如 int 占用8字节、char 占用 1 字节;
    • 对象 -- 对象实际占用的空间大小;
    • 函数 -- 函数的返回类型所占的空间大小,如 int 占用8字节。需要注意的是,该函数的返回类型不能是 void。

2 示例

下面通过代码示例,更清晰地比较 sizeof 与 strlen 的用法和作用。

2.1 示例1

示例代码(sizeof_and_strlen_test1.cpp)如下:

#include <iostream>
#include <string>
#include <string.h>

using namespace std;

int FunA()
{
    cout << "hello world" << endl;

    return 0;
}

int main()
{
    // 指针类型
    const char* pszTest = "pointer";
    cout << "----------------------------------" << endl;
    cout << "const char* pszTest = \"pointer\"" << endl;
    // 指针类型的大小
    cout << "sizeof(pszTest) is: " << sizeof(pszTest) << endl;
    // 指针所指向的类型(char)的大小
    cout << "sizeof(*pszTest) is: " << sizeof(*pszTest) << endl;
    // 字符串的长度
    cout << "strlen(pszTest) is: " << strlen(pszTest) << endl;
    // 字符串中首字符的长度
    //cout << "strlen(*pszTest) is: " << strlen(*pszTest) << endl;    // 此行会报出编译错误

    // 数组类型
    char arrTest[10] = "array";
    cout << "----------------------------------" << endl;
    cout << "char arrTest[10] = \"array\"" << endl;
    // 数组的大小
    cout << "sizeof(arrTest) is: " << sizeof(arrTest) << endl;
    // 数组中字符串的长度
    cout << "strlen(arrTest) is: " << strlen(arrTest) << endl;

    // 函数类型
    cout << "----------------------------------" << endl;
    // 函数类型的大小
    cout << "sizeof(FunA()) is: " << sizeof(FunA()) << endl;
    // 函数类型的长度
    //cout << "strlen(FunA()) is: " << strlen(FunA()) << endl;    // 此行会报出编译错误

    return 0;
}

编译并执行上述代码,结果如下:

2.2 示例2

示例代码(sizeof_and_strlen_test2.cpp)如下:

#include <iostream>
#include <string>
#include <string.h>

using namespace std;

int main()
{
    // 指针类型
    char *pszTest = new char[10];
    cout << "----------------------------------" << endl;
    cout << "char *pszTest = new char[10]" << endl;
    // 字符串的长度
    cout << "strlen(pszTest) is: " << strlen(pszTest) << endl;
    // 指针类型的大小
    cout << "sizeof(pszTest) is: " << sizeof(pszTest) << endl;
    // 指针所指向的类型(char)的大小
    cout << "sizeof(*pszTest) is: " << sizeof(*pszTest) << endl;

    return 0;
}

编译并执行上述代码,结果如下:

注意:从上面的结果能够看到,代码中只为“char *pszTest”分配了空间,并未进行初始化,但是从执行结果看来,pszTest已经被初始化为空字符串了。

猜你喜欢

转载自blog.csdn.net/liitdar/article/details/81777539