Heap allocation with valgrind

  allocation, c++, memory, valgrind
const size_t size = 10000000;
using T = unsigned short[];
vector<unique_ptr<T>> v;
v.resize(size);
for (size_t n = 0; n != size ; ++n) {
  v[n] = make_unique<T>(3);
  for (int i = 0; i!= 3; ++i)
    v[n][i] = rand();
}

I want to measure how much memory it uses.

  1. What I expect: 10.000.000*(8+2*3) = 140.000.000 bytes. 8 bytes per pointer and 2 bytes for each unsigned short.
  2. What "valgrind — –tool=memcheck" returns: total heap usage: 10,000,112 allocs, 10,000,111 frees, 140,161,828 bytes allocated
  3. What it actually is: VIRT = 419044, RES = 394180 (KB).

Why the actual size is 3 times bigger than valgrind displays?
I run it on VSL, ubuntu.

Source: Windows Questions C++

LEAVE A COMMENT