[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”)= 0
,sizeof ("\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 字节的,这样利用该类型可以增强程序的可移植性。
相关文章
- c++介绍与入门基础(详细总结)
- 【C++游戏引擎Easy2D】基于基础类型学习使用核心Game+Window+Input详解
- 【C++保姆级入门】一维数组基础详解+习题巩固
- C++基础语法----多态
- 【C++】c 基础
- C/C++基础:Visual Studio(基于Windows的C++)【比CMake(基于Linux系统)高级的编译器(Level-3)】
- C++基础语法(三):类和对象【struct和class的区别:struct默认访问权限为public;class默认访问权限为private】【用new创建的对象,要用delete销毁】
- C++程序员Protocol Buffers基础指南
- 转: 用 Eclipse 平台进行 C/C++ 开发
- C++ 资源大全中文版
- C++学习笔记_05 string 2021-04-16
- C++ OpenCV基础视频教程---第04讲(摄像头、视频读取_写入)
- C++ OpenCV基础视频教程---第01讲(OpenCV介绍与安装配置)
- 数据结构(C++版)——栈的应用,利用栈的先进后出判断一个包含“(“和“)“ “[“和“]“ “<“和“>“ “{“和“}“的括号序列是否匹配
- 求2-n的所有素数c++
- (三)C++中的内存管理
- C++随机数生成方法(转载,赶紧搜藏)
- 《挑战30天C++入门极限》新手入门:C/C++中数组和指针类型的关系
- [C++] 提升程序效率的小方法
- vc++基础班[22]---文件的基本操作2
- vc++基础班[26]---进程的相关操作
- C++: std::string 与 Unicode 如何结合?
- C++11 多线程
- c++的各种类型转换方式
- 【牛客刷题】带你在牛客刷题第六弹(C/C++基础)
- 【牛客刷题】带你在牛客刷题第三弹(C/C++语言基础)
- [C++基础] 面向对象、C 与 C++ 区别篇
- [C++基础] 数组、指针、内存篇