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 26 of 100

How do you handle dynamic memory allocation in C++? Provide examples using ’new’ and ’delete’.?

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

Dynamic memory allocation in C++ allows us to allocate memory at runtime, rather than at compile time. This can be useful for situations where we don’t know the exact size of the memory we need until the program is running, or when we need to allocate memory on the heap instead of the stack.

In C++, we can use the ’new’ operator to allocate memory dynamically, and the ’delete’ operator to free the memory when we’re done with it. Here’s an example of dynamic memory allocation using ’new’ and ’delete’:

    #include <iostream>
    using namespace std;
    
    int main() {
        int* arr = new int[5]; // allocate an array of 5 integers on the heap
        for (int i = 0; i < 5; i++) {
            arr[i] = i; // initialize the array
        }
        for (int i = 0; i < 5; i++) {
            cout << arr[i] << " "; // print the array
        }
        cout << endl;
        delete[] arr; // free the memory
        return 0;
    }

In this example, we allocate an array of 5 integers on the heap using the ’new’ operator. We then initialize the array with the values 0, 1, 2, 3, and 4 using a for loop, and print the array using another for loop.

When we’re done with the array, we use the ’delete’ operator to free the memory. Note that since we allocated an array using ’new’, we need to use ’delete[]’ to free the memory, rather than ’delete’. This is because arrays are allocated as a contiguous block of memory, and ’delete[]’ ensures that the entire block is freed.

Here’s another example of dynamic memory allocation using ’new’ and ’delete’ for a single object:

    #include <iostream>
    using namespace std;
    
    class Person {
        public:
        Person(const char* name) {
            this->name = new char[strlen(name) + 1];
            strcpy(this->name, name);
        }
        ~Person() {
            delete[] name;
        }
        void printName() {
            cout << name << endl;
        }
        private:
        char* name;
    };
    
    int main() {
        Person* p = new Person("John"); // allocate a Person object on the heap
        p->printName(); // call a function on the object
        delete p; // free the memory
        return 0;
    }

In this example, we define a class called Person that represents a person with a name. The class has a single data member, name, which is a dynamically allocated C-style string. We define a constructor to initialize the name, a destructor to clean up the name when the object is destroyed, and a printName() function to print the name.

In the main() function, we allocate a Person object on the heap using the ’new’ operator, and initialize it with the name "John". We then call the printName() function on the object, and free the memory using the ’delete’ operator when we’re done with the object.

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