1. sizeof

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

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

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

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

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

1
2
3
4
5
6
char c = 'c';
cout << sizeof(char) << endl;   // 输出:1
cout << sizeof(c) << endl;      // 输出:1
//cout << sizeof('') << endl;   // 编译时错误:不能为空字符常量
cout << sizeof("") << endl;     // 输出:1
cout << 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)表示第二个字符,依次类推。

1
2
3
4
const char* str1 = "hello";
cout << sizeof(str1) << endl;   // 输出:4
cout << sizeof(char*) << endl;  // 输出:4
cout << 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 。

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

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

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

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

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

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

1
2
3
4
int a1[10];
cout << sizeof(a1)/sizeof(int) << endl;     // 输出:10
int a2[sizeof(a1)/sizeof(int)];
cout << sizeof(a2)/sizeof(int) << endl;     // 输出:10

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#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只声明未定义:

1
2
3
4
5
6
7
double fun();
 
int main()
{
    cout << sizeof(fun()) << endl;      // 输出:8
    //cout << sizeof(fun) << endl;      // 编译时出错:非法的sizeof操作数
}

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

1
2
//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。

1
2
3
4
5
6
7
char str4[] = "hello";
cout << strlen(str4) << endl;   // 输出: 5
const char *str5 = "hello";
cout << strlen(str5) << endl;   // 输出:5
 
cout << strlen("") << endl;     // 输出:0
cout << strlen("\0") << endl;   // 输出:0

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

1
2
3
4
5
6
7
8
9
10
11
12
13
//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'
                                    // 结尾的字符串,此处输出一个不确定值,本例为19
class B
{
    int x;
    double d;
};
B b[10];
//cout << strlen(b) << endl;    // 出错:一句话,只能用于字符串,还必须是“C风格的”

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

sizeof 和 strlen的更多相关文章

  1. sizeof、strlen、字符串、数组,整到一块,你还清楚吗?

    写在前面 sizeof.strlen.字符串.数组,提到这些概念,相信学过C语言的人都能耳熟能详,也能谈得头头是道,但是,在实际运用中,当这些内容交织在一起时,大家却不一定能搞地清清楚楚,本文的目的正 ...

  2. sizeof和strlen的区别

    一.sizeof    sizeof(...)是运算符,而不是一个函数.    sizeof操作符的结果类型是size_t,在头文件中typedef为unsigned int,其值在编译时即计算好了, ...

  3. Sizeof与Strlen的区别与联系

    转自:http://www.cnblogs.com/carekee/articles/1630789.html 一.sizeof    sizeof(...)是运算符,在头文件中typedef为uns ...

  4. Sizeof与Strlen的区别与联系(转)

    Sizeof与Strlen的区别与联系 一.sizeof     sizeof(...)是运算符,在头文件中typedef为unsigned int,其值在编译时即计算好了,参数可以是数组.指针.类型 ...

  5. sizeof()和strlen()

    sizeof计算的是栈中大小 P { margin-bottom: 0.21cm; direction: ltr; color: rgb(0, 0, 0); text-align: justify } ...

  6. sizeof 和 strlen 的区别

    sizeof 和 strlen 都是c/c++ 中常见的符号,他们的功能是判断数组长度.那么他么到底有什么区别 1.sizeof    不是函数,而是一个操作符.字节数的计算在程序编译时进行,而不是在 ...

  7. C++-sizeof和strlen的区别

    一.sizeof    sizeof(...)是运算符,在头文件中typedef为unsigned int,其值在编译时即计算好了,参数可以是数组.指针.类型.对象.函数等.    它的功能是:获得保 ...

  8. sizeof 和 strlen 区别

    Sizeof与Strlen的区别与联系 一.sizeof    sizeof(...)是运算符,在头文件中typedef为unsigned int,其值在编译时即计算好了,参数可以是数组.指针.类型. ...

  9. sizeof()与strlen()的区别

    首先需要说明的是sizeof和strlen都可以求长度,但是却有很大的区别,简单来说可以概括为以下几点: 1.sizeof是一个关键字,而strlen确实一个函数. 2.sizeof求的是字节长度,而 ...

  10. sizeof和strlen小结

    sizeof和strlen小结 写在前面 之所以要总结一下sizeof和strlen的用法和区别,是因为这些知识可以帮助我们更加深入的理解各种数据结构在内存中的占用情况,也许表面上看好像没有多大用处, ...

随机推荐

  1. Cisco cmd 命令(二)

    1.配置路由器静态路由选择表 ip route [destination_network] [mask] [next_hop_address or exitinterface] [administra ...

  2. 0118——UIButtton

    1.Button的定义 UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; Button有六种类型 enum { UI ...

  3. 写一个Windows上的守护进程(6)Windows服务

    写一个Windows上的守护进程(6)Windows服务 守护进程因为要开机启动,还要高权限,所以我就把它做成Windows服务了. 关于Windows服务的官方文档,大家可以看https://msd ...

  4. (转)最小二乘法拟合圆公式推导及vc实现[r]

    (下文内容为转载,不过已经不清楚原创的是哪里了,特此说明) 转自: http://www.cnblogs.com/dotLive/archive/2006/10/09/524633.html 该网址下 ...

  5. JDK,TomCat安装配置

    JDK.Tomcat.myEclipse安装配置 准备安装包 JAVA运行环境包 JDK1.7下载地址: http://www.veryhuo.com/down/html/43205.html Jsp ...

  6. 使用JavaScript判断图片是否加载完成的三种实现方式

    有时需要获取图片的尺寸,这需要在图片加载完成以后才可以.有三种方式实现,下面一一介绍. 一.load事件 <!DOCTYPE HTML> <html> <head> ...

  7. Yii 安装

    // 安装 composer curl -s http://getcomposer.org/installer | php // 把 composer 添加到全局命令 mv composer.phar ...

  8. arcgis api for silverlight使用google map等多个在线地图

    原文 http://blog.csdn.net/leesmn/article/details/6820245 无可否认,google map实在是很漂亮.可惜对于使用arcgis api for si ...

  9. UESTC_Sliding Window 2015 UESTC Training for Data Structures<Problem K>

    K - Sliding Window Time Limit: 18000/6000MS (Java/Others)     Memory Limit: 131072/131072KB (Java/Ot ...

  10. poj 3728 The merchant(LCA)

    Description There are N cities in a country, and there is one and only one simple path between each ...