C++ 基础面试题, C语言
C++ 基础面试题, C语言
QA
Step 1
Q:: 什么是指针,如何在C++
中使用指针?
A:: 指针是一个变量,它存储另一个变量的内存地址。可以通过&
符号获取变量的地址,并通过*
符号来访问指针指向的变量值。例子:int a = 5; int *p = &a;
。这里,p
是一个指向a
的指针,*p
会返回变量a
的值,即5
。
Step 2
Q:: C++
中如何实现深拷贝?
A:: 深拷贝指的是在拷贝对象时,不仅仅是拷贝对象的指针,而是复制指针指向的实际数据。实现深拷贝时,通常需要定义拷贝构造函数和赋值运算符,以确保在复制对象时为其分配独立的内存,并复制数据。例子:class MyClass { MyClass(const MyClass &other); MyClass& operator=(const MyClass &other); };
Step 3
Q:: C++
中的虚函数是什么?
A:: 虚函数是用来实现多态性的一种机制。当一个基类中的函数被声明为虚函数后,可以在派生类中对其进行重写。在运行时,调用该函数时,会根据对象的实际类型来调用相应的函数。例子:class Base { virtual void show() { cout << "Base"; } }; class Derived : public Base { void show() override { cout << "Derived"; } };
Step 4
Q:: C++
中如何避免内存泄漏?
A:: 内存泄漏是指程序在分配内存后没有正确释放它,导致内存无法再被使用。为了避免内存泄漏,应该确保每一个new
操作符都对应一个delete
,并尽量使用智能指针(如std::unique_ptr
和std::shared_ptr
)来自动管理内存的分配和释放。
用途
C`++`和C语言的基础知识是开发高性能、低级系统程序的核心。面试这些内容的目的是为了确保候选人具备理解和操作底层数据结构、内存管理、以及面向对象设计模式的能力。在实际生产环境中,这些知识广泛应用于操作系统开发、嵌入式系统、游戏引擎开发以及任何需要高性能的应用中。指针操作、内存管理、深拷贝和虚函数等概念在处理复杂系统时经常会用到。\n相关问题
C++基础面试题, C语言
QA
Step 1
Q:: What is the difference between a pointer and a reference in C++?
A:: In C++, a pointer is a variable that holds the memory address of another variable, while a reference is an alias for another variable. Pointers can be reassigned and can be null, whereas references must be initialized when they are created and cannot be reassigned or be null. Pointers support pointer arithmetic, but references do not.
Step 2
Q:: Explain the concept of RAII (Resource Acquisition Is Initialization) in C++ and provide an example.
A:: RAII is a programming idiom used in C++ where resource acquisition (like memory, file handles, etc.) is tied to the lifespan of objects. When an object is created, it acquires certain resources, and when the object goes out of scope, the destructor is called to release those resources automatically. Example: std::unique_ptr in C++ manages dynamic memory, and when the std::unique_ptr goes out of scope, the memory is automatically deallocated.
Step 3
Q:: What is a memory leak in C and how can it be avoided?
A:: A memory leak in C occurs when dynamically allocated memory using malloc() or calloc() is not properly deallocated using free(), leading to a reduction in available memory for the application. Memory leaks can be avoided by ensuring that every malloc() or calloc() is matched with a free(), and by using tools like Valgrind to detect leaks.
Step 4
Q:: What are the differences between malloc() and calloc() in C?
A:: malloc() allocates a block of memory of a specified size but does not initialize the memory. calloc(), on the other hand, allocates memory for an array of elements and initializes all bytes to zero. The function signatures are: void* malloc(size_t size); and void* calloc(size_t nitems, size_t size);
Step 5
Q:: Explain the concept of ‘undefined behavior’ in C/C++. Provide an example.
A:: Undefined behavior in C/C++ occurs when the code executes an operation that has unpredictable results according to the language standards. Examples include dereferencing a null pointer, accessing an array out of bounds, or modifying a variable multiple times between sequence points. The result of undefined behavior can be anything from a crash to seemingly correct behavior, making it very dangerous in production code.