sizeof定义
关于sizeof运算符,《c++ primer》在139页中是这样说明的:
- 对char或者类型为char的表达式执行sizeof运算,结果得1;
- 对引用类型执行sizeof运算得到被引用对象所占空间的大小;
- 对指针执行sizeof()运算得到指针本身所占空间的大小;
- 对解引用执行sizeof()运算得到指针指向的对象所占空间的大小,指针不需要有效。
在sizeof的运算对象中解引用一个无效指针仍然是一种安全的行为,因为指针实际上并没有
真正使用。sizeof不需要真的解引用也能知道它所指对象的类型。 - 对数组执行sizeof运算得到整个数组所占的空间的大小,等价于对数组中所有的元素
各执行一次sizeof运算并将所得结果求和。因此,值得注意的是,sizeof运算不会把数组转换
成指针来处理。同时,执行sizeof运算能得到整个数组的大小,所以可以用数组的大小除以
单个元素的大小得到数组中元素的个数。 - 对string对象或vector对象执行sizeof运算与计算struct时相同,都是根据自身成员变量来计算,为固定值(不同编译器可能略有不同)
内存对齐
内存对齐适用于类,结构体
第一条:第一个成员的首地址为0
第二条:每个成员的首地址是自身大小的整数倍
第二条补充:以4字节对齐为例,如果自身大小大于4字节,都以4字节整数倍为基准对齐。
第三条:最后以结构总体对齐。
第三条补充:以4字节对齐为例,取结构体中最大成员类型倍数,如果超过4字节,都以4字节整数倍为基准对齐。(其中这一条还有个名字叫:“补齐”,补齐的目的就是多个结构变量挨着摆放的时候也满足对齐的要求。)
代码详解
#include<iostream>
#include<string>
#include<vector>
#include<map>
using namespace std;
void func1(char a[]) {
cout << "形参数组:" << sizeof(a) << endl;
}
//内存对齐只计算非静态成员变量
class C1 {
public:
char a;
static char b;
void* p;
static int* c;
virtual void func2() {
int a;
};
};//12
int main() {
cout << "int: " << sizeof(int) << endl;//4
cout << "double: " << sizeof(double) << endl;//8
cout << "char: " << sizeof(char) << endl;//1
cout << "size_t: " << sizeof(unsigned int) << endl;//4
cout << "long int: " << sizeof(long int) << endl;//4
cout << "long long int: " << sizeof(long long int) << endl;//8
const char* str1 = "abcdef";
cout << "str1: " << sizeof(str1) << " " << sizeof(*str1) << " " << strlen(str1) << endl;//4 1 6
char str2[] = "abcdef";
cout << "str2: " << sizeof(str2) << " " << sizeof(*str2) << " " << strlen(str2) << endl;//7 1 6
char str3[10] = "abcdef";
cout << "str3: " << sizeof(str3) << " " << sizeof(*str3) << " " << strlen(str3) << endl;//10 1 6
func1(str2);//4
func1(str3);//4
const char* b = "helloworld";
char* c[10];
double* d;
int** e;
void (*pf)();
cout << "char *b " << sizeof(b) << endl;//指针指向字符串,值为4
cout << "char *b " << sizeof(*b) << endl; //指针指向字符,值为1
cout << "double *d " << sizeof(d) << endl;//指针,值为4
cout << "double *d " << sizeof(*d) << endl;//指针指向浮点数,值为8
cout << "int **e " << sizeof(e) << endl;//指针指向指针,值为4
cout << "char *c[10] " << sizeof(c) << endl;//指针数组,值为40
cout << "void (*pf)() " << sizeof(pf) << endl;//函数指针,值为4
struct stu1 {
char c;
int i;
};
cout << "stu1: " << sizeof(stu1) << endl;//8
/*
#pragma pack(1)
struct stu2 {
char c;
int i;
};
cout << "stu2: " <<sizeof(stu2) << endl; //5
*/
struct stu3 {
char str[10];
int i;
char c;
};
cout << "stu3: " << sizeof(stu3) << endl;//20
struct str {
int len;
char buf[];
};
cout << sizeof(str) << endl;4
//结构体里面的空数组不占用内存空间(Redis使用C语言设计一种精巧的类似于vector的可变长度数组)
union u
{
int a;
float b;
double c;
char d;
};
cout << "union: " << sizeof(u) << endl; //8
cout << "class C1: " << sizeof(C1) << endl;
vector<int> v;
vector<string> vs;
cout << "vector: " << sizeof(v) << " " << sizeof(vs) << endl;
string s;
cout << "string: " << sizeof(s) << endl;
map<int, int> m;
cout << "map: " << sizeof(m) << endl;
return 0;
}