Consider this simple example:
#include <algorithm>
#include <iostream>
#include <list>
#include <numeric>
#include <random>
#include <vector>
#include <iterator>
int main()
{
std::list<int> l(10);
std::iota(l.begin(),l.end(),77);
std::vector<std::list<int>::iterator> v(l.size());
std::iota(v.begin(), v.end(), l.begin());
std::vector<int> dest;
std::copy_if(l.begin(), l.end(), std::back_inserter(dest), [](int i){return i%2==1;});
for(auto n : dest)
std::cout << n << " ";
return 0;
}
When run under Valgrind, it gives me the following output:
==27353== total heap usage: 15 allocs, 15 frees, 380 bytes allocated
Is it possible to track exactly where those allocs occured (i.e. which data structure performed allocation and when exactly)?
The correct way to track C++ library allocation calls is to use Massif, a heap profiler tool (part of Valgrind):
Run Valgrind with Massif enabled:
valgrind --tool=massif
Use
ms_print
to analyze output from Massif:ms_print massif.out.12345 (number varies)