C/C++中sizeof()计算练习题

1.sizeof()

sizeof() 操作符的结果类型为 size_t,计算的是分配空间的实际字节数;
sizeof() 是运算符,可以以类型、函数做参数;
sizeof() 是在编译的时候就将结果计算出来了,结果是类型所占空间的字节数,所以用数组名做参数计算的是整个数组的大小。

2.strlen()

strlen() 结果类型也是 size_t,但 strlen() 计算的是空间中字符的个数,不包括 ‘\0’;
strlen() 是函数,只能以 char* (字符串)做参数,而且,一定要以 ‘\0’ 结尾;
strlen() 是在运行的时候才开始计算结果。

3.单一类型例子

为什么要内存对齐?

  1. 硬件平台限制,内存以字节为单位,不同硬件平台不一定支持任何内存地址的存取,一般可能以双字节、4字节等为单位存取内存,为了保证处理器正确存取数据,需要进行内存对齐。
  2. 提高CPU内存访问速度,一般处理器的内存存取粒度都是N的整数倍,假如访问N大小的数据,没有进行内存对齐,有可能就需要两次访问才可以读取出数据,而进行内存对齐可以一次性把数据全部读取出来,提高效率。

内存对齐规则?

  1. 数据成员对齐规则:struct或者union的数据成员,第一个数据成员放在offset为0的地方,以后每个数据成员都按照#pragma pack数值和这个数据成员自身大小中更小的那个进行对齐。
  2. 整体对齐规则:struct或者union的首地址按照内部最大数据成员的大小和#pragma pack数值较小的那个N进行对齐,并且结构体的总大小为N的整数倍,如有必要编译器也会在最后一个成员后面填充一些字节用于对齐。
#include <iostream>
#include <cstring>

/*
* Author: 酒馆店小二
* Description: sizeof()练习
* Date: 2022-03-08 20:30:09 星期二
* FileName: SizeofTest.cpp
* Location: D:\VSCODE_CPP\algorithm\mainshi\SizeofTest.cpp
*/

using namespace std;

void fun1(char str[20]) {
  cout << sizeof(str) << endl;
}

void fun2(char a[10][9]) {
  cout << sizeof(a) << endl;
}

int func3() {
  cout << "hello world" << endl;
  return 0;
}

