Написал пограммку:
#include <iostream>
#include <string>
#include <map>
int main(){
std::string str("12356789012356789012345678901234567890");
std::map<std::string, int> m;
m.insert(std::pair<std::string, int> (str, 1));
}
запустил под valgrind-ом:
$ valgrind --leak-check=yes ./a.out
==12585== Memcheck, a memory error detector
==12585== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==12585== Using Valgrind-3.8.0.SVN and LibVEX; rerun with -h for copyright info
==12585== Command: ./a.out
==12585==
==12585==
==12585== HEAP SUMMARY:
==12585== in use at exit: 0 bytes in 0 blocks
==12610== total heap usage: 2 allocs, 2 frees, 111 bytes allocated
==12585==
==12585== All heap blocks were freed -- no leaks are possible
==12585==
==12585== For counts of detected and suppressed errors, rerun with: -v
==12585== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)
Подумал, а что если поменять std::map<std::string, int> на std::map<std::string*, int, Comp>, ведь поидее должен быть профит по памяти на длиннных строках.
#include <iostream>
#include <string>
#include <map>
struct Comp {
bool operator() (std::string* lhs, std::string* rhs) const
{return *lhs < *rhs;}
};
int main(){
std::string str("12356789012356789012345678901234567890");
std::map<std::string*, int, Comp> m;
m.insert(std::pair<std::string*, int> (&str, 1));
}
Запустил под valgrind-ом:
$ valgrind --leak-check=yes ./a.out
==12563== Memcheck, a memory error detector
==12563== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==12563== Using Valgrind-3.8.0.SVN and LibVEX; rerun with -h for copyright info
==12563== Command: ./a.out
==12563==
==12563==
==12563== HEAP SUMMARY:
==12563== in use at exit: 0 bytes in 0 blocks
==12633== total heap usage: 2 allocs, 2 frees, 111 bytes allocated
==12563==
==12563== All heap blocks were freed -- no leaks are possible
==12563==
==12563== For counts of detected and suppressed errors, rerun with: -v
==12563== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)
Но как видно всё то же: "111 bytes allocated".
Вот и возник вопрос, а почему?