由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - placement new
相关主题
[合集] C++的弱问题effective C++里的memory pool 一问:
回答C++的弱问题why do we still use dynamic allocation?
C++: operator new 为啥要是 static的, 不是有啥影响?菜鸟请教C问题
一个C++ operator new的重载问题C++问题
one question about overloading operator delete我这个C++程序有没有什么问题啊?请指点。。。谢谢。。。
形参可以直接使用私有数据成员?c++ 宏的问题
What is the difference between operator new and the new几个C++书写风格疑问
关于C/C++里的Static variable的memory allocation/initializa[合集] C++ virtual function的一个问题。。
相关话题的讨论汇总
话题: new话题: placement话题: ctor话题: calls话题: do
进入Programming版参与讨论
1 (共1页)
b***y
发帖数: 2799
1
class A
{
};
void main(void)
{
.... allocate memory m.
A *a = new (m) A;
...
}
结果编译通不过,说NEW不接受两个参数。难道PLACEMENT NEW不象operater new那样有
个global版本?
t****t
发帖数: 6806
2
placement new is declared in

【在 b***y 的大作中提到】
: class A
: {
: };
: void main(void)
: {
: .... allocate memory m.
: A *a = new (m) A;
: ...
: }
: 结果编译通不过,说NEW不接受两个参数。难道PLACEMENT NEW不象operater new那样有

E*****7
发帖数: 128
3
Assuming you have done somewhere in your program:
(1) A* m = new A;
Then try to plamcement new it as:
(2) A* a = (A*) operator new(sizeof(A), m);
Do not forget that now both a and m are pointing to the same object. (2) did not call ctor of A, i.e., (1) differs from (2) in that it calls the ctor of A.
t****t
发帖数: 6806
4
your (2) is not placement new. it's the alternative form of operator new,
which placement new calls, and does nothing except returning the 2nd
parameter.
placement new, on the other hand, does exactly the opposite: calls the ctor
of A, but does not allocate memory.

did not call ctor of A, i.e., (1) differs from (2) in that it calls the ctor
of A.

【在 E*****7 的大作中提到】
: Assuming you have done somewhere in your program:
: (1) A* m = new A;
: Then try to plamcement new it as:
: (2) A* a = (A*) operator new(sizeof(A), m);
: Do not forget that now both a and m are pointing to the same object. (2) did not call ctor of A, i.e., (1) differs from (2) in that it calls the ctor of A.

E*****7
发帖数: 128
5
Can thrust give the placement new answer? Thanks.
t****t
发帖数: 6806
6
the op's placement new is already correct.

【在 E*****7 的大作中提到】
: Can thrust give the placement new answer? Thanks.
E*****7
发帖数: 128
7
Do the following three steps do what placement new is supposed to do?
t****t
发帖数: 6806
8
(3) call (2).

previously

【在 E*****7 的大作中提到】
: Do the following three steps do what placement new is supposed to do?
1 (共1页)
进入Programming版参与讨论
相关主题
[合集] C++ virtual function的一个问题。。one question about overloading operator delete
pointer to base class = new derived, what will happend??形参可以直接使用私有数据成员?
请教一个c++ throw exception 问题What is the difference between operator new and the new
What does the default constructor do?关于C/C++里的Static variable的memory allocation/initializa
[合集] C++的弱问题effective C++里的memory pool 一问:
回答C++的弱问题why do we still use dynamic allocation?
C++: operator new 为啥要是 static的, 不是有啥影响?菜鸟请教C问题
一个C++ operator new的重载问题C++问题
相关话题的讨论汇总
话题: new话题: placement话题: ctor话题: calls话题: do