Re: A question about allocating many std::string and another one about profiling stl memory allocation
luca.regini@gmail.com ha scritto:
Hi,
i have an application that allocates many small classes that contains
a small number of std::string members. This results in a number of
dynamically allocated std::string that is in the order of one milion.
I would like to understand what is the (memory) penalty that i have to
pay for using a std::string and not directly char* buffers. Anyone can
point me in the right direction? Do u think that writing a custom
allocator can improve allocating performance in this case?
Assuming that the cost of allocating memory on the heap is the dominant
factor, I don't see how using char* buffers could have significantly
better performances than std::string, given that you still need to
allocate those char* buffers on the heap anyway. That is unless you know
in advance the maximum size of the string and you can afford the waste
of space of using a million char[N].
On the other hand, several std::string implementations use the so-called
"short string optimization" that avoids the need to a dynamic allocation
for short strings. If in in your usage pattern you expect a significant
amount of very short strings, std::string is going to provide a
significant advantage over char* buffers.
Also, during the creation of the object i use various temporary stl
containers to contain temporary results. I would like to profile this
containers memory consumption but i have not found any reference about
profiling stl memory footprint. Should i write a custom allocator that
computes memory consumption or is there another easier way?
Summarizing the question is about doing a correct memory profiling for
std::string allocation and for other general stl containers ( list and
std::maps ).
The default allocator is required to obtain memory by calling ::operator
new(size_t) and to release memory with ::operator delete(void*), both of
which are "replaceable" functions. So simply provide replacements for
them, for example by implementing them with malloc()/free(), and put any
debug hooks you might need into them.
HTH,
Ganesh
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]