What is the main purpose of RAII in modern C++?

Options

  • A. To manually increase memory allocation
  • B. To tie resource management to object lifetime
  • C. To prevent the use of classes
  • D. More than one of the above
  • E. None of the above

Correct Answer (Detailed Explanation is Below)

B. To tie resource management to object lifetime

Detailed Explanation

Answer: To tie resource management to object lifetime

RAII stands for Resource Acquisition Is Initialization.

The basic idea is that a resource is acquired when an object is created and released automatically when that object is destroyed.

Resources can include memory, files, locks and other system resources. RAII is one of the important techniques used by modern C++ to make resource management safer.