Allocation and deallocation of memory blocks should always be performed using matching functions. This means that when we allocate using C-style functions, we deallocate with the matching function from the same API. The same is true for C++-style allocation and deallocation.
Briefly, this means the following:
- If we allocate using malloc, calloc, valloc, realloc, or memalign, we deallocate with free
- If we allocate with new, we deallocate with delete
- If we allocate with new[], we deallocate with delete[]
Mixing these up won't necessarily cause problems, but doing so is undefined behavior. The latter type of allocating and deallocating is specific to arrays. Not using delete[] for an array that was allocated with new[] likely leads to a memory leak, or worse.