由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - some C++ interview questions
相关主题
C++含有指针成员的类c++ dynamic cast
Help! Virtual Destructor问个 ctor/copy ctor的问题
急问:compile and build dependencyis pthread_mutex_destroy() required to call?
find bugs of c++ codesC++一个类中多个thread的问题
【C++】请问这样有没有memory leak?多谢C++问题,confusing...
Interview question: is the following code OK?一个C++的问题
new一定要和delete配对吗?one question about operator delete
Forward declaration with unique_ptr一道c++的考古题
相关话题的讨论汇总
话题: object话题: when话题: scope话题: gc话题: destructor
进入Programming版参与讨论
1 (共1页)
w**t
发帖数: 592
1
1. Why destructor is not virtual by default?
2. How to bring destructor back once the object is out of scope?
3. How to stop 1 people amony several others from acessing a function object
?
p*u
发帖数: 2454
2

There are many cases when you surely don't want your class to be base class
of other classes. By making a dtor non-virtual, a class is not supposed to be
derived from. As for performance, virtual dtors increase the size of
object(pointer to vt). The problem with virtual dtors is not only the
overhead associated with dynamic binding but also because most dtors are
either explicitly or implicitly inlined. virtual dtors cannot be inlined.
don't quite understand the question...
make the operator()

【在 w**t 的大作中提到】
: 1. Why destructor is not virtual by default?
: 2. How to bring destructor back once the object is out of scope?
: 3. How to stop 1 people amony several others from acessing a function object
: ?

w**t
发帖数: 592
3
don't quite understand the question...
I didnt get it either. Question: when the destructor is called? Answer: when
the object is out of scope? Then he asked how to bring it back (call the
destructor??) even if the object is out of scope?
S*****n
发帖数: 227
4
Did u miss some contexts for these questions? or it looks quite weird.

object

【在 w**t 的大作中提到】
: 1. Why destructor is not virtual by default?
: 2. How to bring destructor back once the object is out of scope?
: 3. How to stop 1 people amony several others from acessing a function object
: ?

p*u
发帖数: 2454
5

when
~~~~~~~~~~~~~~~~~~~~~~~~~~~or when delete gets called
~~~~~~~~~~~~~~~I guess he meant how to bring the object back when it's out
of scope. just declare the object as static.

【在 w**t 的大作中提到】
: don't quite understand the question...
: I didnt get it either. Question: when the destructor is called? Answer: when
: the object is out of scope? Then he asked how to bring it back (call the
: destructor??) even if the object is out of scope?

k*k
发帖数: 508
6
I think he is asking "how to bring the object back"
in C#, you can make a WeakReference to the object. The object is still
garbage collectable. When you try to retrieve the object back via the
weak reference, if the object hasn't been collected, you get it alive
and have a reference (a strong reference) to it; if the object has been
collected, I guess there's no way to get it back.

when

【在 w**t 的大作中提到】
: don't quite understand the question...
: I didnt get it either. Question: when the destructor is called? Answer: when
: the object is out of scope? Then he asked how to bring it back (call the
: destructor??) even if the object is out of scope?

p*u
发帖数: 2454
7
most of the questions are vague. In practice it's barely somebody will try
to beat GC to an out-of-scope object, it's weird.

【在 k*k 的大作中提到】
: I think he is asking "how to bring the object back"
: in C#, you can make a WeakReference to the object. The object is still
: garbage collectable. When you try to retrieve the object back via the
: weak reference, if the object hasn't been collected, you get it alive
: and have a reference (a strong reference) to it; if the object has been
: collected, I guess there's no way to get it back.
:
: when

k*k
发帖数: 508
8
actually it's not weird to get the out-of-scope object. It's a way to
boost performance. Yes, you can always use a global scope variable to
maintain the reference, but if the object is pretty large and you are
not sure whether you will use it in the future, weakreference is very
useful. say, you have a big array of 1GB elements, you need to read
the content from disk I/O. If you need to do some operation on this
array and get some result several times but don't always need it
there, you can main

【在 p*u 的大作中提到】
: most of the questions are vague. In practice it's barely somebody will try
: to beat GC to an out-of-scope object, it's weird.

p*u
发帖数: 2454
9

~~~~~~~~~~~~~~who will put it on stack???

【在 k*k 的大作中提到】
: actually it's not weird to get the out-of-scope object. It's a way to
: boost performance. Yes, you can always use a global scope variable to
: maintain the reference, but if the object is pretty large and you are
: not sure whether you will use it in the future, weakreference is very
: useful. say, you have a big array of 1GB elements, you need to read
: the content from disk I/O. If you need to do some operation on this
: array and get some result several times but don't always need it
: there, you can main

k*k
发帖数: 508
10
i just mention a situation the weak reference can be used, the
important part is not the size of the array, but the slow disc I/O

【在 p*u 的大作中提到】
:
: ~~~~~~~~~~~~~~who will put it on stack???

相关主题
Interview question: is the following code OK?c++ dynamic cast
new一定要和delete配对吗?问个 ctor/copy ctor的问题
Forward declaration with unique_ptris pthread_mutex_destroy() required to call?
进入Programming版参与讨论
k*k
发帖数: 508
11
even if the data is stored in the heap, there's the same problem
when you don't know when to release the resource, you need to maintain
the memory you allocated in the heap. Or you have to do the costly
initialization operation time and time again.

【在 k*k 的大作中提到】
: i just mention a situation the weak reference can be used, the
: important part is not the size of the array, but the slow disc I/O

p*u
发帖数: 2454
12
in that case, keep it on heap until it's never needed. having it controled
by urself is always better than trying to beat GC.

【在 k*k 的大作中提到】
: even if the data is stored in the heap, there's the same problem
: when you don't know when to release the resource, you need to maintain
: the memory you allocated in the heap. Or you have to do the costly
: initialization operation time and time again.

k*k
发帖数: 508
13
it's not beating GC, it's taking advantage of GC
You still have the control.

【在 p*u 的大作中提到】
: in that case, keep it on heap until it's never needed. having it controled
: by urself is always better than trying to beat GC.

p*u
发帖数: 2454
14
u need to consider the opportunity cost. if GC has collected the object, u
need to load it again.

【在 k*k 的大作中提到】
: it's not beating GC, it's taking advantage of GC
: You still have the control.

k*k
发帖数: 508
15
that's the key point. You can maintain a big array in heap, but there
is often the case that you need to release it to give memory to other
modules, and you don't know when to release it. A weakreference can be
maintained until the memory is really released. You don't need to care
about when exactly to delete the array. You just need to check before
if it's still there before using it.
in other words, weak reference can minimize the chance of doing the
costly initialization.

【在 p*u 的大作中提到】
: u need to consider the opportunity cost. if GC has collected the object, u
: need to load it again.

1 (共1页)
进入Programming版参与讨论
相关主题
一道c++的考古题【C++】请问这样有没有memory leak?多谢
C++: protect dtorInterview question: is the following code OK?
请问关于c++实现singleton的问题?new一定要和delete配对吗?
含泪流血裸奔完整代码回答C++弱问题Forward declaration with unique_ptr
C++含有指针成员的类c++ dynamic cast
Help! Virtual Destructor问个 ctor/copy ctor的问题
急问:compile and build dependencyis pthread_mutex_destroy() required to call?
find bugs of c++ codesC++一个类中多个thread的问题
相关话题的讨论汇总
话题: object话题: when话题: scope话题: gc话题: destructor