What is a memory leak in a C++ program?

Options

  • A. Using memory after it has been released
  • B. Allocated memory that is no longer accessible but has not been released
  • C. Accessing a constant variable
  • D. More than one of the above
  • E. None of the above

Correct Answer (Detailed Explanation is Below)

B. Allocated memory that is no longer accessible but has not been released

Detailed Explanation

Answer: Allocated memory that is no longer accessible but has not been released

A memory leak occurs when dynamically allocated memory is not released after it is no longer needed.

For example:

int *p = new int(10);

If the program loses the pointer p without executing delete p;, the allocated memory may remain occupied until the program terminates.

Repeated memory leaks can increase memory usage and may eventually affect program performance.