The first example results in a memory leak -
Try this to see the leak in a debug build -
#ifdef _DEBUG
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#define _ATL_DISABLE_NOTHROW_NEW
#ifndef DBG_NEW
#define DBG_NEW new ( _NORMAL_BLOCK , __FILE__ , __LINE__ )
//#define new DBG_NEW
#endif
#else
#include <stdlib.h>
#endif // _DEBUG
int main()
{
constexpr int size{ 50 }, n{ 10 };
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
int** container = DBG_NEW int* [n];
for (int i = 0; i < n; ++i)
container[i] = DBG_NEW int[size];
// ... and to deallocate...
// Uncomment to fix memory leak
//
//for (int i = 0; i < n; ++i)
// delete[] container[i];
delete[] container;
}
Output pane leak report after program terminates -