Apparently there is a “malloc_allocator” provided with gcc for use with STL. It simply wraps malloc and free. There is also a hook for an out-of-memory handler. Where can I find more about it? Where can I find its header file? I’m using gcc 4.x.
A:
The out-of-memory handler in c++ is defined via set_new_handler
, which might be in the <new>
header, but I'm not sure.
Marcus Lindblom
2010-02-07 17:05:06
A:
Is this something you want? You will need to include and pass in an object as the STL object's allocator template parameter.
dirkgently
2010-02-07 17:07:31
Yes! This is what I want. But why is <new> included? And where can I find this file on my system - or - where did you find this file?
2010-02-08 04:11:20
@prasoon99: A google search brought this up. `<new>` is a system header. I am tempted to believe that this is present on your system already, try to look for the file in `include\ext`.
dirkgently
2010-02-08 08:06:25
My question was WHY is <new> included in the file. I guess it is there because std::bad_alloc() is used.
2010-02-08 10:27:44
Next question: if I allocate:std::vector<T, malloc_allocator<T> > *v = new std::vector<T, malloc_allocator<T> > (5)Then what is the correct way of deleting v?
2010-02-08 10:31:46
`delete v;` should do it. Note the implementation will take care of calling the appropriate `malloc_allocator` based de-allocation function via the `vector`'s dtor.
dirkgently
2010-02-08 10:46:38