C++编程语言中sizeof和strlen介绍

本文主要介绍C++编程语言中sizeof和strlen的相关知识,同时通过示例代码介绍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对于常见的操作数的计算规则如下:
    • 数组 -- 编译时为该数组分配的空间大小;
    • 指针 -- 存储指针所用的空间大小,一般都是固定值,64位操作系统对应8字节;
    • 类型 -- 指定类型所占的空间大小,如int占用8字节、char占用1字节;
    • 对象 -- 对象实际占用的空间大小;
    • 函数 -- 函数的返回类型所占的空间大小,如int占用4字节。需要注意的是,该函数的返回类型不能是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已经在通过new分配空间时,被初始化为空字符串了。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

liitdar

赠人玫瑰,手有余香,君与吾共勉

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值