t*i 发帖数: 72 | 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. |
f**********w 发帖数: 93 | 2 I did not see anything wrong here. Any hint? |
t*i 发帖数: 72 | 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?
|
k****n 发帖数: 1334 | 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.
|
b********n 发帖数: 609 | 5 screen的copy ctor怎么定义的,如果没有copy ps指向的内容,就有问题了呗。
【在 t*i 的大作中提到】 : Class pointer { : .... : ... : } : Class screen { : ... : .. : pointer* mp; : } : function()
|
f**********w 发帖数: 93 | 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. |
S****t 发帖数: 1186 | 7 shallow copy?
【在 t*i 的大作中提到】 : Class pointer { : .... : ... : } : Class screen { : ... : .. : pointer* mp; : } : function()
|
t****u 发帖数: 8614 | 8 you need to implement copy-ctor for class screen.
【在 t*i 的大作中提到】 : Class pointer { : .... : ... : } : Class screen { : ... : .. : pointer* mp; : } : function()
|
t*i 发帖数: 72 | 9 感谢各位的解答,我当时也猜是copy constructor的问题, 但是想不明白为啥会crash.
【在 t****u 的大作中提到】 : you need to implement copy-ctor for class screen.
|
t****u 发帖数: 8614 | 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.
|
|
|
j***i 发帖数: 3096 | 11 google 深拷贝 浅拷贝
crash.
【在 t*i 的大作中提到】 : 感谢各位的解答,我当时也猜是copy constructor的问题, 但是想不明白为啥会crash.
|
c********x 发帖数: 84 | |
c********x 发帖数: 84 | 13 the reason is you have an unintialized pointer : pointer* mp;
mp is not null. |
c********x 发帖数: 84 | 14 int *a = new int(2);
delete a;
delete a;
the delete *a twice is OK. |
c********x 发帖数: 84 | 15 test.
pointer {
....
...
}
Class screen {
...
..
pointer* mp=null; // <- should fix the problem.
}
function()
{
screen* ps = new screen();
screen ps2= *ps;
....
...
delete ps;
} |
e*****w 发帖数: 144 | 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.
|
r****r 发帖数: 115 | 17 你确信?
【在 c********x 的大作中提到】 : int *a = new int(2); : delete a; : delete a; : the delete *a twice is OK.
|
c********x 发帖数: 84 | 18
pretty sure.
【在 r****r 的大作中提到】 : 你确信?
|
p****x 发帖数: 707 | 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.
|
z***e 发帖数: 5393 | 20 no, at least doesn't work for visual c++, tested.
【在 c********x 的大作中提到】 : : pretty sure.
|
W*********g 发帖数: 409 | |
h****e 发帖数: 2125 | 22 请你不要玷污Caltech的名誉好不好。
【在 c********x 的大作中提到】 : test. : pointer { : .... : ... : } : Class screen { : ... : .. : pointer* mp=null; // <- should fix the problem. : }
|