avatar
t*i
1
Class pointer {
....
...
}
Class screen {
...
..
pointer* mp;
}
function()
{
screen* ps = new screen();
screen ps2= *ps;
....
...
delete ps;
}
when function run, the program will crash. Give the reason.
avatar
f*w
2
I did not see anything wrong here. Any hint?
avatar
t*i
3
I don't know either. This is a question I were asked during a interview.

【在 f**********w 的大作中提到】
: I did not see anything wrong here. Any hint?
avatar
k*n
4
write the code by yourself and see if it crashes.

【在 t*i 的大作中提到】
: I don't know either. This is a question I were asked during a interview.
avatar
b*n
5
screen的copy ctor怎么定义的,如果没有copy ps指向的内容,就有问题了呗。

【在 t*i 的大作中提到】
: Class pointer {
: ....
: ...
: }
: Class screen {
: ...
: ..
: pointer* mp;
: }
: function()

avatar
f*w
6
Based on the original code provided by OP, it wont crash.
But once the mp pointer was pointed to an actual instance of pointer class ,
that will cause problem. Anyway, it is not a good practice.
avatar
S*t
7
shallow copy?

【在 t*i 的大作中提到】
: Class pointer {
: ....
: ...
: }
: Class screen {
: ...
: ..
: pointer* mp;
: }
: function()

avatar
t*u
8
you need to implement copy-ctor for class screen.

【在 t*i 的大作中提到】
: Class pointer {
: ....
: ...
: }
: Class screen {
: ...
: ..
: pointer* mp;
: }
: function()

avatar
t*i
9
感谢各位的解答,我当时也猜是copy constructor的问题, 但是想不明白为啥会crash.

【在 t****u 的大作中提到】
: you need to implement copy-ctor for class screen.
avatar
t*u
10
if you don't have copy-ctor, it just does a member wise copy,
so mp ps->mp will be duplicated to ps2.mp.
your "delete ps", will be supposed to delete the mp in your screen class'
dtor, (otherwise, you will have memory leak). When the function ends, ps2's
dtor will get called. It will try to delete ps2.mp (the same pointer as ps->
mp which is already deleted in ps's dtor), then crash.

crash.

【在 t*i 的大作中提到】
: 感谢各位的解答,我当时也猜是copy constructor的问题, 但是想不明白为啥会crash.
avatar
j*i
11
google 深拷贝 浅拷贝

crash.

【在 t*i 的大作中提到】
: 感谢各位的解答,我当时也猜是copy constructor的问题, 但是想不明白为啥会crash.
avatar
c*x
12
test.
avatar
c*x
13
the reason is you have an unintialized pointer : pointer* mp;
mp is not null.
avatar
c*x
14
int *a = new int(2);
delete a;
delete a;
the delete *a twice is OK.
avatar
c*x
15
test.
pointer {
....
...
}
Class screen {
...
..
pointer* mp=null; // }
function()
{
screen* ps = new screen();
screen ps2= *ps;
....
...
delete ps;
}
avatar
e*w
16
this is a typical source for crashing..

【在 c********x 的大作中提到】
: int *a = new int(2);
: delete a;
: delete a;
: the delete *a twice is OK.

avatar
r*r
17
你确信?

【在 c********x 的大作中提到】
: int *a = new int(2);
: delete a;
: delete a;
: the delete *a twice is OK.

avatar
c*x
18

pretty sure.

【在 r****r 的大作中提到】
: 你确信?
avatar
p*x
19
good point.

s
->

【在 t****u 的大作中提到】
: if you don't have copy-ctor, it just does a member wise copy,
: so mp ps->mp will be duplicated to ps2.mp.
: your "delete ps", will be supposed to delete the mp in your screen class'
: dtor, (otherwise, you will have memory leak). When the function ends, ps2's
: dtor will get called. It will try to delete ps2.mp (the same pointer as ps->
: mp which is already deleted in ps's dtor), then crash.
:
: crash.

avatar
z*e
20
no, at least doesn't work for visual c++, tested.

【在 c********x 的大作中提到】
:
: pretty sure.

avatar
W*g
21
dangling pointer.
avatar
h*e
22
请你不要玷污Caltech的名誉好不好。

【在 c********x 的大作中提到】
: test.
: pointer {
: ....
: ...
: }
: Class screen {
: ...
: ..
: pointer* mp=null; // : }

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。