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

C++ · Basic · question 19 of 100

What is the purpose of the ’new’ and ’delete’ operators in C++?

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

In C++, the ’new’ and ’delete’ operators are used for dynamic memory allocation and deallocation, respectively. Dynamic memory allocation allows programs to allocate memory during runtime, unlike static memory allocation, which allocates memory at compile time.

The ’new’ operator is used to allocate memory dynamically on the heap. It returns a pointer to the newly allocated memory, which can be assigned to a pointer variable of the appropriate type. For example:

    int* ptr = new int; // allocate memory for an integer on the heap
    *ptr = 5; // assign a value to the integer

In this example, the ’new’ operator is used to allocate memory for an integer on the heap. The pointer variable ’ptr’ is assigned the address of the newly allocated memory. The value of the integer can then be assigned using the dereference operator ’*’.

The ’delete’ operator is used to deallocate memory that was previously allocated with the ’new’ operator. It takes a pointer to the memory to be deallocated as an argument. For example:

    int* ptr = new int; // allocate memory for an integer on the heap
    *ptr = 5; // assign a value to the integer
    delete ptr; // deallocate the memory

In this example, the ’new’ operator is used to allocate memory for an integer on the heap. The pointer variable ’ptr’ is assigned the address of the newly allocated memory. The value of the integer is assigned using the dereference operator ’*’. The memory is deallocated using the ’delete’ operator.

The ’new’ and ’delete’ operators can also be used to allocate and deallocate arrays of objects. For example:

    int* arr = new int[10]; // allocate an array of 10 integers on the heap
    arr[0] = 5; // assign a value to the first element of the array
    delete[] arr; // deallocate the memory for the array

In this example, the ’new’ operator is used to allocate an array of 10 integers on the heap. The first element of the array is assigned a value using array indexing. The memory for the array is deallocated using the ’delete[]’ operator.

It is important to note that memory allocated with the ’new’ operator must always be deallocated with the ’delete’ operator, and memory allocated with the ’new[]’ operator must always be deallocated with the ’delete[]’ operator. Failure to do so can result in memory leaks and other memory-related issues.

In summary, the ’new’ and ’delete’ operators in C++ are used for dynamic memory allocation and deallocation, respectively. They allow programs to allocate memory during runtime and are important for creating flexible and efficient programs.

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