通过不同类型的指针删除缓冲区? [英] deleting a buffer through a different type of pointer?
本文介绍了通过不同类型的指针删除缓冲区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
说我有以下C ++:
char *p = new char[cb];
SOME_STRUCT *pSS = (SOME_STRUCT *) p;
delete pSS;
根据C ++标准,这是否安全?是否需要转回 char *
,然后使用 delete []
?我知道它将工作在大多数C ++编译器,因为它是普通的普通数据,没有析构函数。确保安全吗?
Is this safe according to the C++ standard? Do I need to cast back to a char*
and then use delete[]
? I know it'll work in most C++ compilers, because it's plain-ordinary-data, with no destructors. Is it guaranteed to be safe?
推荐答案
不能保证安全。这里有一个相关的链接在C + + FAQ lite:
It's not guaranteed to be safe. Here's a relevant link in the C++ FAQ lite:
[16.13]删除数组时可以删除 []
的某些内置类型( char
, int
等)?
[16.13] Can I drop the []
when deleting array of some built-in type (char
, int
, etc.)?
http://www.parashift.com/c++-faq -lite / freestore-mgmt.html#faq-16.13
这篇关于通过不同类型的指针删除缓冲区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文