一文搞定sizeof笔试面试题

本文详细解析了 C++ 中 sizeof 运算符的使用方法及其在不同数据类型和复合类型上的表现。通过具体实例展示了 sizeof 如何确定变量或类型的大小,并解释了内存对齐和内存共用的概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转自 https://blog.youkuaiyun.com/stpeace/article/details/113838908

1.sizeof(基本类型)

#include<iostream>
#include<cstring>
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()
{
  cout << sizeof(int) << endl;  // 4

  cout << sizeof(long) << endl; // 8

  int i = 0;
  cout << sizeof(++i) << endl; // 4
  cout << i << endl; // 0, 注意不是1哦

  cout << sizeof(func3()) << endl; // 4, 注意没有hello world打印哈

  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; // 7  (数值不确定)

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

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

  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;
}

2.sizeof(复合类型)

#include<iostream>
#include<cstring>
using namespace std;

struct A  
{};

struct B
{
  char c;
  int i;
};

struct C
{
  int i;
  char c;
};

struct D
{
  char c1;
  char c2;
  int i;
};

struct E
{
  int i;
  char c1;
  char c2;
};

struct F
{
  char c1;
  int i;
  char c2;
};

union G
{
  char c1;
  int i;
  char c2;
  double d;
};

class H
{};

class I
{
private:
  int i;
};

class J
{
public:
  virtual void display();
};

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

class L
{
private:
  int i;
public:
  virtual void display();
};

class M
{
private:
  int i;
  int j;
public:
  virtual void display();
};

class N
{
private:
  static int i;
};

class O
{
private:
  static int i;
  int j;
};

int main()
{
  cout << sizeof(A) << endl; // 1  (编译器实现)
  cout << sizeof(B) << endl; // 8  (内存对齐)
  cout << sizeof(C) << endl; // 8  (内存对齐)
  cout << sizeof(D) << endl; // 8  (内存对齐)
  cout << sizeof(E) << endl; // 8  (内存对齐)
  cout << sizeof(F) << endl; // 12 (内存对齐)

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

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

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

  return 0;
}

### 关于 UniApp 框架推荐资源与教程 #### 1. **Uniapp 官方文档** 官方文档是最权威的学习资料之一,涵盖了从基础概念到高级特性的全方位讲解。对于初学者来说,这是了解 UniApp 架构技术细节的最佳起点[^3]。 #### 2. **《Uniapp 从入门到精通:案例分析与最佳实践》** 该文章提供了系统的知识体系,帮助开发者掌握 Uniapp 的基础知识、实际应用以及开发过程中的最佳实践方法。它不仅适合新手快速上手,也能够为有经验的开发者提供深入的技术指导[^1]。 #### 3. **ThorUI-uniapp 开源项目教程** 这是一个专注于 UI 组件库设计实现的教学材料,基于 ThorUI 提供了一系列实用的功能模块。通过学习此开源项目的具体实现方式,可以更好地理解如何高效构建美观且一致的应用界面[^2]。 #### 4. **跨平台开发利器:UniApp 全面解析与实践指南** 这篇文章按照章节形式详细阐述了 UniApp 的各个方面,包括但不限于其工作原理、技术栈介绍、开发环境配置等内容,并附带丰富的实例演示来辅助说明理论知识点。 以下是几个重要的主题摘选: - **核心特性解析**:解释了跨端运行机制、底层架构组成及其主要功能特点。 - **开发实践指南**:给出了具体的页面编写样例代码,展示了不同设备间 API 调用的方法论。 - **性能优化建议**:针对启动时间缩短、图形绘制效率提升等方面提出了可行策略。 ```javascript // 示例代码片段展示条件编译语法 export default { methods: { showPlatform() { console.log(process.env.UNI_PLATFORM); // 输出当前平台名称 #ifdef APP-PLUS console.log('Running on App'); #endif #ifdef H5 console.log('Running on Web'); #endif } } } ``` #### 5. **其他补充资源** 除了上述提到的内容外,还有许多在线课程视频可供选择,比如 Bilibili 上的一些免费系列讲座;另外 GitHub GitCode 平台上也有不少优质的社区贡献作品值得借鉴研究。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值