由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - some c++ question.
相关主题
问题:vptr/vtable for virtual function & vptr/vtable forC++虚函数动态绑定的含义?
A C++ puzzle for me一道 memset in C++的题
我有个很傻的问题,关于function call via pointer请教 C/C++ 指向多维数组的指针的问题
How to check the virtual function table size?为什么derived object没有vptr?
C++小插曲一个很诡异的ifstream问题,求助~~
A C++ compiler related interview questionQuestion about memset() and memmove() size limit
腆着脸在问一道请教一个linux下的POSIX timer的问题。
师傅们都出来看看吧,我也问个C++返回值问题。数组问题
相关话题的讨论汇总
话题: vptr话题: memset话题: a2话题: test话题: a1
进入Programming版参与讨论
1 (共1页)
g**********1
发帖数: 1113
1
If I use memset in the constructor of a class as follows
#include
#include
class A {

public:
A()
{
memset(this, 0,sizeof(*this));
}
virtual void test()const
{}
private:
};
int main()
{
A A1;
A * A2=new A;
A1.test(); //not crash here.
A2->test();//crash here.
return 0;
}
I know memset will set VPTR to be NULL but I do not know why If I do not use
new, A1.test() can be called properly.
z****e
发帖数: 2024
2
怎么又来了,
不是不crash就没事,因为undefined, 什么都可能发生。
这个红猪侠解释的比较好。
我的一点愚见,因为vptr木有了,dereference 一个木有了的指针显然不行。
但是 concrete object 可能没事,因为不动用dynamic binding, 不查vtable,不调vptr。故而,一个普通函数在栈上调用而已。
这个要大牛来确认。我不知道C++ under the hood,因为木有时间看了。。。。
面试被问这种,就认栽了。
g**********1
发帖数: 1113
3
I was asked in the interview but only asked me why memset is not good. I
told them the problem with the VPTR but I try to compile some code with
concrete object. It works but after I get it address and defer it with *, it
does not works. I believe some happens with the concrete call. It should
not call the virtual through VPTR. I wonder what it calls.
g**********1
发帖数: 1113
4
It seems that A1.test() calls the test() at compile time and (&A1)->test()
calls at run time. I am not sure.
Thank you guys.
z****e
发帖数: 2024
5
我不是说过了吗?
一个是static binding, 不用调vptr,没事
一个是dynamic binding,要用vptr,但是vptr木有了,。。。

【在 g**********1 的大作中提到】
: It seems that A1.test() calls the test() at compile time and (&A1)->test()
: calls at run time. I am not sure.
: Thank you guys.

h****8
发帖数: 599
6
A1.test()和A2->test()调用的都是同一个函数virtual test()
不同的是,对这个函数的地址的解析。
对于A1.test(),在编译的时候编译器已经确定了函数地址
对于A2->test(),需要在runtime,通过vptr来找到函数地址。然而vptr已经是空指针,
所以runtime出错

it

【在 g**********1 的大作中提到】
: I was asked in the interview but only asked me why memset is not good. I
: told them the problem with the VPTR but I try to compile some code with
: concrete object. It works but after I get it address and defer it with *, it
: does not works. I believe some happens with the concrete call. It should
: not call the virtual through VPTR. I wonder what it calls.

g**********1
发帖数: 1113
7
Thank you and got it. :)
1 (共1页)
进入Programming版参与讨论
相关主题
数组问题C++小插曲
关于c++的constructor的面试题A C++ compiler related interview question
关于C++中 extern "C"的问题。腆着脸在问一道
一个c++问题 (转载)师傅们都出来看看吧,我也问个C++返回值问题。
问题:vptr/vtable for virtual function & vptr/vtable forC++虚函数动态绑定的含义?
A C++ puzzle for me一道 memset in C++的题
我有个很傻的问题,关于function call via pointer请教 C/C++ 指向多维数组的指针的问题
How to check the virtual function table size?为什么derived object没有vptr?
相关话题的讨论汇总
话题: vptr话题: memset话题: a2话题: test话题: a1