ceorron
ceorron

Reputation: 1268

Does customizing "::operator new" require special allocators for std containers?

I have written a memory allocator to solve a problem with the speed of the default one in C++ (mingw).

To do this I've overloaded global new and delete. All the requests i'm getting through are being correctly allocated with memory of the correct sizes but i'm still getting segmentation faults. These faults seem to be about the use of vectors.

I assume these vectors would be covered by the global override of new and delete but I maybe wrong. So what i'm asking is would I need to have an stl allocator that works off of my memory allocator or is it likely some other problem using global new and delete that I have overlooked?

Upvotes: 4

Views: 345

Answers (2)

jogojapan
jogojapan

Reputation: 70037

I assume these vectors would be covered by the global override of new and delete but I maybe wrong. So what i'm asking is would I need to have an stl allocator that works off of my memory allocator or is it likely some other problem using global new and delete that I have overlooked?

The C++ Standard (here from C++11) says about std::allocator (the default allocator used for standard containers):

(§20.6.9/1) Remark: the storage is obtained by calling ::operator new(std::size_t) (18.6.1), but it is unspecified when or how often this function is called. [...]

So the fact that you didn't implement a special allocator class for use with the vectors is not a problem. The problem is probably caused by something wrong about your definition of the global operator itself, or perhaps about the way you use the vectors. Recommended next steps are:

  • Check whether the code works when ::operator new is not replaced
  • Use a debugger to obtain a stack trace of the crash and analyse the stack at that point to obtain clues on what functions are involved in the crash, what the values of local variables at that point is, etc.

Upvotes: 5

Karthik T
Karthik T

Reputation: 31972

I believe STL Allocators would need to be implemented for you to control vectors allocations

Upvotes: 0

Related Questions