C/C++ Language Fundamentals Flashcards

1
Q

What is the difference between malloc and new in C/C++?

A

malloc allocates raw memory in C, doesn’t call constructors, and returns void*. new is C++-specific, allocates memory, calls constructors, and returns typed pointer. malloc requires manual size calculation; new handles it automatically.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a dangling pointer?

A

A pointer that references memory that has been deallocated (e.g., after free or delete), leading to undefined behavior if dereferenced.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Explain the difference between const and constexpr in C++.

A

const ensures a variable’s value doesn’t change after initialization; evaluated at runtime. constexpr ensures compile-time evaluation, used for constants and functions to optimize performance.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the purpose of a virtual function in C++?

A

A virtual function enables runtime polymorphism, allowing derived classes to override base class methods. Declared with virtual keyword; requires vtable for dynamic dispatch.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is RAII in C++?

A

Resource Acquisition Is Initialization: resources (e.g., memory, files) are acquired in a constructor and released in a destructor, ensuring automatic cleanup (e.g., std::unique_ptr).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the difference between a pointer and a reference in C++?

A

A pointer is a variable storing a memory address, can be reassigned, and may be null. A reference is an alias for a variable, cannot be reassigned, and cannot be null.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the volatile keyword used for in C/C++?

A

Tells the compiler not to optimize access to a variable, as its value may change unexpectedly (e.g., hardware registers or multithreaded variables).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly