z****e 发帖数: 2024 | 1 oops, i misread the question, the question is "not" be called. I though it
was "should be called".
hehe, even public virtual dtor can not be called if you copy it improperly. |
|
|
a****n 发帖数: 1887 | 3 ctor里面, 子类的虚表没建好, 不能做多态的调用, 只能用静态类型调用
dtor里面, 子类的续表已经销毁, 不能做多态的调用, 只能用静态类型调用 |
|
s*********t 发帖数: 1663 | 4 有几个connect the dots我大概说一下,未必对,大家参考
以下全部是原题
面试问题
1
Q@倒霉的大牛A:几百个文件有memory leak,怎么DEBUG?
Q@幸运的大牛B:你给我写个my_malloc,要求有A,B,C,D等功能。
2
Q@倒霉的大牛A:一程序,try, catch,还是挂了,咋地?
Q@幸运的大牛B:你给我说说dtor里为啥不能throw
Q@一般幸运的大牛C:来给我写个singleton,说说有啥好处
Q@一般幸运的大牛D:In which cases can the destructor of a class not be calle
d?
太多了,不举例了,我在看面经的时候发现太多悲剧,同一个题换个问法会导致完全没
思路,希望大家复习的时候能联系起来,看出题目到底是要问什么。
向fzzh24和jntl致敬
, |
|
f******n 发帖数: 90 | 5 yes. But why do you have to destroy it explicitly? Why not a public dtor?
cal |
|
M******q 发帖数: 94 | 6 in some variants of singleton, like the instance pointer is instantiated thr
ough "new", public dtro doesn't make a difference. you have to delete it exp
licitly anyway, if you really want to call the dtor to do some cleanup |
|
|
l*******o 发帖数: 791 | 8 来自主题: JobHunting版 - 问几道老题 1. Refer to IDE tool
2. Other part of the program throw exceptions are not handled.
3. In placement new, dtor needs to explicit called or it is not called
automatically.
4. Using smart_pointer? |
|
h*****1 发帖数: 74 | 9 来自主题: JobHunting版 - 问几道老题 Derived dtor is not called? |
|
j********x 发帖数: 2330 | 10 这回碰到一个非常nice的国人,整个过程给了合适的提示,并且在答题之后给
了详细的分析。并且这回的题目是迄今为止C++方面技术性最强的一次,当然也
并不难应付
1. C++ type cast和C type cast的区别
2. const char * p 和 char const * p 和 char * const p的区别
3. 为什么在dtor中抛出异常会导致严重后果(exceptional c++)
4. 如果降低编译期的文件依赖,(pimpl,exceptional c++)
5. 给定一个base和一个derived,derived 继承自 base,将一个base*
p static_cast和reinterpret_cast到dervied *有什么区别
6. 同上,但是现在derived使用多继承,有什么区别
7. 一个经典智力题,一个disk按直径分两边,一边白一边黑,给出若干个
pixel sensor,如果判断disk的转向
另外刚收到邮件Amazon给了offer,虽然最后一个是老印。 |
|
B********8 发帖数: 9 | 11 Are they from this page: http://drdobbs.com/cpp/184403766?
There are basically two points here:
1) LockPtr is a smartptr. It's ctor will call Mutex.aquire(); and its dtor
will call Mutex.release().
2) (This is harder) The smart usage of "volatile" and "const_cast<>" to make
user defined class variable/function thread-safe.
a) "volatile" object can NOT access normal members, such as "begin()", "
end()". It must be cast to "const" using const_cast<>
b) LockPtr will do this cast for you, when i... 阅读全帖 |
|
h*********n 发帖数: 915 | 12 为何dtor里解锁?没必要吧。
另外其实最简单的做法是在全局直接初始化:
Singleton::instance = new Singleton; |
|
S**I 发帖数: 15689 | 13 ☆─────────────────────────────────────☆
libei (Bei) 于 (Wed Jan 11 15:43:39 2012, 美东) 提到:
面试官是Google+组的,
一上来她说看到我简历上的一篇测试自动化的文章,读了一遍,感觉"very
informative",让后让我介绍一下相关经验。让我小高兴了一下。
第一题是coding,做的还算顺利,后来她评价说所有的cases都覆盖到了。可能算是过
关吧。
第二题我想复杂了,然后在她提示下才解决。自我感觉很不好。其实sort一下就差不多
了,不过我往复杂的树结构想去了。虽然树结构确实能解决这个问题,不过当时我解释
得很不清楚。反正很不爽。
最后瞎聊时间,她说我提到的测试自动化实践和Google内部的基本完全一样blahblah。
。。,不过我觉得这点也算不上加分吧,是个人进google一段时间后都能学会。就怕她
觉得我想问题太复杂,直接negative。
大家有啥建议想法??
☆─────────────────────────────────────☆
peking2 (myfac... 阅读全帖 |
|
b*****k 发帖数: 26 | 14 The IntegerSet Class
the IntegerSet class represents the set of integers ranging from 1 to 10,000
,000. This class has a default ctor and a dtor. It also contains two
additional public methods: Acquire and Relinquish.
The Acquire method picks an available value from the set, removes it fro mm
the set, and returns this value to the callers. If the set of available
integers is empty, the Acquire method returns 0.
The Relinquish method adds an available integer value back to the set. If
the value i... 阅读全帖 |
|
d**********x 发帖数: 4083 | 15 读书,大型项目。开源的项目也有很多优秀的,比如webkit之类。
感觉如果以后不接触大型cpp项目的话,也就这样了,没必要学太多。。现在我感觉cpp
的未来不是很光明
像我当年那么脑残粉的确实不多。。以下是cpp深入学习的书单,传说中的书单
Effective cpp, More Effective cpp
Exceptional cpp, More Exceptional cpp
Imperfect cpp
Modern cpp Design
cpp Template
cpp Inside Object Model
the cpp Programming Language
cpp Standard Library
Generic Programming and the STL
侯捷的 深入理解stl
另外参考03版标准。
下面是前几天newsmth cpp版贴的面试题目....顺便也贴一下
1. str的内容是什么?
char* a = "
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrs... 阅读全帖 |
|
i******e 发帖数: 273 | 16 这里的mutex是static类型的,你不用在dtor中销毁,当整个程序结束操作系统会clean
up static memory的。
static 类成员是由SmartPointer类对象所有实例所共享的,它只有一个copy, 所以不
论哪个实例的assignment operator 或 copy ctor 被调用, 只能等mutex。它的作用
就是保护reference counter在多线程异步访问时避免出现data inconsistency。 |
|
d**********x 发帖数: 4083 | 17 in ctor, whatever, it is okay
in dtor, exception must be caught internally. |
|
d**e 发帖数: 6098 | 18 ☆─────────────────────────────────────☆
CAIWU (Cai) 于 (Mon Jul 16 01:46:48 2012, 美东) 提到:
看了c++complete reference 那本书
☆─────────────────────────────────────☆
mxf (mxf) 于 (Mon Jul 16 01:59:30 2012, 美东) 提到:
继续写code
☆─────────────────────────────────────☆
CAIWU (Cai) 于 (Mon Jul 16 03:07:17 2012, 美东) 提到:
有些理论光写小程序学不来
想找点进阶的书看看
练习code就那些leetcode上的
基本只用到c的东西
☆─────────────────────────────────────☆
bokertov (早上好) 于 (Mon Jul 16 05:39:35 2012, 美东) 提到:
effective C++讲得比较深入
☆────────────... 阅读全帖 |
|
l*****a 发帖数: 14598 | 19 基本C++指什么程度呢?
我知道virtual function,vTable,vPtr.
还知道virtual dtor
你看够吗? |
|
w*****d 发帖数: 105 | 20 1,what is abi and api, give some example
api: usually means interfaces of a library or class.
abi: this is means the binary conversion rules for different compilers or
platforms, such as arguments order in stack for a function call.
2, what happens when exception is thrown?
in C++, the following operations are carried out:
1)unwind the current stack, call any dtor for local vars if necessary, until
there is a catch block that can handle the exception obj;
2)if there is no such catch block in the... 阅读全帖 |
|
a****r 发帖数: 87 | 21 like xunhuan119 said. instances allocated on heap have a global scope, and
instances allocated on stack have local scope. When the instances are out of
scope, their dtor will clean up the allocated memory. The global scoped
instances will never out of scope. |
|
P*****f 发帖数: 2272 | 22 In emdedded environment, the resource is quite limited, e.g., the memory.
Make a virtual dtor will add a ptr to vtbl in each class instance. This
comsumes lots of memory if there are thousands of instances. |
|
t****t 发帖数: 6806 | 23 You are usually not supposed to throw exception in destructor.
If a dtor throw exception in the process of stack unrolling
(i.e. processing another exception), the program calls a pre-defined
function and exits immediately, there's no cure for this.
However, throwing exception in constructor is perfectly legal and will
often be met, e.g. if constructor calls new and new throws out bad_alloc. |
|
p*u 发帖数: 2454 | 24 那你的ctor和dtor在哪个thread里? |
|
t****t 发帖数: 6806 | 25 constructor or destructor can call virtual member functions, but the version
they called is the one defined in its own class (i.e. will not be overrided
). in other words, you can't do dynamic binding in ctor/dtor.
if you really have to, do a delayed initialization in a separate member (e.g
. init()) |
|
j****g 发帖数: 597 | 26 如果dtor的时候释放,我是不是可以理解为push_back或者operator[]用的是传值参数
,存入的实际是原来对象的一个副本?
不然的话如果有其他对象引用被存入的对象的话就会有问题。 |
|
x*********h 发帖数: 25 | 27 if converted from some memory buffer, the ctor has no place to be called.
but if your class has no use of ctor, dtor, maybe it could be feasible.
if you have a huge of data i/o and wish to improve performance by decreasing
the number of memory allocation and free, maybe consider memory pool or
object pool.
what is your i/o data rate in your program? could you give some hint about
that? |
|
x*********h 发帖数: 25 | 28 Hi, I have a question about operator delete, dtor.
define a class like below,
class TestDelete{
public:
virtual ~TestDelete(){
std::cout << "TestDelete::~TestDelete" << std::endl;
}
void operator delete (void* p){
std::cout << "TestDelete::operator delete()" << std::endl;
}
// void operator delete[] (void* p){
// std::cout << "TestDelete::operator delete()" << std::endl;
// }
};
then, write statements like :
TestDelete* p = NULL;
delete p;
I found |
|
p****o 发帖数: 1340 | 29 for g++, if you comment out the dtor, the delete will be called.
i don't know why. |
|
d****2 发帖数: 6250 | 30
dtor is object-specific. must check first.
now after free object-specific space, free the space the object itself in
heap (space usage sizeof(object)), that will check NULL pointer there so no
need to check before calling delete.
Basically it is implementation-specific based on delete semantic in C++. |
|
t****t 发帖数: 6806 | 31 今天我有点无聊, 就说说这个temp object的事情
如果完全不优化, 实际上这个程序的结果应该是
1 5 4
可以用g++ -fno-elide-constructors 得到这个结果.
调用的顺序是
A::A(&a); // A a
A::A(&c, a); // foo(a) (1)
A::A(&temp1, c); // A(c) (2)
A::A(&ab, temp1); // A ab=... (3)
A::A(&temp2, ab); // return ab (4)
A::A(&d, temp2); // A d=.... (5)
dtor就省掉了.
这5个里面, (1)和(5)是不能省的, 这两个都不是temp object.
1 3 2的结果是把两个temp object省了.
1 2 1的结果是把(3)也省了, 这个叫做named return value optimization. VC8事实上
有这个功能,如果你用release编译,那结果就是1 2 1.
标准是这样写的 |
|
P********e 发帖数: 2610 | 32 我FT,标准什么时候说,(3)可以省了?
你帖的,第一句,(2)可以省略,第二句话(4)可以省略
ab is a variable, without (3), it's un_initialized local variable, which
doesnot make sense to me.
今天我有点无聊, 就说说这个temp object的事情
如果完全不优化, 实际上这个程序的结果应该是
1 5 4
可以用g++ -fno-elide-constructors 得到这个结果.
调用的顺序是
A::A(&a); // A a
A::A(&c, a); // foo(a) (1)
A::A(&temp1, c); // A(c) (2)
A::A(&ab, temp1); // A ab=... (3)
A::A(&temp2, ab); // return ab (4)
A::A(&d, temp2); // A d=.... (5)
dtor就省掉了.
这5个里面, (1)和(5)是不能省的, |
|
X****r 发帖数: 3557 | 33 Some base classes are not supposed to be instantiated at all.
nonvirtual |
|
|
w*******e 发帖数: 285 | 35 【 以下文字转载自 JobHunting 讨论区 】
发信人: windforce (大怪兽), 信区: JobHunting
标 题: 请问关于c++实现singleton的问题?
发信站: BBS 未名空间站 (Sun Nov 16 06:56:48 2008), 转信
我觉得目标和方法是不是差不多这样?
public static的getinstance function返回唯一的instance。
declare private的ctor,copy ctor和copy assignment防止私自创建和copy。
只在第一次调用getinstance的时候才创建对象,所谓的laziness。
在创建instance的时候用mutex同步,但是以后就不用同步提高效率。
我写了一个如下,我觉得运行起来还可以,大家看看有什么问题吗?
class Singleton
{ public:
static Singleton* GetInstance();
~Singleton() {cout << "Dtor"<< endl;};
vo |
|
c*****t 发帖数: 1879 | 36 #include
using namespace std;
class A
{
private:
int m_value;
public:
A (int v) : m_value (v) { std::cout << "ctor" << std::endl; }
~A () { std::cout << "dtor" << std::endl; }
int getValue () { return m_value; }
};
#define SIZE 10
int main ()
{
// init
A* array = (A*)new char[sizeof (A) * SIZE];
for (int i = 0; i < SIZE; ++i)
new (&array[i]) A (5);
for (int i = 0; i < SIZE; ++i)
std::cout < |
|
r*********r 发帖数: 3195 | 37 如果问题是关于 object model 的 ( copy ctor, dtor, v-table etc), 1 很好, 但是
有点过时.
2. 是百科书, 不一定都能记住.
如果是在一些 c 的问题上不清楚 ( bit op, multi-array, c string etc), 有本 c
programming FAQs 很好. |
|
r*********r 发帖数: 3195 | 38 unnamed semaphore 应该不涉及系统资源. destroy 就是把里面的 mutex, condition
variable, count 清零, 禁止再对其用p/v操作.
这种有可能涉及资源的, 用 c++ RAII 最安全, dtor 自动释放 whatever needed to
be released. |
|
c*****t 发帖数: 1879 | 39 Can't use realloc, because if resizing isn't successful, the old
memory would be freed before you have any chances calling dtors.
So, you will just have to use malloc/new to create a new memory
space, and then call placement new with copy constructor on each
and individual array element.
You can read std::vector code yourself to see how that gets
implemented. |
|
t****t 发帖数: 6806 | 40 这是个老题了
其实增加一个constructor, 如果和旧的不冲突, 是有可能不需要重编译的
另外几个确实都需要
这个问题实际上的问的是C++类代码的interface和implementation
增加一个数据成员相当于改变了sizeof
把dtor改成virtual相当于改变了delete的调用方式
增加一个有缺省值的参数把成员的signature改了(缺省值不是signature的一部分)
但是增加一个普通的ctor, 如果没有代替系统产生的缺省ctor, 那相当于增加一个非
virtual的普通成员(ctor不可能是virtual), 这个可以不用recompile. 如果代替了,
那就是改动了. |
|
s**9 发帖数: 207 | 41 我这么理解对不对。用个具体的例子来说:
Setting: C.h define a class C. C.c implements C. main.c includes C.h and
creates instances of C.
Question: If some changes are made to C.h. does recompiling main.c generate
a new main.o file?
如果这道题问的就是上面的情况,那么add data, change dtor to virtual会改变main
.o (所以需要recompile main.c). add a parameter with default value 也会改变
main.o (所以也需要recompile main.c). 这和上面thrust给的答案一致,但和lanti的
不一致?
add a constructor。有的情况main.c 不编译,有的时候编译但main.o会变,有的编译
且main.o不变。所以是否要recompile main.c 不定。
刚在精华区看到了这道题 |
|
S*********g 发帖数: 5298 | 42 XYZ. Otherwise, you run into trouble if abc is called in XYZ's destructor |
|
t****t 发帖数: 6806 | 43 C++ exception的资源释放一般是通过自动变量的destructor自动做的, 就是说不管什
么理由离开了这个scope, 那么auto variable的dtor就会被调用. 这个就相当于
finally. |
|
b***y 发帖数: 2799 | 44 FAQ 24.12 Should the assignment operator be implemented by using placement n
ew and the copy constructor?
It's a well-known trap!
It is tempting to avoid duplicate code for the assignment operator for class
X by trying something like this.
#include
using namespace std;
X& X::operator= (const X& rhs)
{
if (this != &rhs) {
this->~X();
new(this) X(rhs);
}
return *this;
}
There are many problems with this approach. rhs will be sliced whenever it i
s not of type X, and the dtor-ne |
|
t****u 发帖数: 8614 | 45 1,显然是错的,
第一 ctor Word() 没有body,不可能compile过的,
第二 Word(int, float=3.14, short), short 没有default value.
2, 错的,当然可以那么做。没有default ctor而有带parameter的ctor多了。
3,错的,当然可以用没有dtor()的class。
4,错的,你provide了ctor Word(),不过没有body(implementation)而已。
5,就是我的解释,如果专指这个题的话,那也是错的,因为第二个float有default
value,那么short也要有。如果泛指就是对的。 |
|
t****t 发帖数: 6806 | 46 首先容器里不能放auto_ptr. 一定会出问题. STL container对元素的要求是
assignable. 基于这个理由, auto_ptr和valarray都不能作为STL container的元素
其次就是ownership要注意了. 一般指针没有ownership的说法, 但你自己心里要有数,
ownership是不是这个container. 如果是, 那么在erase, pop, pop_back, dtor等地方
要记得delete. 如果不是, 那就所有的地方都没有delete.
放shared_ptr就没有问题了. shared_ptr是assignable的, 所以不用你自己管. |
|
t****t 发帖数: 6806 | 47 1 E
2 A (well D is also right since getchar is not recognized...)
3 B
4 D
5 C
6 B
7 E
8 E
9 D (actually it's not strictly correct: you may call Account::get_name() if
you define it. Yes you can define a pure virtual function. Although in this
case, it's probably not what you want, since in dtor, it will not call
overrided get_name().)
10 A (but I think E is also correct. In fact, A::getBaseNum HIDES Base::
getBaseNum, and A::baseNum HIDES Base::baseNum (which is private anyway).)
11 A
12 E
13 C |
|
k*****2 发帖数: 252 | 48 俺来抛块砖
D(A如果理解成这个类里的inline无法更改ctor和dtor,那个人感觉A也对)
D
C
??? |
|
w****m 发帖数: 146 | 49 delete will invoke dtor |
|
B*******g 发帖数: 1593 | 50 class A(){....}
int main()
{
A & a = A(); //--- 1
A * p = & A(); //--- 2
p->doSomething();
return 0;
}
Q:
1. 1,2 有没有用到copy ctor (我觉得都没有,VC下跑了一下也没看到copy ctor被调
用)
2. 对于a,p dtor是何时被调用的。 (a是main() 结束的时候,但是p似乎是在
statement 2结束
就调用了,这点我不能理解.而且在这之后p指向的对象的成员变量还都合法存在) |
|