views:

181

answers:

2

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
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
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?
@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
My question was WHY is <new> included in the file. I guess it is there because std::bad_alloc() is used.
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?
`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