由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 问个无厘头问题
相关主题
[C++] when destructors get called面试问题
new一定要和delete配对吗?(面试题) 给code挑毛病(updated with multiple choices)
one more interview question请教个Bloomberg 的 C++ 题目
question regarding effective c++ by Meyers【C++】请问这样有没有memory leak?多谢
0 < -1 ? A c++ question[合集] c++ delete问题
求教两个new - delete 问题, C++弱问一下
请教一下,exception时,destructor一定会被调用么?容器中放置智能指针一问
问个c++的弱问题C++隐式类型转换的规则?
相关话题的讨论汇总
话题: delete话题: memory话题: type话题: object话题: undefined
进入Programming版参与讨论
1 (共1页)
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

1 (共1页)
进入Programming版参与讨论
相关主题
C++隐式类型转换的规则?0 < -1 ? A c++ question
C++命名空间和算子重载求教两个new - delete 问题, C++
关于数组请教一下,exception时,destructor一定会被调用么?
有个SB interviewer和我说++i比i++好 (转载)问个c++的弱问题
[C++] when destructors get called面试问题
new一定要和delete配对吗?(面试题) 给code挑毛病(updated with multiple choices)
one more interview question请教个Bloomberg 的 C++ 题目
question regarding effective c++ by Meyers【C++】请问这样有没有memory leak?多谢
相关话题的讨论汇总
话题: delete话题: memory话题: type话题: object话题: undefined