{"id":13467592,"url":"https://github.com/SYaoJun/CPPInterview","last_synced_at":"2025-03-26T03:30:41.751Z","repository":{"id":37048301,"uuid":"246863933","full_name":"SYaoJun/CPPInterview","owner":"SYaoJun","description":"C/C++高频面试题","archived":false,"fork":false,"pushed_at":"2025-03-22T05:20:59.000Z","size":138225,"stargazers_count":485,"open_issues_count":0,"forks_count":60,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-03-22T06:23:05.239Z","etag":null,"topics":["c","cpp","linux"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/SYaoJun.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-03-12T15:06:34.000Z","updated_at":"2025-03-22T05:21:02.000Z","dependencies_parsed_at":"2024-01-13T18:03:32.444Z","dependency_job_id":"f0b52e8e-45fa-4233-9263-afde45f06ba1","html_url":"https://github.com/SYaoJun/CPPInterview","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SYaoJun%2FCPPInterview","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SYaoJun%2FCPPInterview/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SYaoJun%2FCPPInterview/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SYaoJun%2FCPPInterview/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SYaoJun","download_url":"https://codeload.github.com/SYaoJun/CPPInterview/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245584441,"owners_count":20639549,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["c","cpp","linux"],"created_at":"2024-07-31T15:00:58.283Z","updated_at":"2025-03-26T03:30:41.738Z","avatar_url":"https://github.com/SYaoJun.png","language":null,"readme":"\u003cp align='center'\u003e\n\u003ca href=\"https://github.com/syaojun/cppinterview\" target=\"_blank\"\u003e\u003cimg alt=\"GitHub\" src=\"https://img.shields.io/github/stars/syaojun/cppinterview?label=Stars\u0026style=flat-square\u0026logo=GitHub\"\u003e\u003c/a\u003e\n\u003ca href=\"https://mp.weixin.qq.com/s/YeyAgD52zCadtrdXLxrT9A\" target=\"_blank\"\u003e\u003cimg src=\"https://img.shields.io/badge/%E5%85%AC%E4%BC%97%E5%8F%B7-@%E6%88%91%E4%B8%8D%E6%98%AF%E5%8C%A0%E4%BA%BA-000000.svg?style=flat-square\u0026logo=WeChat\"\u003e\n\u003ca href=\"https://www.zhihu.com/people/wan-yi-er-89\" target=\"_blank\"\u003e\u003cimg src=\"https://img.shields.io/badge/%E7%9F%A5%E4%B9%8E-@姚军-000000.svg?style=flat-square\u0026logo=Zhihu\"\u003e\u003c/a\u003e\n\n\u003c/p\u003e\n\n# C/C++基础面试问题\n## 类中成员函数有两个`void hello()`和`void hello() const`，怎么在调用是区分调的哪一个？\n根据创建的实例对象而决定，如果实例对象是const则自动调第二个，如果非const调用第一个。\n\n## 析构函数是否可以重载？析构函数是否可以是虚函数？如果不是虚函数会产生什么问题？举个例子\n析构函数不可以重载。\n析构函数不重载，可能导致父类资源无法释放。\n构造函数的调用顺序是，先构造父类，再构造子类。析构函数的顺序是反过来的，先析构子类，再析构父类。\n```c++\n#include \u003ciostream\u003e\n\nclass Base {\npublic:\n    Base() {\n        std::cout \u003c\u003c \"Base constructor called.\" \u003c\u003c std::endl;\n    }\n\n    // 注意：这里没有将析构函数声明为虚函数\n    ~Base() {\n        std::cout \u003c\u003c \"Base destructor called.\" \u003c\u003c std::endl;\n    }\n};\n\nclass Derived : public Base {\npublic:\n    Derived() {\n        std::cout \u003c\u003c \"Derived constructor called.\" \u003c\u003c std::endl;\n    }\n\n    ~Derived() {\n        std::cout \u003c\u003c \"Derived destructor called.\" \u003c\u003c std::endl;\n    }\n};\n\nint main() {\n    Base* ptr = new Derived();  // 创建一个Derived对象，并用Base指针指向它\n    delete ptr;  // 通过基类指针删除对象\n\n    return 0;\n}\n// 如果析构不是虚函数，那么只会释放父类的资源，而泄露了子类的资源\n```\n\n## 什么场景需要用dynamic_cast？\n如果想要将父类转换为子类，需要父类中至少有一个虚函数，dynmic_cast依赖于运行时类型信息（RTTI），因此需要虚函数的存在。\n```c++\n\n## #include \u003ciostream\u003e\n#include \u003cexception\u003e\n\nclass Base {\npublic:\n    virtual ~Base() {}  // 基类需要至少一个虚函数（通常是析构函数），以支持RTTI\n};\n\nclass Derived1 : public Base {\npublic:\n    void derived1Function() {\n        std::cout \u003c\u003c \"Derived1 function called.\" \u003c\u003c std::endl;\n    }\n};\n\nclass Derived2 : public Base {\npublic:\n    void derived2Function() {\n        std::cout \u003c\u003c \"Derived2 function called.\" \u003c\u003c std::endl;\n    }\n};\n\nint main() {\n    Base* basePtr1 = new Derived1();\n    Base* basePtr2 = new Derived2();\n\n    // 尝试将 basePtr1 转换为 Derived1*\n    Derived1* derivedPtr1 = dynamic_cast\u003cDerived1*\u003e(basePtr1);\n    if (derivedPtr1) {\n        derivedPtr1-\u003ederived1Function();\n    } else {\n        std::cout \u003c\u003c \"Conversion to Derived1 failed.\" \u003c\u003c std::endl;\n    }\n\n    // 尝试将 basePtr2 转换为 Derived1*（应该失败）\n    Derived1* derivedPtr2 = dynamic_cast\u003cDerived1*\u003e(basePtr2);\n    if (derivedPtr2) {\n        derivedPtr2-\u003ederived1Function();\n    } else {\n        std::cout \u003c\u003c \"Conversion to Derived1 failed (as expected).\" \u003c\u003c std::endl;\n    }\n\n    // 清理内存\n    delete basePtr1;\n    delete basePtr2;\n\n    return 0;\n}\n```\n\n## 什么是柔性数组？有什么用处？\n\n## 什么是大小端？如何用代码判断大小端？\n\n## 如何定位内存泄露问题？\n\n## 如何使用perf分析程序性能？\n\n## C/C++内存模型\n\n## 什么是内存对齐？为什么内存要对齐？\n\n## 提高C/C++程序性能的技巧？\n\n## C/C++互相调用通常在头文件中做什么处理？\n\n## static在类成员变量和类成员函数中有什么作用？\n静态成员函数只能访问静态成员变量。静态成员变量在所有对象中共享。\n## 如何实现类的单例模式？\n\n## 如何判断链表是否有环？如何找到链表的中点？如何对链表排序？如何对二叉树做层序遍历？给定四个坐标如何判断是否是正方形？\n\n## 如何提高TLB缓存命中率？\n\n## 什么时候需要用虚函数？\n\n## 什么时候需要用智能指针？\n想要malloc和new时，不用显式分配内存和管理内存，就可以使用智能指针。\n\n## 移动语义与完美转发\n\n## C++20协程\n\n## 源文件生成可执行文件的过程\n\n## 内存管理？如何避免内存碎片？\n\n## 线程上下文切换会做哪些工作？\n\n## 虚拟地址空间到物理地址空间的流程说一下\n\n## 如何减小锁的粒度？如何避免死锁？\n\n## 如何保证程序是线程安全的？\n\n## 如何实现一个线程安全的延迟队列？\n\n## 如何实现原子操作CAS\n\n## 你技术上最大的优势是什么？你最擅长什么技术？C/C++功能实现，算法与数据结构，linux内核，操作系统，还是协作沟通，英语等？","funding_links":[],"categories":["Others"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FSYaoJun%2FCPPInterview","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FSYaoJun%2FCPPInterview","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FSYaoJun%2FCPPInterview/lists"}