展示位置运算符新/删除问题 [英] Placement operator new/delete question

查看:68
本文介绍了展示位置运算符新/删除问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们使用< new>中提供的标准展示位置新运算符,并且

不是owr自己的定义时,是不是要调用展示位置删除?

考虑代码:

#include< new>

class SomeClass {};


int main()

{

使用命名空间std;


unsigned char garbage [sizeof(SomeClass)];


SomeClass * t = new(垃圾)SomeClass;


删除t;

}


-

Ioannis Vranos

http: //www23.brinkster.com/noicys

When we use the standard placement new operator provided in <new>, and
not a definition of owr own, isn''t a call to placement delete enough?
Consider the code:
#include <new>
class SomeClass{};

int main()
{
using namespace std;

unsigned char garbage[sizeof(SomeClass)];

SomeClass *t = new(garbage) SomeClass;

delete t;
}

--
Ioannis Vranos

http://www23.brinkster.com/noicys

推荐答案

" Ioannis Vranos" < iv*@guesswh.at.grad.com>在消息中写道

news:1097885170.258380@athnrd02 ...
"Ioannis Vranos" <iv*@guesswh.at.grad.com> wrote in message
news:1097885170.258380@athnrd02...
当我们使用< new>中提供的标准放置新运算符时,
不是定义owr自己,是不是打电话给地点删除够了?

考虑代码:

#include< new>

class SomeClass {};

int main()
{
使用命名空间std;

unsigned char garbage [sizeof(SomeClass)];

SomeClass * t = new(垃圾)SomeClass;


由于对齐问题,上述内容通常不可移植。这是

更便携:

int main()

{

unsigned char * garbage = new unsigned char [sizeof(SomeClass)];


SomeClass * t = new(垃圾)SomeClass;


t->〜 SomeClass();

删除[]垃圾;

}


我正在讲一个关于任意类的一般术语名为SomeClass。

可能有一些特殊情况对于我不知道的空类。

http://www.gotw.ca/gotw/028.htm

删除t;
}
When we use the standard placement new operator provided in <new>, and
not a definition of owr own, isn''t a call to placement delete enough?
Consider the code:
#include <new>
class SomeClass{};

int main()
{
using namespace std;

unsigned char garbage[sizeof(SomeClass)];

SomeClass *t = new(garbage) SomeClass;
The above is generally not portable because of alignment issues. This is
more portable:

int main()
{
unsigned char* garbage = new unsigned char[sizeof(SomeClass)];

SomeClass *t = new(garbage) SomeClass;

t->~SomeClass();
delete[] garbage;
}

I''m speaking in general terms about an arbitrary class named SomeClass.
There may be some "special case" for empty classes of which I''m not aware.
See http://www.gotw.ca/gotw/028.htm.
delete t;
}



您对展示位置删除的调用是什么意思?什么是展示位置

删除?你的代码似乎没有做任何与众不同的事情

。这看起来像你试图释放数组垃圾,

,这是一些未定义的行为。你为什么要这样做?如果你只是想要调用SomeClass的析构函数,那么只需要写一下


t->〜SomeClass();


如上所述。


-

David Hilsee



What do you mean by a call to "placement delete"? What is "placement
delete"? You code doesn''t seem to be doing anything out of the ordinary
here. This looks like you''re trying to deallocate the array "garbage",
which smacks of undefined behavior. Why would you do this? If you just
want to call SomeClass''s destructor, then just need to write

t->~SomeClass();

as I have written above.

--
David Hilsee


在文章< 1097885170.258380@athnrd02>,

Ioannis Vranos< iv*@guesswh.at.grad.com>写道:
In article <1097885170.258380@athnrd02>,
Ioannis Vranos <iv*@guesswh.at.grad.com> wrote:
当我们使用< new>中提供的标准放置新运算符,并且
不是owr自己的定义时,isn''ta调用放置删除足够?

考虑代码:

#include< new>

class SomeClass {};

int main ()
{
使用命名空间std;

unsigned char garbage [sizeof(SomeClass)];

SomeClass * t = new(垃圾)SomeClass ;

删除t;
}
When we use the standard placement new operator provided in <new>, and
not a definition of owr own, isn''t a call to placement delete enough?
Consider the code:
#include <new>
class SomeClass{};

int main()
{
using namespace std;

unsigned char garbage[sizeof(SomeClass)];

SomeClass *t = new(garbage) SomeClass;

delete t;
}




在我看来,上面的代码相当糟糕。你在堆栈上的代码块上调用了删除

(即它没有被新生。)


我认为它应该是写得像这样:

int main(){

unsigned char garbage [sizeof(SomeClass)];

SomeClass * t = new(垃圾)SomeClass;


t-> ~For SomeClass();

}



It seems to me that the above code is rather bad. You are calling delete
on a block of code that is sitting on the stack (ie it wasn''t newed.)

I would think it should be written like this:

int main() {
unsigned char garbage[sizeof(SomeClass)];
SomeClass* t = new( garbage ) SomeClass;

t->~SomeClass();
}


David Hilsee写道:
David Hilsee wrote:
由于对齐问题,上述内容通常不可移植。这更便于携带:

int main()
{
unsigned char * garbage = new unsigned char [sizeof(SomeClass)];

SomeClass * t = new(垃圾)SomeClass;

t-> ~TomeClass();
删除[]垃圾;
}



什么是对齐问题?我看不出这两种表格之间有什么区别。

您对展示位置删除的调用是什么意思?什么是展示位置
删除?你的代码似乎没有做任何与众不同的事情。这看起来像你试图释放数组垃圾,
这是一些未定义的行为。你为什么要这样做?如果你只想打电话给SomeClass的析构函数,那么只需要写一下

t-> ~AskClass();

就像我写的那样上面。
The above is generally not portable because of alignment issues. This is
more portable:

int main()
{
unsigned char* garbage = new unsigned char[sizeof(SomeClass)];

SomeClass *t = new(garbage) SomeClass;

t->~SomeClass();
delete[] garbage;
}

What alignment issues? I can''t see any difference between the two forms.
What do you mean by a call to "placement delete"? What is "placement
delete"? You code doesn''t seem to be doing anything out of the ordinary
here. This looks like you''re trying to deallocate the array "garbage",
which smacks of undefined behavior. Why would you do this? If you just
want to call SomeClass''s destructor, then just need to write

t->~SomeClass();

as I have written above.




关于贴牌操作员删除的参考:

C ++ 2003 18.4.1.3

TC ++ PL 3:Page 576,19.4.5。


VS帮助说明了展示位置删除表格:


"第二个函数由放置删除表达式调用

对应于new(std :: size_t)形式的新表达式。它没有.b
没有。

在另一个地方:


"这个运算符的第二和第三种形式通常会不能从代码中调用

但是存在给编译器一个匹配的删除来调用

a placement new失败。

所以如果我如果没有明确定义
明确定义,则放置删除不会做任何事情,但是当例外是

抛出等等时提供匹配。


所以我们确实必须明确地调用析构函数。然而实验

我的编译器让我感到困惑。


-

Ioannis Vranos

http://www23.brinkster.com/noicys


这篇关于展示位置运算符新/删除问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