Question

Assuming an integer array is dynamically allocated: int *array = new int[10]; What is the correct...

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;

Homework Answers

Answer #1

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.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
In the attached FlexArray Java class, implement a method public int delete (int location) { }...
In the attached FlexArray Java class, implement a method public int delete (int location) { } that deletes the integer value stored at location in the array, returns it, and ensures that the array values are contiguous.  Make sure to handle the array empty situation.  What is the time-complexity of the method, if the array size is n. ***************************************************************************************************************************** public class FlexArray { int [] array; private int size; private int capacity; public FlexArray() { capacity=10; size=0; array=new int[10]; } public FlexArray(int...
Declare an integer array of size 10 with values initialized as follows. int intArray[] = {1,...
Declare an integer array of size 10 with values initialized as follows. int intArray[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; Compute each item of a new array of same size derived from the above array by: adding first item (intArray[0]) of the array with 3rd, 2nd with 4th, 3rd with 5th and so on. For the last-but-one item intArray[8], add it with first item and for the last item (intArray[9]) add it with 2nd item...
10. Number Array Class Design a class that has an array of floating-point numbers. The constructor...
10. Number Array Class Design a class that has an array of floating-point numbers. The constructor should accept an integer argument and dynamically allocate the array to hold that many numbers. The private data members of the class should include the integer argument in a variable to hold the size of the array and a pointer to float type to hold the address of the first element in the array. The destructor should free the memory held by the array....
Write a function that accepts an int array arr, the array’s size sz and integer n,...
Write a function that accepts an int array arr, the array’s size sz and integer n, the function should create a new array that it’s n times the array arr. The fucntion copy the content of arr to the new array n times. The Example arr = {5, 2, 1} n=3 Newarr = {5, 2, 1, 5, 2, 1, 5, 2, 1} (c++)
C++ for (double i=0; i<100000000; i++){ int *p = new int[100000000]; delete p; } Which of...
C++ for (double i=0; i<100000000; i++){ int *p = new int[100000000]; delete p; } Which of the following statement(s) is/are correct?   There is a run-time error There is a syntax error There is a memory leak There is no error
Write a PHP code that: 1- Creates an array that holds 10 random integer numbers between...
Write a PHP code that: 1- Creates an array that holds 10 random integer numbers between 1 and 100. 2- Moves all multiple of 3-numbers in the array that created in part-a into a new array. 3- Moves all multiple of 5-numbers in the array that created in part-a into a new array. 4- Find the maximum and the minimum multiple of 3-numbers, if exist. 5- Find the maximum and the minimum multiple of 5-numbers, if exist. 6- Prints the...
Write a template-based class that implements a template-based implementation of Homework 3 that allows for any...
Write a template-based class that implements a template-based implementation of Homework 3 that allows for any type dynamic arrays (replace string by the template in all instances below). • The class should have: – A private member variable called dynamicArray that references a dynamic array of type string. – A private member variable called size that holds the number of entries in the array. – A default constructor that sets the dynamic array to NULL and sets size to 0....
Given the array-based list (20, 30, 40, 50), what will the new array look like after...
Given the array-based list (20, 30, 40, 50), what will the new array look like after the following operations? ArrayListPrepend(list, 10) ArrayListAppend(list, 25) ArrayListRemoveAt(list, 3) ArrayListPrepend(list, 40) ArrayListRemoveAt(list, 3) Show your work on your trace file.
One way to represent a very large integer (one that won't fit into a variable of...
One way to represent a very large integer (one that won't fit into a variable of type short, int, or even long) is to use an array. The array is of type int, so each element in the array can hold an integer -- we will store just one digit of our number per array element. So if a user entered 2375, it might be stored as -------------------------- | 2 | 3 | 7 | 5 | ... | --------------------------...
I'm generating a random number every 10 second and Storing and Updating the number every 10...
I'm generating a random number every 10 second and Storing and Updating the number every 10 second when I generate new number in UpdateInt method. However, In my main method when I called UpdateInt method it generate every 10 second new number but it doesn't update and store in the arraylist. ***I want the Runnable to be in the arraylist method. Not in main method. my goal is to send the arraylist that has the update number*** import java.util.ArrayList; import...