I am trying to understand the intricacies of memory management in C++, in order to teach it to my students. Since the best way to learn is by trying, I would like to try several code snippets and see how they affect the memory usage. For example, I would like to try the following in order to understand how unique pointers work:
#include <memory>
using namespace std;
int main()
{
{
// print memory - should be X
auto i = make_unique<int>();
// print memory - should be X-4
}
// print memory - should be X
}
The comments I wrote are based on my current understanding, which of course might be wrong; I want to check whether I understood correctly. My question is: what can I write instead of "print memory"?
I found several apparently similar questions, such as: How to determine CPU and memory consumption from inside a process? and C++: Measuring memory usage from within the program, Windows and Linux . However, the answers there are very complicated and platform-dependent. My need is much simpler: I do not need the absolute memory consumption of my program (I.e, I do not need X to be accurate). All I need is a relative measurement that will show me how my actions affect the memory consumption. For this need, is there a simpler solution?