More C/C++ Flashcards

1
Q

What are smart pointers in C++, and why are they used?

A

Smart pointers are objects that act like pointers but also manage the lifetime of the objects they point to, automatically handling memory management. They are used to prevent memory leaks and dangling pointers. Common smart pointers in C++ include std::unique_ptr, std::shared_ptr, and std::weak_ptr.

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

What is the difference between shallow copy and deep copy in C++?

A

A shallow copy copies the values of the members, but if those members are pointers, both objects will point to the same memory location. A deep copy creates new objects for the pointed-to data, ensuring that the two objects are completely independent.

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

How do you handle exceptions in C++?

A

Exceptions in C++ are handled using try-catch blocks. Code that might throw an exception is placed inside a try block, and exceptions are caught using catch blocks that specify the type of exception to handle.

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

What is a process in C++?

A

A process is an instance of a running program, with its own memory space.

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

What is a thread in C++?

A

A thread is a subset of a process and shares the same memory space as other threads in the same process.

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

What are threads used for in C++?

A

Threads are used for concurrent execution within a single program.

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