zl程序教程

您现在的位置是:首页 >  后端

当前栏目

[C++基础] sizeof篇

C++基础 sizeof
2023-09-27 14:29:14 时间

sizeof 篇

1 32 位和 64 位编译器的区别

测试代码如下:

#include <iostream>
using namespace std;

int main()
{
	// 32和64位编译器区别: 除了*与long随操作系统子长变化而变化外,其他的都固定不变(32位和64相比)
	// 32: sizeof(*)=4 sizeof(long)=4
	// 64: sizeof(*)=8 sizeof(long)=8

	// 测试编译器VS2013为32位编译器
	cout << "bool size = " << sizeof(bool) << endl; // bool size = 1
	cout << "char size = " << sizeof(char) << endl; // char size = 1
	cout << "wchar_t size = " << sizeof(wchar_t) << endl; // short size = 2
	cout << "short size = " << sizeof(short) << endl; // short size = 2
	cout << "int size = " << sizeof(int) << endl;   // int size = 4
	cout << "long size = " << sizeof(long) << endl; // long size = 4
	cout << "long long size = " << sizeof(long long) << endl;   // long long size = 8
	cout << "char*=" << sizeof(char*) << " int*=" << sizeof(int*) << endl; // char*=4 int*=4
	cout << "float size = " << sizeof(float) << endl; // float size = 4
	cout << "double size = " << sizeof(double) << endl; // double size = 8
	cout << "long double size = " << sizeof(long double) << endl;   // long double size = 8

	return 0;
}

再看一个例子:

struct A
{
   unsigned int a;
   char b[2];
   double c;
   short d;
}

printf("%d\n", sizeof(A)); // 32位编译器为20字节,若是64位编译器则为24字节

32 位编译器(计算机)的字长为 32 位,所以遵循 4 字节对齐;对应的,64 位编译器的字长为 64 位,所以遵循 8 字节对齐。


2 sizeof 字符数组

测试代码如下:

#include <iostream>
using namespace std;

void printSize(char aInFunc[])
{
	printf("sizeof(aInFunc) = %lu\n", sizeof(aInFunc));
}

int main()
{
	char str[5] = { 0 };
	char str1[] = "hello";
	char str2[5] = { '0' };
	char str3[5] = { '0', '0', '0' };

	// str为含有5个元素的数组(后面会自动填充空格' '),数组名代表首元素的地址,所以sizeof(str)代表整个数组所占的内存空间
	// 容易误认为传的是地址,判断为4,但实际上你传的是数组名,数组名不等价于地址
	printf("sizeof(str) = %lu\n", sizeof(str));  // sizeof(str) = 5
	printf("sizeof(str2) = %lu\n", sizeof(str2));  // sizeof(str2) = 5
	printf("sizeof(str3) = %lu\n", sizeof(str3));  // sizeof(str3) = 5

	// *str为首元素地址的内容,即是首元素str[0],字符类型
	printf("sizeof(*str) = %lu\n", sizeof(*str));  // sizeof(*str) = 1

	// 容易认为是5,但"hello"是字符串,最后一个是'\0'
	printf("sizeof(hello) = %lu\n", sizeof("hello"));  // sizeof(hello) = 6

	// str1是数组,保存的是字符串,所以长度为6
	printf("sizeof(str1) = %lu\n", sizeof(str1));  // sizeof(str1) = 6

	// 在数组作为参数传入的时候实际上就退化为普通的指针了
	printSize(str); // sizeof(aInFunc) = 4

	return 0;
}

3 strlen 字符数组

#include <iostream>
using namespace std;

int main()
{
	char str[5] = { 0 };
	char str1[] = "hello";
	char str2[5] = { '0' };
	char str3[5] = { '0', '0', '0' };

	// strlen函数 需要注意的是,这个函数返回的结果不包含\0
	// ASCII码中: 十进制的0 - 字符空格' ' - 转义字符'\0' - 代码NULL ,所以strlen(str) == 0
	printf("strlen(str) = %lu\n", strlen(str));  // strlen(str) = 0
	printf("strlen(str1) = %lu\n", strlen(str1));  // strlen(str1) = 5
	printf("strlen(str2) = %lu\n", strlen(str2));  // strlen(str2) = 1
	printf("strlen(str3) = %lu\n", strlen(str3));  // strlen(str3) = 3
	printf("strlen(hello) = %lu\n", strlen("hello"));  // strlen(hello) = 5

	return 0;
}

4 sizeof 数组指针/指针数组

#include <iostream>
using namespace std;

int main()
{
	// VS为32位编译器
	char **str1[4]; // str是指针数组,数组包含4个指向指针的指针(**)
	char *(*str2)[4]; // str是数组指针,指向包含4个指针的char*数组
	char(**str3)[4]; // str是数组指针,指向包含4个char的char数组

	printf("strlen(str) = %lu\n", sizeof(str1));  // strlen(str) = 16
	printf("strlen(str1) = %lu\n", sizeof(str2));  // strlen(str1) = 4
	printf("strlen(str2) = %lu\n", sizeof(str3));  // strlen(str2) = 4

	return 0;
}

5 其它笔试题

1. strlen("\0")=? sizeof("\0")=?

strlen("\0”)= 0sizeof ("\0") = 2

注意 "\0" 是一个字符串,所以 "\0" 本质上是一个字符 '\0' 加上一个结束符 '\0',有2个字节。


再看下面的示例程序:

#include <stdio.h>
#include <string.h>

int main()
{
	char * parr = new char[10]; 
	printf("%d\n", strlen(parr)); // 14
	printf("%d\n", sizeof(parr)); // 4
	printf("%d\n", sizeof(*parr)); // 1
	
	return 0;
}

在上例中,程序定义了一个字符指针 parr,它指向一个分配了 10 个空间的字符数组,由于没有进行初始化,根据 strlen 的计算原理,所以不能够确定 strlen(parr) 的值,因为无法确定字符串的终止位置,所以该值为一个随机值,本例中输出为14。


2. size_t的含义?

size_t 是标准 C 库中定义的,应为 unsigned int,在 64 位系统中为 long unsigned int。

在 C++ 中,设计 size_t 就是为了适应多个平台的 。size_t 的引入增强了程序在不同平台上的可移植性。size_t 是针对系统定制的一种数据类型,一般是整型。经测试发现,在 32 位系统中 size_t 是 4 字节的,而在 64 位系统中,size_t 是 8 字节的,这样利用该类型可以增强程序的可移植性。