C++ 中的放置新运算符是什么? java有吗?
我听说过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
以下文章解释了 C++ 中放置 new 的含义: http://www.glenmccl.com/nd_cmp.htm
这个术语本身与重载的
new
语句相关。由于 Java 根本不允许重载运算符,特别是 new 运算符,因此 new 的位置与 Java 无关。但你有几种选择。
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.
“新的展示位置”这个词本身就有些含糊。该术语被使用
在 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
functionwhich 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*)
andoperator new(size_t,
.std::nothrow_t const&)
The second meaning refers to the specific overload
operator new(size_t,
, which is used in fact to explicitly call the constructor of anvoid*)
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 thansize()
.)In Java, memory management is integrated into the language, and is not
part of the library, so there can be no equivalents.
新的放置允许指定带有额外参数的自定义分配器。
还有一个预定义的放置分配器,它接受一个指针作为额外参数,并且只返回该指针的分配结果,基本上允许您的代码在您指定的地址创建对象。
但是,您可以定义采用其他参数的其他类型的分配器,例如我们的调试分配器将文件名和执行分配的行作为额外参数。将这些额外信息与分配的对象一起存储使我们能够追溯到创建某个对象实例的源代码,例如该对象实例在释放后被泄漏、覆盖或使用。
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.