Knowee
Questions
Features
Study Tools

Which of the following correctly deallocates memory allocated for an array in C++?delete ptr;delete[] ptr;free(ptr);delete array ptr;

Question

Which of the following correctly deallocates memory allocated for an array in C++?

  • delete ptr;
  • delete[] ptr;
  • free(ptr);
  • delete array ptr;
🧐 Not the exact question you are looking for?Go ask a question

Solution

The correct way to deallocate memory allocated for an array in C++ is by using delete[] ptr;.

Here's why:

  1. delete ptr; - This is used to delete memory allocated for a single object, not an array. If used on an array, it will only delete the first element, leading to a memory leak.

  2. delete[] ptr; - This is the correct way to delete memory allocated for an array in C++. It deletes the entire array and deallocates the memory.

  3. free(ptr); - This is a C language function, not C++. While it can deallocate memory, it doesn't call the destructor for the object, which can lead to issues in C++.

  4. delete array ptr; - This is not a valid syntax in C++. The correct syntax to delete an array is delete[] ptr;.

This problem has been solved

Similar Questions

How is memory allocated for an array dynamically in C++?int* arr = new int[10];int arr = new int[10];int arr[10] = new int;int* arr = alloc int[10];

What function is used to deallocate memory that was previously allocated by malloc?

blank is used to allocate memory to array variable in Javamalloc calloc new alloc

Where is an array stored in memory?a) heap spaceb) stack spacec) heap space and stack spaced) first generation memory

The statement char arr [10]; is an example of a type of ____________ memory allocation.

1/1

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.