Difference between new operator and new [] operator?

I overloaded the global operator new / delete / new [] / delete [], but simple tests show that although my versions new and delete are called correctly, they perform simple array allocations and are deleted using new [] and deleting [] calls in newaop.cpp and delete2.cpp.

For example, this code

int* a = new int[10];

calls the new [] operator in newaop.cpp, which in turn calls my version of the new operator. So it looks like they are overloaded globally, but for some reason are not versions of the array. Is something missing?

EDIT: My statement implementation is in a separate project, which is compiled into a library and linked to a static one. Looking back, it might be useful to include in the original post, as this probably has something to do with it. Although I still can not understand why only the versions of arrays are affected.

+3
source share
4 answers

Well, I managed to crack it, so I'm sending messages if anyone else stumbles on this.

, , , , , . , , , .cpp ( ). , , . , Visual Studio, , newaop.cpp delete2.cpp . new [], delete [] (bot new/delete!). , , , .cpp .

, .

0

, operator new[], MSVC2008:

void* operator new[](size_t size)
{
  return 0;
}

int main()
{
  int* a = new int[5];
}

operator new[].

, : - operator new[], operator new[], operator new . operator new, .

+3

new . new [] n n .

edit: [] [] . , ? ? , newaop delete2 in (.. exe)?

0

, , : vP. , int , size_t. , .

0

Source: https://habr.com/ru/post/1728834/


All Articles