EDIT: Following the answer of Jorge Y., I created the following program. It is not optimal since (a) it works only on Linux, (b) it requires me to keep a terminal window open besides the program console to track the memory. But, at least it is good for demonstration purposes.
#include <iostream>
#include <memory>
#include <vector>
#include <thread>
#include <chrono>
using namespace std;
#define USE_UNIQUE_PTR
constexpr int SIZE=1000*1000*1000;
struct Large {
char x[SIZE];
};
int main() {
cout << "Before braces" << endl;
this_thread::sleep_for(chrono::milliseconds(5000));
// On Linux, run: cat /proc/meminfo |grep MemFree
// Available memory is X
{
#ifdef USE_UNIQUE_PTR
auto p = make_unique<Large>();
#else
auto p = new Large();
#endif
cout << "Inside braces" << endl;
p->x[0] = 5;
cout << p->x[0] << endl;
this_thread::sleep_for(chrono::milliseconds(5000));
// On Linux, run: cat /proc/meminfo |grep MemFree
// Available memory should be X-SIZE
}
cout << "Outside braces" << endl;
this_thread::sleep_for(chrono::milliseconds(5000));
// On Linux, run: cat /proc/meminfo |grep MemFree
// Available memory should be X if USE_UNIQUE_PTR is defined, X-SIZE if it is undefined.
}