WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

C++ · Intermediate · question 30 of 100

Explain the concept of RAII (Resource Acquisition Is Initialization) in C++ and its importance.?

📕 Buy this interview preparation book: 100 C++ questions & answers — PDF + EPUB for $5

RAII stands for Resource Acquisition Is Initialization. It is a programming technique in C++ that involves using constructors and destructors to manage resources automatically, without the need for explicit memory management. The idea behind RAII is that when an object is created, it acquires the resources it needs to function properly. When the object is destroyed, the resources are automatically released.

The most common use of RAII is for managing dynamically allocated memory, such as memory allocated with new or malloc. By encapsulating the memory allocation and deallocation inside the constructor and destructor of a class, we can ensure that the memory is properly allocated and deallocated, even in the presence of exceptions or early returns.

Here’s an example of using RAII to manage dynamically allocated memory:

    #include <iostream>
    #include <cstring>
    using namespace std;
    
    class String {
        public:
        String(const char* str) {
            m_size = strlen(str);
            m_str = new char[m_size + 1];
            strcpy(m_str, str);
        }
        ~String() {
            delete[] m_str;
        }
        const char* getStr() const {
            return m_str;
        }
        private:
        char* m_str;
        size_t m_size;
    };
    
    int main() {
        String str("hello");
        cout << str.getStr() << endl;
        return 0;
    }

In this example, we define a String class that encapsulates a dynamically allocated C-style string. The constructor allocates memory for the string, and copies the contents of the input string into it. The destructor releases the memory when the object is destroyed. We then create a String object in the main function, passing in the string "hello", and print the string to the console.

By using the String class, we ensure that the memory for the string is properly managed, without the need for explicit memory allocation or deallocation. This helps to prevent memory leaks, memory corruption, and other memory-related bugs that can be difficult to debug.

In addition to managing memory, RAII can also be used to manage other types of resources, such as file handles, database connections, and locks. The key idea is to encapsulate the acquisition and release of resources inside a class, and let the constructor and destructor handle the details of managing the resources automatically. This can lead to more robust and maintainable code, with fewer bugs and memory leaks.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic C++ interview — then scores it.
📞 Practice C++ — free 15 min
📕 Buy this interview preparation book: 100 C++ questions & answers — PDF + EPUB for $5

All 100 C++ questions · All topics