Assuming an integer array is dynamically allocated:
int *array = new int[10];
What is the correct way of deallocating this array to not cause any memory leaks?
delete array[10];
delete array;
delete [10] array;
delete *array;
delete [] array;
The below statement is correct in deallocating this array to not cause any memory leaks.
delete [] array;
Explanation:
delete array[10];
The first statement is incorrect because this statement has a syntax error.
delete array;
The second statement is incorrect because this statement will not delete the complete memory allocated to the array.
delete [10] array;
The third statement is incorrect because this statement has a syntax error.
delete *array;
The fourth statement is incorrect because this statement has a syntax error.
delete [] array;
The fifth statement is incorrect because this statement will delete the complete memory allocated to the array.
Get Answers For Free
Most questions answered within 1 hours.