C++面经

本文深入探讨了C++中的placement new与normal new的区别,虚拟函数在实现运行时多态中的作用,以及编译器如何通过vtable和vptr实现运行时解析。同时,解释了内联函数的优化效果以及智能指针在内存管理中的应用。接着,对比了B-Tree和B+Tree的数据结构特点,尤其是它们如何减少磁盘访问。最后,分析了TCP与UDP在网络通信中的不同特性,如连接建立、数据可靠性和传输速度。

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

1. placement new

normal new

  1. allocates memory
  2. constructs an object in allocated memory

placement new

  1. seperate above two things
  2. we can pass a pre-allocated memory and construct an object in the passed memory
  3. new allocates memory in heap, whereas placement new can be done at known address

2. virtual function

  1. cannot be static
  2. used to achieve runtime polymorphism
  3. there is virtual destructor but there is no virtual constructor

how does the compiler perform runtime resolution?

  1. vtable: a table of function pointers, maintained per class
  2. vptr: a pointer to vtable, maintained per object instance

3. inline function

  1. the compiler places a copy of inline function at each point where it is called
  2. function call overhead does not occur 不会产生函数调用开销
  3. 只有当编译器知道对象的确切类型时虚调函数才能被内联

4. smart pointer

  1. Java和C#的Garbage Collection Mechanisms
  2. a smart pointer is a wrapper class over a pointer with an operator like * and -> overloaded

5. b-tree, b+ tree

  1. the main idea of using B-Trees is to reduce the number of disk accesses
  2. most of the tree operations require O(h) disk accesses where h is the height of the tree
  3. B-tree is a fat tree, the height of B-Trees is kept low by putting maximum possible keys in a B-Tree node
  4. B+ tree: all keys are at leaf nodes

6. udp and tcp

tcp

  1. the communicating devices should establish a connection before transmitting data and should close the connection after transmitting the data
  2. guarantees the delivery of data
  3. flow control and acknowledgment of data
  4. packets arrive in order
  5. slower

udp

  1. there is no overhead for opening a connection, maintaining a connection, and terminating a connection
  2. no guarantees the delivery of data
  3. only checksum
  4. no sequencing of data
  5. faster
### C++ 试中的常见问题 #### 基类的析构函数为何要声明为虚函数? 当基类指针指向派生类对象并删除该指针时,如果基类的析构函数不是虚函数,则只会调用基类的析构函数而不会调用派生类的析构函数。这可能导致资源泄漏或其他未定义行为。因此,为了确保所有层次结构中的析构函数都能被正确调用,应该将基类的析构函数声明为虚函数[^1]。 #### C++ 的异常处理机制有何优点? C++ 的异常处理允许程序在遇到错误条件时不立即终止执行流程而是抛出异常给更高层来决定如何处理这些异常状况。这种分离使得底层代码专注于业务逻辑而不是复杂的错误检测与恢复工作;同时让高层能够集中管理不同类型的异常情形,提高了代码清晰度和维护性[^2]。 #### 指针和引用的区别是什么? - **生命周期**:指针可以为空(null),也可以重新赋值指向另一个地址;而引用一旦初始化就必须关联到某个有效变量,并且不能再改变所引用的对象。 - **操作符支持**:对于指针来说存在解引用(`*`)以及取址(&)的操作符号;但是引用则不需要额外的操作符来进行访问因为它本身就是作为目标实体的一个别名。 - **内存开销**:通常情况下两者占用相同的字节数(取决于平台),不过由于编译器优化等原因实际表现可能会有所差异[^3]。 #### `new` 和 `delete` 如何实现?它们与 `malloc` 及 `free` 有什么异同? - **相同点**:都是用于分配/释放动态存储空间的方法,在堆区创建对象实例或者数组。 - **不同之处**: - `new/delete` 是运算符而非库函数形式; - 使用 `new` 创建对象会自动调用相应的构造函数完成初始化过程,同样销毁时也会触发对应的析构函数清理资源; - 对象类型安全检查更为严格——即通过 `new T[]` 形式的表达式申请多维数组时能保留维度信息供后续使用; - 支持自定义配置参数如对齐方式等高级特性. #### 结构体(struct) 和 类(class) 主要有哪几个方的差别? 主要体现在默认访问权限上: - Struct 默认成员是 public 访问级别,默认继承也是公有的(public); - Class 则相反,默认私有(private), 私密程度较高一些; 除此之外二者语法几乎完全一致,都可以拥有数据成员、方法成员(含静态成员)、友元声明等等. ```cpp struct Point { int x; // Public by default for structs. }; class Rectangle { private: float width_; // Private by default for classes. public: void setWidth(float w){width_ = w;} }; ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值