首页 技术 正文
技术 2022年11月13日
0 收藏 770 点赞 4,095 浏览 3124 个字

1. sizeof

1.1 sizeof是一个独立的运算符,不是函数。sizeof给我们提供有关数据项目所分配的内存的大小。例如:

12 cout << sizeof(long) << endl;   // 输出: 4cout << sizeof(double) << endl; // 输出:8

1.2 如果将sizeof应用于一个类型,必须要像上面所示那样使用括号。但如果对一个变量使用它,可以不用括号。

12 int x;cout << sizeof x << endl;       // 输出: 4

1.3 char类型表示单个字符,只占一个字节的内存空间,所以sizeof(char)=sizeof(‘a’)=1 。另外,相关数据项目为空,所以sizeof(‘’)不为零,而是报错。但是,“”为一个C风格的字符串,末尾会被自动加上结束符‘\n’,所以有sizeof(“”)=1,sizeof(“\0”)=2。

123456 char c = 'c';cout << sizeof(char) << endl;   // 输出:1cout << sizeof(c) << endl;      // 输出:1//cout << sizeof('') << endl;   // 编译时错误:不能为空字符常量cout << sizeof("") << endl;     // 输出:1cout << sizeof("\0") << endl;   // 输出:2

1.4 str1是一个指针,只是指向了字符串”hello”而已。所以sizeof(str1)不是字符串占的空间也不是字符数组占的空间,而是一个字符型指针占的空间。所以sizeof(str1)=sizeof(char*)=4,在C/C++中一个指针占4个字节。*str与str[0]一样,都表示字符串中的第一个字符‘h’,所以sizeof(*str1)=1。*(str1+1)表示第二个字符,依次类推。

1234 const char* str1 = "hello"; cout << sizeof(str1) << endl;   // 输出:4cout << sizeof(char*) << endl;  // 输出:4cout << sizeof(*str1) << endl;  // 输出:1

1.5 str2是一个字符型数组。C/C++规定,对于一个数组,返回这个数组占的总空间,所以sizeof(str2)取得的是字符串”hello” 占的总空间。”hello”中,共有h e l l o \0六个字符,所以str2数组的长度是6,所以sizeof(str2)=6*sizeof(char)=6 。

12 char str2[]="hello"; cout << sizeof(str2) << endl;   // 输出:6

1.6 str3已经定义成了长度是8的数组,所以sizeof(str3)=8*sizeof(char)=8 。

12 char str3[8]={'h',}; cout << sizeof(str3) << endl;   // 输出:8

1.7 如果给出的数据项目不是字符型数组,而是一种自定义类型对像数组,那么sizeof(str3)=8*sizeof(对像类型或对像)。如:

12345678 class A{    int x;    double d;};A a[10];cout << sizeof(A) << endl;      // 输出:16,此处有内存补齐,所以不是12cout << sizeof(a) << endl;      // 输出:160

1.8 大部分编译程序在编译的时候就把sizeof计算过了,这就是sizeof(x)可以用来定义数组长度的原因。下面数组a2的长度是根据a1的长度来定的。

1234 int a1[10];cout << sizeof(a1)/sizeof(int) << endl;     // 输出:10int a2[sizeof(a1)/sizeof(int)];cout << sizeof(a2)/sizeof(int) << endl;     // 输出:10

1.9 sizeof 操作符不能返回动态地被分派了的数组或外部的数组的尺寸,特别是当sizeof应用于虚参形式的数组时,得到的结果是4(指针大小) 。应牢记住数组在作为参数传递时,永远都是传递数组的地址。

1234567891011121314 #define MAXSIZE 100 // 此处等同于 void PrintSize(char* str)void PrintSize(char str[MAXSIZE]){    cout << sizeof(str) << endl;        // 输出:4} int main(){    char str1[MAXSIZE];    cout << sizeof(str1) << endl;       // 输出:100    PrintSize(str1);}

2.0 sizeof还可以应用于函数,注意函数fun只声明未定义:

1234567 double fun(); int main(){    cout << sizeof(fun()) << endl;      // 输出:8    //cout << sizeof(fun) << endl;      // 编译时出错:非法的sizeof操作数}

2.1 sizeof操作符不能用于函数类型,不完全类型或位字段。不完全类型指具有未知存储大小的数据类型,如未知存储大小的数组类型、未知内容的结构或联合类型、void类型等

12 //cout << sizeof(void) << endl;     // 编译时出错:非法的sizeof操作数cout << sizeof(void*) << endl;      // 输出:4,就是指针的存储空间

总之,对于指针,sizeof操作符返回这个指针占的空间,一般是4个字节;而对于一个数组,sizeof返回这个数组所有元素占的总空间。char*与char[]容易混淆,一定要分清。sizeof可应用于任何内置类型和自定义类型。

2. strlen

2.1 strlen是函数,它不区分是数组还是指针,就读到\0为止返回长度。而且strlen是不把/0计入字符串的长度的。使用时需要包函头文件string.h。

1234567 char str4[] = "hello";cout << strlen(str4) << endl;   // 输出: 5const char *str5 = "hello";cout << strlen(str5) << endl;   // 输出:5 cout << strlen("") << endl;     // 输出:0cout << strlen("\0") << endl;   // 输出:0

2.2 strlen只能用于C类型字符串,注意必须是字符串且以‘\0’结尾,用于其它任何类型都是错误的。

12345678910111213 //cout << strlen(int) << endl;  // 出错:不能用于类型//cout << strlen('') << endl;   // 出错:不能用于字符//cout << strlen('a') << endl;  // 出错:不能用于字符char str6[5] = {'h', 'e', 'l', 'l', 'o'};//cout << strlen(str6) << endl; // 运行时出错:str6不是一个"C风格字符串",即不是以'\0'                                    // 结尾的字符串,此处输出一个不确定值,本例为19class B{    int x;    double d;};B b[10];//cout << strlen(b) << endl;    // 出错:一句话,只能用于字符串,还必须是“C风格的”

2.3 strlen的结果要在运行的时候才能计算出来。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,027
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,518
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,365
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,146
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,780
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,857