Reputation:
I've overloaded the global operator new/delete/new[]/delete[] but simple tests show that while my versions of new and delete are being called correctly, doing simple array allocations and deletes with new[] and delete[] causes the implementations in newaop.cpp and delete2.cpp to be called.
For example, this code
int* a = new int[10];
calls operator new[] in newaop.cpp, which in turn calls my version of operator new. So it seems they are globally overloaded but for some reason not the array versions. Is there something I'm missing?
EDIT: My implementation of the operators are in a separate project which is compiled into a library and linked to statically. In retrospect, this might have been useful to include in the original post, as it probably has something to do with this. Although I still can't figure out why only the array versions are affected.
Upvotes: 3
Views: 866
Reputation:
Ok, I managed to crack this so am posting in case anyone else stumbles upon this.
The reason for the operator not being called was because my implementation was located in a library, not in the project which called the operators. In fact, since technically you only need to include an implementation of the operators, they are already globally defined, I only specified the implementation of the operators in a .cpp in my library (this was the wrong step). The code obviously only included the header files from the library and didn't ahve visibility to the implementations. Moreover, Visual Studio seems to have linked newaop.cpp and delete2.cpp into my application. These two files contain implementations for operator new[] and operator delete[] (bot not for regular new/delete!). This is most likely the reason why the compiler saw these two implementations and chose them over mine, which resided in a .cpp file in a library.
The solution to this was to move the implementation of my overloaded operators to a header file in the library which is directly included from my code.
Upvotes: 0
Reputation: 137800
You told us your code is buggy but didn't post any of it :vP . My guess would be that you used int
as the argument of the overload rather than size_t
. Some overloads might get called because the compiler decided to be tolerant in that instance.
Upvotes: 0
Reputation: 70204
I don't know how you overloaded operator new[]
but I just tried it with MSVC2008:
void* operator new[](size_t size)
{
return 0;
}
int main()
{
int* a = new int[5];
}
The code above effectively calls my faulty implementation of operator new[]
.
So here is my guess: you failed at overloading operator new[]
for some reason and your program uses the compiler's version of operator new[]
which relies on operator new
to allocate the memory. Since you overloaded operator new
, your implementation gets called.
Upvotes: 3
Reputation: 62323
operator new allocates one object and calls its constructor. new[] allocates n objects and calls n constructors.
edit: Having multiple new[] and delete[] overloads could be considered a bit of and odd one. How does the compiler know which one to link too? Any chance you could post your overloads? Also do yours get called if you don't link newaop and delete2 in (ie yours are the only implementations in the exe)?
Upvotes: 0