删除POD的[] [英] delete[] of POD
问题描述
int * p;
p = new int [10];
void * q =(void *)p;
delete [] q;
如果p保证是POD,这是否有效? (不要问为什么......有一个
的原因我需要这个。显然我会在函数中创建p,在另一个
函数中使用p知道什么在一个函数中输入p是和删除p为
简单不知道p是什么)
读取MSDN似乎
"全局运算符删除函数,如果声明,则采用单个参数
$ b $类型为void *,其中包含指向要释放的对象的指针。
返回类型为void(operator delete无法返回值)。存在两种形式
用于类成员操作员删除功能:
所以应该没问题但是这是MSDN指南: - )
--- bye
int *p;
p = new int[10];
void *q = (void*)p;
delete[] q;
Is this valid if p is guaranteed to be a POD? (don''t ask why... there is a
reason I need this. Clearly I will create p in function, use p in another
function that know what type p is and delete p in a function that for
simplicity won''t know what p is)
Reading the MSDN it seems that
"The global operator delete function, if declared, takes a single argument
of type void *, which contains a pointer to the object to deallocate. The
return type is void (operator delete cannot return a value). Two forms exist
for class-member operator delete functions:"
so it should be ok But this is the MSDN guide :-)
--- bye
推荐答案
MaxMax写道:
int * p;
p = new int [10];
void * q =(void *)p;
delete [] q;
这是否有效p保证是POD吗? (不要问为什么......有一个原因我需要这个。显然我会在函数中创建p,在另一个知道什么类型p的函数中使用p并删除p用于简单的功能不知道p是什么)
阅读MSDN似乎
全局运算符删除函数,如果声明,采用类型为void *的单个参数,其中包含指向要释放的对象的指针。
返回类型为void(operator delete无法返回值)。对于类成员操作员删除功能存在两种形式:
所以它应该没问题但是这是MSDN指南:-)
--- bye
int *p;
p = new int[10];
void *q = (void*)p;
delete[] q;
Is this valid if p is guaranteed to be a POD? (don''t ask why... there is a
reason I need this. Clearly I will create p in function, use p in another
function that know what type p is and delete p in a function that for
simplicity won''t know what p is)
Reading the MSDN it seems that
"The global operator delete function, if declared, takes a single argument
of type void *, which contains a pointer to the object to deallocate. The
return type is void (operator delete cannot return a value). Two forms exist
for class-member operator delete functions:"
so it should be ok But this is the MSDN guide :-)
--- bye
operator delete []在标准中有一个void *作为参数,所以你很好。
-
问候,
Ferdi Smit(理学硕士)
电邮: Fe ******** @ cwi.nl
房间:C0.07电话: 4229
INS3可视化和3D界面
CWI荷兰阿姆斯特丹
operator delete[] has a void* as parameter in the standard, so you''re fine.
--
Regards,
Ferdi Smit (M.Sc.)
Email: Fe********@cwi.nl
Room: C0.07 Phone: 4229
INS3 Visualization and 3D Interfaces
CWI Amsterdam, The Netherlands
* Ferdi Smit:
* Ferdi Smit:
MaxMax写道:
int * p;
p = new int [10];
void * q =(void *)p;
delete [] q;
如果p保证是POD,这是否有效? (不要问为什么......有一个原因我需要这个。显然我会在函数中创建p,在另一个知道什么类型p的函数中使用p并删除p用于简单的功能不知道p是什么)
阅读MSDN似乎
全局运算符删除函数,如果声明,采用类型为void *的单个参数,其中包含指向要释放的对象的指针。
返回类型为void(operator delete无法返回值)。对于类成员操作员删除功能存在两种形式:
所以应该没问题但是这是MSDN指南: - )
int *p;
p = new int[10];
void *q = (void*)p;
delete[] q;
Is this valid if p is guaranteed to be a POD? (don''t ask why... there is a
reason I need this. Clearly I will create p in function, use p in another
function that know what type p is and delete p in a function that for
simplicity won''t know what p is)
Reading the MSDN it seems that
"The global operator delete function, if declared, takes a single argument
of type void *, which contains a pointer to the object to deallocate. The
return type is void (operator delete cannot return a value). Two forms exist
for class-member operator delete functions:"
so it should be ok But this is the MSDN guide :-)
> operator delete []在标准中有一个void *作为参数,所以你没事。
operator delete[] has a void* as parameter in the standard, so you''re fine.
对不起,这是一个不正确的推理。 />
标准将此定义为未定义行为,如果某人不应该看到这个含义,它会在说明73中明确指出void *:
[上下文:删除数组]" ...使用
类型void *的指针无法删除对象因为..."。
-
答:因为它弄乱了人们通常阅读文字的顺序。
问:为什么这么糟糕东西?
A:热门发布。
问:usenet和电子邮件中最烦人的是什么?
Sorry, that''s an incorrect inference.
The standard defines this as Undefined Behavior, and in case somebody should
not see that implication, it states that explicitly for void* in note 73:
[context: delete array] "... an object cannot be deleted using a pointer of
type void* because ...".
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Alf P. Steinbach写道:
Alf P. Steinbach wrote:
operator delete []在标准中有一个void *作为参数,所以你没事。
operator delete[] has a void* as parameter in the standard, so you''re fine.
对不起,这是一个不正确的推理。
标准将此定义为未定义行为,如果某人不应该看到这种含义,它会在注释73中明确指出void *:
[context:delete array ]...使用
类型void *的指针无法删除对象因为...。
Sorry, that''s an incorrect inference.
The standard defines this as Undefined Behavior, and in case somebody should
not see that implication, it states that explicitly for void* in note 73:
[context: delete array] "... an object cannot be deleted using a pointer of
type void* because ...".
我认为指的是一个虚空的数组是不可能的(大小为0,类似的原因,为什么一个(空的)实例化对象的大小至少为
1);因此你不能删除[]一个真正的空白*。但在max'的情况下,
基础数据实际上是另一种类型(int *)。运算符delete []
取无效*,所以无论你传递什么指针,它都会在运算符运行之前首先转换为
void *。 />
-
问候,
Ferdi Smit(理学硕士)
电子邮件: Fe********@cwi.nl
房间:C0.07电话:4229
INS3可视化和3D界面
CWI荷兰阿姆斯特丹
I think that refers to an array of void which is impossible (size 0, a
similar reason to why an (empty) instantiated object has size at least
1); and thus you can''t delete[] a true void*. But in max''s case the
underlying data is in fact of another type (int*). The operator delete[]
takes a void*, so whatever pointer you pass it, it will be cast to a
void* first before the operator runs anyway.
--
Regards,
Ferdi Smit (M.Sc.)
Email: Fe********@cwi.nl
Room: C0.07 Phone: 4229
INS3 Visualization and 3D Interfaces
CWI Amsterdam, The Netherlands
这篇关于删除POD的[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!