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...
List the errors in the following array declarations. A. int intArray[] = new double[10]; B. int...
List the errors in the following array declarations. A. int intArray[] = new double[10]; B. int intArray[] = new int[1.5]; C. double[] doubleArray = new double[-10] D. int intMatrix[] [] = new int[10]; - Write a method selectRandom that expects an array of integers as a parameter. The method should return the value of an array element at a randomly selected position.
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...
C++ 5a)We have the following class: class Banana { private:      string s;      int y;...
C++ 5a)We have the following class: class Banana { private:      string s;      int y; public: bool theMemberFunc (string); void setterForS(string); // setter for s string getterForS(); // getter for s }; Instantiate a static object of Banana named co. a)int Banana; b)co.Banana = new; c)Banana = new class; d)Banana co; 5b)Code a call to function aNonclassFunction passing co. a)aNonclassFunction (co); b)aNonclassFunction (* co); c)aNonclassFunction (aGoodClass.co); d)aNonclassFunction (aGoodClass); 5c)Code the function definition for aNonclassFunction, picking up co. aNonclassFunction has...
Part 1: Determine what is wrong (if any) with the following statements int A[4] = {...
Part 1: Determine what is wrong (if any) with the following statements int A[4] = { 1, 2, 3, 4, 5}; int A[4] = {1,2}; int A[4] = [1,2,3,4]; Part 2: Create one dimensional array and save the following integers in it:  2, 3, 6, 7, 10, 12. Display the values on the screen. (for example, a[0] = 2, a[1] = 3, etc.) Part 3: Write a program to read 5 integers from the user. Save these integers in an array...
Answer question for C# 1 of 10 When items are added or removed, a list resizes...
Answer question for C# 1 of 10 When items are added or removed, a list resizes itself. True False Question 2 of 10 What happens when you assign one list to another using the assignment operator? An error message generates. Elements copy to new memory locations. Both lists become value-type data elements. Both lists reference the same memory locations. Question 3 of 10 Which of the following is a valid example of calling a method and sending it an array...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT