运算符 new 和运算符 new[] 之间的区别?

发布于 2024-08-18 07:58:13 字数 409 浏览 5 评论 0原文

我已经重载了全局运算符new/delete/new[]/delete[],但简单的测试表明,虽然我的new和delete版本被正确调用,但使用new[]和delete[]进行简单的数组分配和删除会导致要调用的newaop.cpp和delete2.cpp中的实现。

例如,此代码

int* a = new int[10];

调用 newaop.cpp 中的operator new[],而newaop.cpp 又调用我的operator new 版本。所以看起来它们是全局重载的,但由于某种原因不是数组版本。我有什么遗漏的吗?

编辑:我对运算符的实现位于一个单独的项目中,该项目被编译到库中并静态链接。回想起来,这可能有助于包含在原始帖子中,因为它可能与此有关。虽然我仍然不明白为什么只有数组版本受到影响。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

暮光沉寂 2024-08-25 07:58:13

我不知道您是如何重载 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,因此您的实现将被调用。

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.

偏闹i 2024-08-25 07:58:13

new运算符分配一个对象并调用其构造函数。 new[] 分配 n 个对象并调用 n 个构造函数。

编辑:拥有多个 new[] 和 delete[] 重载可以被认为是有点奇怪。编译器如何知道要链接哪一个呢?您有机会发布您的超载吗?如果您不链接 newaop 和 delete2 (即您的是 exe 中唯一的实现),您的也会被调用吗?

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)?

复古式 2024-08-25 07:58:13

您告诉我们您的代码有错误,但没有发布任何内容:vP。我的猜测是您使用 int 作为重载的参数,而不是 size_t。某些重载可能会被调用,因为编译器决定在该实例中容忍。

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.

樱娆 2024-08-25 07:58:13

好吧,我设法解决了这个问题,所以我发帖以防其他人偶然发现这个问题。

操作员未被调用的原因是因为我的实现位于库中,而不是在调用操作员的项目中。事实上,由于从技术上讲,您只需要包含运算符的实现,它们已经是全局定义的,因此我只在我的库中的 .cpp 中指定了运算符的实现(这是错误的步骤)。代码显然只包含库中的头文件,并且对实现没有可见性。此外,Visual Studio似乎已将newaop.cpp和delete2.cpp链接到我的应用程序中。这两个文件包含运算符 new[] 和运算符 delete[] 的实现(bot 不适用于常规的 new/delete!)。这很可能就是编译器看到这两个实现并选择它们而不是我的实现的原因,我的实现位于库中的 .cpp 文件中。

解决方案是将重载运算符的实现移动到库中的头文件中,该头文件直接包含在我的代码中。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文