int main(int argc, char* argv[]) {
  int i = 0;
  cout << sizeof(++i) << endl; // 4
  cout << i << endl; // 0, 注意不是1

  cout << sizeof(func3()) << endl; // 4, 注意没有hello world打印,返回类型是 int

  char *p1;
  cout << sizeof(p1) << endl; // 8

  int *p2 = new int[100]; 
  cout << sizeof(p2) << endl; // 8
  delete [] p2;

  float *p3;
  cout << sizeof(p3) << endl; // 8

  double ******p4;
  cout << sizeof(p4) << endl; // 8

  char str[100] = "abcdefg";
  fun1(str); // 8  (fun1中的形参str是指针变量)
  cout << sizeof(str) << endl;  // 100  (str的容量为100)
  cout << sizeof(*str) << endl; // 1    (*str是第一个字符数据)
  cout << strlen(str) << endl;  // 7    (字符串str的长度)

  char str1[] = "abcdeofg"; //  (里面是字符'o')
  cout << sizeof(str1) << endl; // 9
  cout << strlen(str1) << endl; // 8

  char str2[] = "abcde0fg"; //  (里面是字符'0',不等于'\0')
  cout << sizeof(str2) << endl; // 9
  cout << strlen(str2) << endl; // 8

  char str3[] = "abcde\0fg";  
  cout << sizeof(str3) << endl; // 9
  cout << strlen(str3) << endl; // 5

  char str4[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g'};
  cout << sizeof(str4) << endl; // 7
  cout << strlen(str4) << endl; // 12  (数值不确定)

  char str5[] = {'a', 'b', 'c', 'd', 'e', 'o', 'f', 'g'};
  cout << sizeof(str5) << endl; // 8
  cout << strlen(str5) << endl; // 20  (数值不确定)

  char str6[] = {'a', 'b', 'c', 'd', 'e', '0', 'f', 'g'};
  cout << sizeof(str6) << endl; // 8
  cout << strlen(str6) << endl; // 28  (数值不确定)

  char str7[] = {'a', 'b', 'c', 'd', 'e', '\0', 'f', 'g'};
  cout << sizeof(str7) << endl; // 8
  cout << strlen(str7) << endl; // 5

  char str8[] = {'a', 'b', 'c', 'd', 'e', 0, 'f', 'g'};
  cout << sizeof(str8) << endl; // 8
  cout << strlen(str8) << endl; // 5

  char str9[] = "";
  cout << sizeof(str9) << endl; // 1
  cout << strlen(str9) << endl; // 0
  char a[10][9];
  cout << sizeof(a) << endl; // 90
  fun2(a); // 8  (fun2中的a为指针变量)

  return 0;
}

4.复合类型例子

#include <iostream>
#include <cstring>
using namespace std;
/*
* Author: 酒馆店小二
* Description: sizeof()复合类型
* Date: 2022-03-08 20:48:09 星期二
* FileName: SizeofTezt02.cpp
* Location: D:\VSCODE_CPP\algorithm\mainshi\SizeofTezt02.cpp
*/

struct A  // 1,编译器实现
{};

struct B { // 8,内存对齐
    char c;
    int i;
};

struct C { // 8,内存对齐
    int i;
    char c;
};

struct D { // 8,内存对齐
    char c1;
    char c2;
    int i;
};

struct E { // 8,内存对齐
    int i;
    char c1;
    char c2;
};

struct F { // 12,内存对齐
    char c1;
    int i;
    char c2;
};

union G { // 8,内存共用
    char c1;
    int i;
    char c2;
    double d;
};

class H // 1,编译器实现
{};

class I { // 4
private:
    int i;
};

class J { // 8,虚指针
public:
    virtual void display();
};

class K { // 4
private:
    int i;
public:
    void display();
};

class L { // 16,虚指针
private:
    int i;
public:
    virtual void display();
};

class M { // 16,虚指针
private:
    int i;
    int j;
public:
    virtual void display();
};

class N { // 1(静态成员变量不专属某一对象,属于类)
private:
    static int i;
};

class O { // 4(静态成员变量不专属某一对象,属于类)
private:
    static int i;
    int j;
};

int main(int argc, char* argv[]) {
    cout << "A: " << sizeof(A) << endl; // 1  (编译器实现)
    cout << "B: " << sizeof(B) << endl; // 8  (内存对齐)
    cout << "C: " << sizeof(C) << endl; // 8  (内存对齐)
    cout << "D: " << sizeof(D) << endl; // 8  (内存对齐)
    cout << "E: " << sizeof(E) << endl; // 8  (内存对齐)
    cout << "F: " << sizeof(F) << endl; // 12 (内存对齐)

    cout << "G: " << sizeof(G) << endl; // 8  (内存共用)

    cout << "H: " << sizeof(H) << endl; // 1   (编译器实现)
    cout << "I: " << sizeof(I) << endl; // 4   
    cout << "J: " << sizeof(J) << endl; // 8   (虚指针)
    cout << "K: " << sizeof(K) << endl; // 4
    cout << "L: " << sizeof(L) << endl; // 16  (虚指针)  对比一下sizeof(M)
    cout << "M: " << sizeof(M) << endl; // 16  (虚指针)  对比一下sizeof(L)

    cout << "N: " << sizeof(N) << endl; // 1   (静态成员变量不专属某一对象,属于类)
    cout << "O: " << sizeof(O) << endl; // 4   (静态成员变量不专属某一对象,属于类)

    return 0;
}

sizeof() 还是比较绕的。

云想衣裳花想容,春风拂槛露华浓。
修改于:2022.3.8

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值