C++ 中的放置新运算符是什么? java有吗?

发布于 2024-12-04 17:42:16 字数 177 浏览 0 评论 0原文

我听说过 C++ 的放置新运算符。我很困惑它是什么。不过,我可以在 stackoverflow 中的一个问题下看到它可以在哪里使用。我也很困惑java中是否有这个。 所以我的问题非常精确:什么是放置新运算符,我们在java中有类似的东西吗?

请注意,不要与 stackoverflow 上的其他问题混淆:它们与此问题不重复。

I heard about placement new operator of C++. I am confused what it is. However, I can see where it can be used under a question in stackoverflow. I am also confused whether we have this in java or not.
So my question is very precise: What is placement new operator and do we have something like it in java?

Note please, don't be confused with other questions on stackoverflow: they are not duplicate of this question.

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

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

发布评论

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

评论(3

一绘本一梦想 2024-12-11 17:42:16

以下文章解释了 C++ 中放置 new 的含义: http://www.glenmccl.com/nd_cmp.htm

这个术语本身与重载的new 语句相关。由于 Java 根本不允许重载运算符,特别是 new 运算符,因此 new 的位置与 Java 无关。

但你有几种选择。

  1. 使用工厂或构建器模式
  2. 使用包装器/装饰器模式(可能与工厂一起)允许通过包装其方法来更改某些类功能。
  3. 面向方面的编程。它的工作方式几乎类似于装饰器模式,但可以使用字节码修改来实现。
  4. 类加载器拦截

The following article explains the meaning of placement new in C++: http://www.glenmccl.com/nd_cmp.htm

This term itself is relevant for overloaded new statement. Since Java does not allow to overload operators at all and specifically new operator the placement new is irrelevant for Java.

But you have several alternatives.

  1. Using factory or builder pattern
  2. Using wrapper/decorator pattern (probably together with factory) that allows changin some class functionality by wrapping its methods.
  3. Aspect oriented programming. It works almost like decorator pattern but can be implemented using byte code modifiction.
  4. Class loader interception
独孤求败 2024-12-11 17:42:16

“新的展示位置”这个词本身就有些含糊。该术语被使用
在 C++ 标准中以两种不同的方式,因此由 C++
社区。

第一个含义是指任何重载的operator new函数
它有多个参数。附加参数可以是
用于几乎任何事情 - 有两个例子
标准本身:operator new(size_t, void*) 和operator new(size_t,
std::nothrow_t const&)。

第二个含义是指具体的重载操作符new(size_t,
void*),它实际上用于显式调用构造函数
从其他地方获得的内存上的对象:单独分配
从初始化。 (它将用在像 std::vector 这样的类中,
例如,其中 capacity() 可能大于 size()。)

在 Java 中,内存管理已集成到该语言中,而不是
库的一部分,因此不可能有等效项。

The term "placement new" itself is somewhat ambiguous. The term is used
in two different ways in the C++ standard, and thus by the C++
community.

The first meaning refers to any overloaded operator new function
which has more than one parameter. The additional parameters can be
used for just about anything—there are two examples in the
standard itself: operator new(size_t, void*) and operator new(size_t,
std::nothrow_t const&)
.

The second meaning refers to the specific overload operator new(size_t,
void*)
, which is used in fact to explicitly call the constructor of an
object on memory obtained from elsewhere: to separate allocation
from initialization. (It will be used in classes like std::vector,
for example, where capacity() may be greater than size().)

In Java, memory management is integrated into the language, and is not
part of the library, so there can be no equivalents.

唠甜嗑 2024-12-11 17:42:16

新的放置允许指定带有额外参数的自定义分配器。

还有一个预定义的放置分配器,它接受一个指针作为额外参数,并且只返回该指针的分配结果,基本上允许您的代码在您指定的地址创建对象。

但是,您可以定义采用其他参数的其他类型的分配器,例如我们的调试分配器将文件名和执行分配的行作为额外参数。将这些额外信息与分配的对象一起存储使我们能够追溯到创建某个对象实例的源代码,例如该对象实例在释放后被泄漏、覆盖或使用。

AFAIK Java 在更高的概念级别上工作,并且没有指针概念(只有空指针异常;-))。内存只是一个黑魔法盒,程序员从不使用内存地址的概念。

我只知道 Java 1.1,当时决定不再在该商业产品上投入时间,因此今天 Java 的逻辑级别可能已降低到足以达到随机存取存储器概念。

Placement new allows to specify custom allocators that take extra parameters.

There is also a predefined placement allocator that takes as extra parameter a pointer and that just returns as result of allocation that pointer, basically allowing your code to create objects at the address you specify.

You can however define other types of allocators that take other parameters, for example our debug allocator takes as extra parameters the filename and the line on which the allocation is performed. Storing this extra information with the allocated object allows us to track back to the source code where has been created a certain object instance that for example got leaked or overwritten or used after deallocation.

AFAIK Java works at an higher conceptual level and has no pointer concept (only the null pointer exception ;-) ). Memory is just a black magic box and the programmer never use the idea of memory address.

I only knew Java 1.1 and back then decided to not invest time on that commercial product so may be the logical level of Java lowered enough today to reach the random access memory concept.

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