interview
cpp-basics
C语言

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_ptrstd::shared_ptr)来自动管理内存的分配和释放。

用途

C`++`和C语言的基础知识是开发高性能、低级系统程序的核心。面试这些内容的目的是为了确保候选人具备理解和操作底层数据结构、内存管理、以及面向对象设计模式的能力。在实际生产环境中,这些知识广泛应用于操作系统开发、嵌入式系统、游戏引擎开发以及任何需要高性能的应用中。指针操作、内存管理、深拷贝和虚函数等概念在处理复杂系统时经常会用到。\n

相关问题

🦆
C++中什么是智能指针?

智能指针是一种自动管理内存的工具,它在指针不再需要时自动释放内存。C++11引入了std::unique_ptrstd::shared_ptrstd::weak_ptr来管理动态内存。智能指针防止了传统指针容易出现的内存泄漏问题。

🦆
在C语言中,如何进行动态内存分配?

C语言使用malloccallocrealloc进行动态内存分配,使用free释放分配的内存。malloc用于分配指定大小的内存块,而calloc则分配并初始化为0realloc可以调整已经分配的内存大小。

🦆
C++中的RAII是什么?

RAII(资源获取即初始化)是一种C++编程惯例,它通过对象的生命周期管理资源。通过在对象构造时分配资源,在析构时释放资源,RAII确保资源的正确释放,防止资源泄漏。智能指针是RAII模式的一个应用。

🦆
C++中的继承与多态的区别是什么?

继承是指一个类(子类)从另一个类(父类)获取属性和行为的过程,多态是指在继承体系中,基类指针或引用可以指向派生类对象,并在运行时调用派生类中重写的函数。继承是一种结构性关系,而多态是一种行为性关系。

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.

用途

These questions are essential to assess a candidate`'s understanding of fundamental concepts in C/C++ that are crucial for writing safe, efficient, and maintainable code. Pointers and memory management, for instance, are core topics in systems programming, where fine-grained control over memory is required. RAII is heavily used in resource management in C++ projects, especially in scenarios where resources like file handles or network connections need to be managed efficiently. Understanding these concepts is critical in environments like embedded systems, game development, high-performance computing, and large-scale systems where C/C++ is often used.`\n

相关问题

🦆
How does the stack differ from the heap in terms of memory allocation in CC++?

The stack is a region of memory where local variables are stored and is managed automatically by the compiler. The heap, on the other hand, is used for dynamic memory allocation, managed manually by the programmer using malloc(), calloc(), and free() in C, or new and delete in C++. Stack memory is faster to allocate and deallocate compared to heap memory.

🦆
What are the dangers of using new and delete in C++?

Improper use of ‘new’ and ‘delete’ can lead to memory leaks, dangling pointers, and double deletions. A memory leak occurs when memory allocated with ‘new’ is not deallocated with ‘delete’. A dangling pointer is a pointer that points to memory that has already been freed. Double deletion can cause undefined behavior, as attempting to delete memory that has already been freed can corrupt the memory management system.

🦆
What is a segmentation fault and how can it be prevented in CC++?

A segmentation fault occurs when a program tries to access a memory location that it is not allowed to access. Common causes include dereferencing null pointers, accessing memory out of bounds, or writing to read-only memory. Prevention strategies include careful pointer management, using tools like static analyzers to catch potential issues, and testing the code rigorously.

🦆
What is the purpose of the const keyword in C++ and how does it affect the code?

The ‘const’ keyword in C++ is used to define variables that cannot be modified after initialization. It can also be used to create constant pointers, constant references, and to specify that a member function does not modify the object. This helps in making the code more robust, as it prevents accidental changes to data that should remain unchanged.