Skip to content

Clear or Empty Array

C

Syntax

c
free(arrayName);
  • free() is the function to deallocate the memory allocated for a dynamically allocated array using malloc() or calloc(). It releases the memory to prevent memory leaks

C++

Syntax

c++
delete[] arrayName;
  • delete[] ensures that memory is properly deallocated, preventing memory leaks for arrays allocated with new[].

Java

Syntax

java
arrayName.clear();
  • clear() method removes all elements from the ArrayList, allowing the garbage collector to reclaim memory associated with those elements.

Python

Syntax

python
array_name.clear()
  • clear() is a method that is more straightforward for clearing the elements of a dynamic array-like structure in Python.