演算子 new[] を次のようにオーバーライドしました
void* operator new[](std::size_t sz, const char *file, int line)
{
void* mem = malloc(sz);
if(mem == 0){
printf("Could not allocate the desired memory, the new operator fails\n");
std::abort();
}
printf("Allocation has been done!\n");
printf("Allocation has been done! In %s, line #%i, %p[%i]\n", file, line, mem, sz);
return mem;
}
#define DEBUG_NEW2 new[](__FILE__, __LINE__)
#define new[] DEBUG_NEW2
私のプログラムは主にこのタイプの new 演算子を使用しているため、私はそれについてもっと心配しています。ただし、コンパイラは「マクロ名 [-Werror] の後に空白がありません」というエラー メッセージを表示します。「#define new[] DEBUG_NEW2」で遊んでみました。場合によっては問題なくコンパイルできますが、その場合は new[] がオーバーライドされません。