简体   繁体   中英

C++ / Windows: Replacing global new works, new[] doesn't

I just started playing around with overriding global new/delete (purely for educational purposes) and was having an issue.

I'm using a fairly mature project of mine to test with several k loc. First thing I did was override new and new[] to simply use malloc and increment a static counter. new seems to work fine but I noticed that my new gets called by the crt version of new[] from newaop.cpp which looks like so:

void *__CRTDECL operator new[](size_t count) _THROW1(std::bad_alloc)
{   // try to allocate count bytes for an array
    return (operator new(count));
}

My version is

void* operator new[](size_t count)
{
    // malloc and whatnot
}

The new[] is called by some stl functions. My versions are in a .h/.cpp file which is the first include in every other file in the project.

I didn't have much time to try and figure out why this morning, but I would love it if someone could explain why my version is not being called.

Edit: build is debug/x86, I don't remember the stl container (not at home) but probably map .

Just do something like: char* p = new char[100]; delete[] p; char* p = new char[100]; delete[] p; and put a breakpoint and see what happens.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM