问题:delete一块不是由常规new分配的内存会干什么?
c++吧
全部回复
仅看楼主
level 11
LuoJi_1995 楼主
这是未定义行为吗?
@幻の上帝 
2011年07月23日 15点07分 1
level 12
你是说非常规的new?[啊!]
2011年07月23日 15点07分 2
level 11
LuoJi_1995 楼主
嗯。
2011年07月23日 15点07分 3
level 7
是像delete (C*)malloc(sizeof(C))这种吗?这是未定义的,而且经常会破坏堆
2011年07月23日 15点07分 4
level 11
LuoJi_1995 楼主
可以这么理解。
2011年07月23日 15点07分 5
level 7
实际是给函数传递非法参数,函数会干什么谁知道……
2011年07月23日 15点07分 6
level 9
Using delete on a pointer to an object not allocated with new gives unpredictable results. //来源MSDN
2011年07月23日 15点07分 7
level 15
如果操作数是空指针,那么没效果。
其它情况下,C++03没说清楚(数组形式则说清楚需要是new-operator分配的),估计是个bug,会有CWT的defect report,等等去找找。
C++0x则明确地说明会引起undefined behavior。
ISO C++03:
5.3.5/2
... In either alternative, if the value of the operand of delete is the null pointer the operation has no effect. In the first alternative (delete object), the value of the operand of delete shall be a pointer to a non-array object or a pointer to a sub-object (1.8) representing a base class of such an object (clause
10). If not, the behavior is undefined. In the second alternative (delete array), the value of the operand of delete shall be the pointer value which resulted from a previous array new-expression.72) If not, the behavior is undefined.
72) For non-zero-length arrays, this is the same as a pointer to the first element of the array created by that new-expression. Zero-length arrays do not have a first element.
ISO C++0x(N3242):
5.3.5/2 ...In the first alternative (delete object), the value of the operand of delete may be a null pointer value, a pointer to a non-array object created by a previous new-expression, or a pointer to a subobject (1.8)
representing a base class of such an object (Clause 10). If not, the behavior is undefined. In the second alternative (delete array), the value of the operand of delete may be a null pointer value or a pointer value that resulted from a previous array new-expression.78
78) For non-zero-length arrays, this is the same as a pointer to the first element of the array created by that new-expression. Zero-length arrays do not have a first element.

2011年07月23日 15点07分 8
level 15
口胡,CWT→CWG。。
找到了(地址已被度娘吃→ →)
C++ Standard Core Language Defect Reports, Revision 76
1037. Requirements for operands of delete-expressions and deallocation functions
2011年07月23日 15点07分 10
level 12
虽然是这样说,我认为只要用定位new将内存初始化了就不会有问题。
2011年07月23日 15点07分 11
level 13
指针直接调析构,然后内存free
2011年07月23日 16点07分 12
level 15
好吧,表示漏看了LZ“常规”两个字。。[拍砖]
那么看你底层是怎么搞到的了。。。
比如:
#include <cstdlib>
#include <new>
int main()
{
{
char* p = static_cast<char*>(std::malloc(1000));
char* q = new(p) char[1000/sizeof(char)]; // post-condition: q == p;
delete q; // equivilent to delete p, which is not resulted from a new-expression, causes undefined behavior;
}
{
char* p = new char[1000];
char* q = new(p) char[1000/sizeof(char)]; // post-condition: q == p;
delete q; // well-formed, equivalent to delete p at once, and both p and q become invalid pointers due to the call of deallocation function;
delete p; // double-deletion definitely leads to undefined behavior, for invalid pointer passed to deallocation function;
}
}

2011年07月23日 17点07分 13
level 15
equivilent→equivalent...[拍砖]
2011年07月23日 17点07分 14
level 1
这个不就是某段内存已经释放了还要释放一次,当然会出错了。
按理说假如new返回的地址是分配内存的首地址,系统又没有两种动态分配内存的方式(是系统),delete又仅是释放内存(没有析构函数或用默认析构函数)就不会有问题。
2011年07月24日 03点07分 15
level 11
LuoJi_1995 楼主
比如这么一段代码:
struct Foo
{
};
//...
Foo *f = (Foo *)malloc(sizeof(Foo) * 2) + 1;
new (Foo) f;
delete f;
free(--f);
2011年07月24日 04点07分 16
level 11
LuoJi_1995 楼主
2011年07月24日 04点07分 17
level 12
显然内存重复释放了
2011年07月24日 04点07分 18
level 11
LuoJi_1995 楼主
可是delete不是不能释放malloc出来的东西么
2011年07月24日 04点07分 19
level 12
另外为什么要用malloc呢?new并不一定调用malloc的。
Foo* f=static_cast<Foo*>(::operator new(2 * sizeof(Foo)));
2011年07月24日 04点07分 20
level 15
重复释放内存在这里只是对比举例……尽管都会引起UB,但是不见得是可观察的错误。系统有几种动态分配内存的方式系统,通过核心语言特性是无法判断的。
上面那个例子可能不太清楚,换成这个
char* p = static_cast<char*>(std::malloc(1000));
int* q = new(p) int[1000/sizeof(int)];
delete q;
就应该比较明确的会引起UB了。

2011年07月24日 04点07分 21
1 2 尾页