P********e 发帖数: 2610 | 1 int * i = (int *) new double(3.0);
delete i;
这样会不会有内存泄露. |
p***o 发帖数: 1252 | 2 Standard says: void operator delete(void*) throw()
There is no size in the parameters. So I guess there will be no memory leak.
【在 P********e 的大作中提到】 : int * i = (int *) new double(3.0); : delete i; : 这样会不会有内存泄露.
|
b******n 发帖数: 592 | 3 I would think not. If it is on heap, I don't think it will cause any problem
. If the compiler decides to put it in stack for some reason, then it will
cause
problem.
【在 P********e 的大作中提到】 : int * i = (int *) new double(3.0); : delete i; : 这样会不会有内存泄露.
|
X****r 发帖数: 3557 | 4 There should be not memory leak, as delete knows the size
of the memory chunk to be release. But just don't do it.
【在 P********e 的大作中提到】 : int * i = (int *) new double(3.0); : delete i; : 这样会不会有内存泄露.
|
s*w 发帖数: 729 | 5 某些 compilers 分配内存的时候,用指针地址当 key, 具体的字节数目当 value ,
统统存起
来;所以delete的时候知道释放多少字节。另一些直接多分配一个 word, 存后面分配
的字节数目n,
new 返回实际指针后一个 word 的地址, delete 的时候往前面走一格,释放n字节+
1word
现学的,请指点 why: " But just don't do it."
【在 X****r 的大作中提到】 : There should be not memory leak, as delete knows the size : of the memory chunk to be release. But just don't do it.
|
X****r 发帖数: 3557 | 6 Because strictly speaking the result is undefined, per 5.3.5
3 In the first alternative (delete object), if the static type
of the operand is different from its dynamic type, the static
type shall be a base class of the operand's dynamic type
and the static type shall have a virtual destructor or the
behavior is undefined. In the second alternative (delete array)
if the dynamic type of the object to be deleted differs from
its static type, the behavior is undefined.
And this trick is not buying you anything anyway.
【在 s*w 的大作中提到】 : 某些 compilers 分配内存的时候,用指针地址当 key, 具体的字节数目当 value , : 统统存起 : 来;所以delete的时候知道释放多少字节。另一些直接多分配一个 word, 存后面分配 : 的字节数目n, : new 返回实际指针后一个 word 的地址, delete 的时候往前面走一格,释放n字节+ : 1word : 现学的,请指点 why: " But just don't do it."
|
s*w 发帖数: 729 | 7 涨知识了,多谢
【在 X****r 的大作中提到】 : Because strictly speaking the result is undefined, per 5.3.5 : 3 In the first alternative (delete object), if the static type : of the operand is different from its dynamic type, the static : type shall be a base class of the operand's dynamic type : and the static type shall have a virtual destructor or the : behavior is undefined. In the second alternative (delete array) : if the dynamic type of the object to be deleted differs from : its static type, the behavior is undefined. : And this trick is not buying you anything anyway.
|
j*******e 发帖数: 674 | 8 My understanding about this:
delete will do two things - destruct the object, and release the memory.
In this case, the destruction of the object is "undefined".
But the memory will be released for sure. Specifically for the C++ primitive data types, there will be no memory leak.
【在 X****r 的大作中提到】 : Because strictly speaking the result is undefined, per 5.3.5 : 3 In the first alternative (delete object), if the static type : of the operand is different from its dynamic type, the static : type shall be a base class of the operand's dynamic type : and the static type shall have a virtual destructor or the : behavior is undefined. In the second alternative (delete array) : if the dynamic type of the object to be deleted differs from : its static type, the behavior is undefined. : And this trick is not buying you anything anyway.
|
g*********s 发帖数: 1782 | 9 under which reason would compiler put a new-created pointer to stack?
problem
will
【在 b******n 的大作中提到】 : I would think not. If it is on heap, I don't think it will cause any problem : . If the compiler decides to put it in stack for some reason, then it will : cause : problem.
|
d****p 发帖数: 685 | 10 That's very right - if the destructor is not virtual, deleting child object
as parent object pointer is very bad.
I would like to provide an extreme case:
The child class has custom new operator such that its instance is not
allocated on heap. So trying to delete the instance typed under its parent
class would raise signal for freeing unallocated memory.
primitive data types, there will be no memory leak.
【在 j*******e 的大作中提到】 : My understanding about this: : delete will do two things - destruct the object, and release the memory. : In this case, the destruction of the object is "undefined". : But the memory will be released for sure. Specifically for the C++ primitive data types, there will be no memory leak. :
|
b******n 发帖数: 592 | 11 probably never. If I am the compiler and I see code like this, I wish I can
use stack.
【在 g*********s 的大作中提到】 : under which reason would compiler put a new-created pointer to stack? : : problem : will
|