More C/C++ Flashcards
What are smart pointers in C++, and why are they used?
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.
What is the difference between shallow copy and deep copy in C++?
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 do you handle exceptions in C++?
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.
What is a process in C++?
A process is an instance of a running program, with its own memory space.
What is a thread in C++?
A thread is a subset of a process and shares the same memory space as other threads in the same process.
What are threads used for in C++?
Threads are used for concurrent execution within a single program.