由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 问一个constructor的问题
相关主题
an old c++ question还是基础题
C++相关的面经C++ Question
问个C++重新编译的问题C++ online Test 一题
A question about C++. Thanks.发面经攒rp —— Bloomberg
Please help on Effective C++ Item 11关于singleton 的面试题
请教一个C++的题目,谢谢大家谈谈看???
再帖一遍Amazon Onsite的题请问这个C++问题对吗?
C++: Q75 copy constructor 问什么用 const reference?C++ Q47: protected constructor (C39)
相关话题的讨论汇总
话题: object话题: c++话题: leak
进入JobHunting版参与讨论
1 (共1页)
g***j
发帖数: 1275
1
当constructor里面的某个member在initialization的时候fail了,到底会有memory
leak么?其他的已经完成的member到底由谁来释放?是系统自己自动释放还是需要在
catch exception之后用户写代码来自己释放?
我曾经看过说“C++里面没有partially constructed object, 只有full constructed
object”,难道不是说没有memory leak么?
为什么在Effective C++里面有这样一句话“if an exception is thrown during
construction of an object, any parts of the object that have already been
fully constructed are automatically destroyed”,这是不是说,只要throw
exception就够了,不需要在catch之后在代码里面释放? 如果这样,是不是说哪怕
fail也没有memory leak了?
总之,这个地方很confuse,谁来解释一下?
x***y
发帖数: 633
2
I have not used C++ for a long time, but I think I remeber this.
First of all, you can definitely having a try catch for constructor or
destructor
2nd of all, all the objects in the stack will be unwinded
3nd of all, the object on the heap will not.
s***0
发帖数: 117
3
If you throw during construction, you will get a leak (on whatever pointer
you allocated memory to).
You can avoid this by using smart pointers.
d**********x
发帖数: 4083
4
in ctor, whatever, it is okay
in dtor, exception must be caught internally.

【在 x***y 的大作中提到】
: I have not used C++ for a long time, but I think I remeber this.
: First of all, you can definitely having a try catch for constructor or
: destructor
: 2nd of all, all the objects in the stack will be unwinded
: 3nd of all, the object on the heap will not.

d**********x
发帖数: 4083
5
已经完成的member会回滚的,但是如果你是new出来的,你必须要手动delete才行。
另外c++的异常处理就是个渣,直接disable了得了。。

constructed

【在 g***j 的大作中提到】
: 当constructor里面的某个member在initialization的时候fail了,到底会有memory
: leak么?其他的已经完成的member到底由谁来释放?是系统自己自动释放还是需要在
: catch exception之后用户写代码来自己释放?
: 我曾经看过说“C++里面没有partially constructed object, 只有full constructed
: object”,难道不是说没有memory leak么?
: 为什么在Effective C++里面有这样一句话“if an exception is thrown during
: construction of an object, any parts of the object that have already been
: fully constructed are automatically destroyed”,这是不是说,只要throw
: exception就够了,不需要在catch之后在代码里面释放? 如果这样,是不是说哪怕
: fail也没有memory leak了?

g**w
发帖数: 969
6
如果class的成员都是纯正100% object,那么不用担心leak问题。没有指针,不要用
char* s = new char[100]; 不要用OS API, 而是包装过的类库。
s***0
发帖数: 117
7
Do you care about your application's footprint and performance?

【在 g**w 的大作中提到】
: 如果class的成员都是纯正100% object,那么不用担心leak问题。没有指针,不要用
: char* s = new char[100]; 不要用OS API, 而是包装过的类库。

1 (共1页)
进入JobHunting版参与讨论
相关主题
C++ Q47: protected constructor (C39)Please help on Effective C++ Item 11
how to detect memory leak请教一个C++的题目,谢谢
给用C++面试的同胞提个醒再帖一遍Amazon Onsite的题
Qualcomm Phone Interbview面筋,赚RPC++: Q75 copy constructor 问什么用 const reference?
an old c++ question还是基础题
C++相关的面经C++ Question
问个C++重新编译的问题C++ online Test 一题
A question about C++. Thanks.发面经攒rp —— Bloomberg
相关话题的讨论汇总
话题: object话题: c++话题: leak