All Exams Test series for 1 year @ ₹349 only
Question

Which of the following expression will delete the entire array pointed to by q?

The correct answer is

delete[ ] q;

When dealing with dynamic memory allocation in languages like C++, it's crucial to deallocate the memory correctly to prevent memory leaks. The way you deallocate memory depends on how it was allocated.

If you allocate memory for a single object using new, you deallocate it using delete.

int* single_int = new int; // Allocate memory for one integer
// Use single_int
delete single_int; // Deallocate memory for one integer

If you allocate memory for an array of objects using new[], you must deallocate it using delete[].

int* int_array = new int[10]; // Allocate memory for an array of 10 integers
// Use int_array
delete[] int_array; // Deallocate memory for the array

The question asks how to delete an entire array pointed to by q. This implies the array was allocated using new[]. Therefore, the correct way to deallocate this memory is by using the delete[] operator followed by the pointer.

Analyzing the Array Deletion Options

Let's examine the provided options for deleting an entire array pointed to by q:

  1. delete all q; - This syntax is not valid in C++ for memory deallocation. The delete operator does not work with an all keyword.
  2. delete array q; - This syntax is also not valid in C++ for memory deallocation. The delete operator does not use an array keyword.
  3. delete * q; - This syntax would attempt to deallocate the memory *pointed to by* the first element of the array (if q points to the beginning of the array). Using delete on an array allocated with new[] is undefined behavior and will likely lead to errors, memory corruption, or crashes, especially if the array contains objects with destructors. It only deallocates the memory for the first element, not the entire block allocated for the array.
  4. delete[ ] q; - This is the correct syntax in C++ for deallocating memory that was allocated using new[]. The square brackets [] signal to the runtime system that it needs to call the destructor for each element in the array (if the elements are objects) and then deallocate the entire block of memory allocated for the array.

Correct Method for Array Deallocation

To delete an entire array pointed to by a pointer like q (assuming it was created using new[]), the standard C++ syntax is delete[] q;. This ensures that the correct deallocation process is followed for the array, including calling destructors for each element if necessary and releasing the entire memory block back to the system.

Memory Allocation and Deallocation Summary
Allocation Deallocation Purpose
new Type; delete pointer; Allocate/deallocate memory for a single object.
new Type[size]; delete[] pointer; Allocate/deallocate memory for an array of objects.

Revision Table: C++ Memory Management

Let's quickly review key concepts related to C++ dynamic memory management:

  • Dynamic Memory: Memory allocated during program execution using new or new[].
  • new Operator: Allocates memory for a single object and returns a pointer to it.
  • new[] Operator: Allocates memory for an array of objects and returns a pointer to the first element.
  • delete Operator: Deallocates memory for a single object allocated with new.
  • delete[] Operator: Deallocates memory for an array of objects allocated with new[].
  • Memory Leak: Occurs when dynamically allocated memory is no longer accessible but has not been deallocated, wasting system resources.
  • Dangling Pointer: A pointer that points to a memory location that has been deallocated. Accessing a dangling pointer results in undefined behavior.

Additional Information: Why delete[] is Crucial for Arrays

The primary reason delete[] is necessary for arrays allocated with new[] (especially for arrays of objects) is related to destructors. When you allocate an array of objects, each object might have a destructor that needs to be called to clean up resources specific to that object. The delete[] operator knows the size of the array and iterates through each element, calling its destructor before deallocating the memory block. Using plain delete on memory allocated with new[] only calls the destructor for the first element (if any) and attempts to deallocate memory for just one object, leading to potential resource leaks (if other objects' destructors aren't called) and memory corruption upon deallocation. For primitive types like int or float, where elements don't have destructors, using delete instead of delete[] might *sometimes* appear to work on certain systems but is still technically undefined behavior according to the C++ standard and should always be avoided.

Was this answer helpful?

Important Questions from Array

  1. Which of the following initialization statement store six integer values in array?

  2. Consider the following C program segment.

    #include <stdio.h>

    int main()
    {
      char s1[7] = "1234";
        char *p;

        p = s1 + 2;
        *p = '0';

        printf("%s", s1);

        return 0;
    }

    What will be printed by the program?
  3. #include <stdio.h>
    
    void foo(int *p, int x){
        *p = x;
    }
    
    int main(){
        int *z;
        int a = 20, b = 25;
        z = &a;
        foo(z, b);
        printf("%d", a);
        return 0;
    }
    
    The output of the given C program is __________. (Answer in integer)
  4. Consider an array $A$ of integers of size $n$. The indices of $A$ run from 1 to $n$. An algorithm is to be designed to check whether $A$ satisfies the condition given below.

    $\forall i,j \in \{1, \dots, n-1\}$ such that $i > j$, $(A[i + 1] - A[i]) > (A[j + 1] - A[j])$

    Which one of the following gives the worst case time complexity of the fastest algorithm that can be designed for the problem?
  5. What is the output the given C language code snippet? 

    #include<stdio.h>

     int main()

    {

     int x[] = {10,20,30}; 

    int *p = x; 

    p++; 

    printf("%d",*p); 

    return 0; 

    }

Need Expert Advice?

Start Your Preparation with Prepp Mobile App

Download the app from Google Play & App Store
Download the app from Google Play & App Store
Prepp Mobile App