RAII in C++

RAII is one of the patterns in C++ to manage resources.

Sometimes at some part of the program, we need to allocate some memory and later free it.

template <typename T, std::size_t N>
T* allocateMemory()
{
return new T[N];
}

Disaster can happen when

  1. allocateMemory() is not…