由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - a c++ question
相关主题
pointer to base class = new derived, what will happend??求教:c++中如何从raw data中创建对象?
one question about operator delete一道c++的考古题
class with a pointer member请问关于c++实现singleton的问题?
What are possible reasons for the program to crash before r[合集] An interview question, what is the answer? (转载)
VC++ release VS debugC++ 书推荐
Why my new or delete operator would fail?问一个empty class的size的问题
c++ pointers are iterators, why?急问:compile and build dependency
C++ questionWhoCrashed v1.01 - post-mortem crashdump analysis.
相关话题的讨论汇总
话题: pointer话题: screen话题: mp话题: ps话题: class
进入Programming版参与讨论
1 (共1页)
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.
相关主题
Why my new or delete operator would fail?求教:c++中如何从raw data中创建对象?
c++ pointers are iterators, why?一道c++的考古题
C++ question请问关于c++实现singleton的问题?
进入Programming版参与讨论
j***i
发帖数: 3096
11
google 深拷贝 浅拷贝

crash.

【在 t*i 的大作中提到】
: 感谢各位的解答,我当时也猜是copy constructor的问题, 但是想不明白为啥会crash.
c********x
发帖数: 84
12
test.
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
21
dangling pointer.
h****e
发帖数: 2125
22
请你不要玷污Caltech的名誉好不好。

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

1 (共1页)
进入Programming版参与讨论
相关主题
WhoCrashed v1.01 - post-mortem crashdump analysis.VC++ release VS debug
Interview Question: System CrashWhy my new or delete operator would fail?
find bugs of c++ codesc++ pointers are iterators, why?
about STL functor and function pointersC++ question
pointer to base class = new derived, what will happend??求教:c++中如何从raw data中创建对象?
one question about operator delete一道c++的考古题
class with a pointer member请问关于c++实现singleton的问题?
What are possible reasons for the program to crash before r[合集] An interview question, what is the answer? (转载)
相关话题的讨论汇总
话题: pointer话题: screen话题: mp话题: ps话题: class