由买买提看人间百态

topics

全部话题 - 话题: vptr
1 2 下页 末页 (共2页)
s*******u
发帖数: 1855
1
【 以下文字转载自 JobHunting 讨论区 】
发信人: siriusliu (天狼), 信区: JobHunting
标 题: 问题:vptr/vtable for virtual function & vptr/vtable for virtual inheritance, are they the same?
发信站: BBS 未名空间站 (Wed Nov 5 00:58:56 2008)
we know that both virtual function and virtual inheritance have vptr
pointing to vtable. My question is: do they use one SAME table/ptr, or TWO separate table/ptr?
For ex:
s*******u
发帖数: 1855
2
还有,当一个class有vptr指向vtable时,他的derived class,继承他的member时,是
否继承这个vptr?
感觉应该不继承,但是如果这样,无论如何算不出为什么上个例子中,sizeof(MI) can
be 36.
thanks!
P********e
发帖数: 2610
3
来自主题: Programming版 - 为什么derived object没有vptr?
class A
{
int i;
public:
virtual void f(){}
};
class DerivedA: public A
{
int j;
public:
virtual void f(){}
virtual void ff(){}
};
DerivedA da;
为什么da里面没有一个vptr是DerivedA?
da 的layout是 :
[
i
A.vptr
j
// 为什么这里没有DerivedA.vptr???
]
P********e
发帖数: 2610
4
来自主题: Programming版 - 为什么derived object没有vptr?
我大概知道为什么没有额外的vptr in da
不过这样的情况,是怎么处理的?
A a = da;
因为, a 和 da里面 vptr地址是不一样的.这样怎么能保证integrity of subobject in
da.
P********e
发帖数: 2610
5
来自主题: Programming版 - 为什么derived object没有vptr?
谢谢
那这个问题还是不太清楚:
A a = da;
上面这个怎么保证,da里面的subobject的完整性,下面这样理解对吗?
compiler要做:
a.i = da.i;
a.vptr = A:vptr;???这样吗?

For every virtual function of A, its position in A's vtable
is the same as the position of the same or overridden function
of DerivedA in DerivedA's vtable.
s****j
发帖数: 43
6
来自主题: Programming版 - vptr 到底是在第一个还是最后一个
我看到资料说vptr不是排在member第一个就是最后一个,但是一般是最后一个,今天一个
朋友很肯定的跟我说,是第一个,我当时没有直接跟他argu,于是先来问问....
X****r
发帖数: 3557
7
来自主题: Programming版 - 为什么derived object没有vptr?
For single non-virtual inheritence like this, da's vptr points
to DeriveA's vtable, which is compatible with A's vtable.
X****r
发帖数: 3557
8
来自主题: Programming版 - 为什么derived object没有vptr?
对。
A a = da;就是调用隐含定义的A::A(const A& other),this为&a,
other为da里的a子对象。
这个copy constructor和A的其他所有constructor一样,把this的vptr指向
A的vtable。
P********e
发帖数: 2610
9
来自主题: Programming版 - 为什么derived object没有vptr?
我开始没把他们联系起来,觉得子类的vptr好像不对。。。
S**Y
发帖数: 136
10
来自主题: JobHunting版 - 问几个跟C++有关的面试题
let's discuss this..
about vptr and vtable
Since every class has only one copy of vptr and vtable, the vptr and vtable
should be static? ->data area?
How is vptr and vtable implemented indeed?

base;
,
l*******o
发帖数: 791
11
来自主题: JobHunting版 - C++ Q21: size of virtual table
B, 因为每个含有若干virtual function的class只有一个vptr。这个“含有”包括即使
本类没有virtual function,但父类有,也就是继承下来的。所以A有一个VTABLE和一
个VPTR,B也有自己的一套。因为VPTR是在调用constructor时被insert进入的,所以
sizeof(B)就只是一个void * 的大小。在我的mac上是8
z****e
发帖数: 2024
12
来自主题: Programming版 - some c++ question.
我不是说过了吗?
一个是static binding, 不用调vptr,没事
一个是dynamic binding,要用vptr,但是vptr木有了,。。。
Q******e
发帖数: 85
13
来自主题: JobHunting版 - virtual table存在memory的哪块啊?
Dynamic binding is decided by the actual object type. suppose Base* ptr =
new Derived. ptr actually point to a Derived object and compiler knows it.
Through ptr,we can get virtual table pointer vptr (the code segment address)
of Derived object. When running, the virtual function can be found through
vptr with virtual function index (offset). In that way, we have dynamic
binding.
g*******y
发帖数: 1930
14
来自主题: JobHunting版 - 问几个跟C++有关的面试题
我来挣点包子~

when delete a base pointer that actually points to a derived obj;
a destructor should be made virtual if that class intends to serve as a base;
compiler generate a virtual table(addresses of VFs) for each class that
contains VF; it secretly put the VPtr(address of Vtable) in the object.
When you call VF through ptr/ref, compiler first get the value of VPtr to
access Vtable, then it can get the address of the function from the Vtable,
because the table offset of the VF is known.
候: 决定的?
z***e
发帖数: 5393
15
来自主题: JobHunting版 - 一道 C++ 的题。
interesting, why do you think it doesn't work for vptr? vptr is nothing just
a pointer, kind like:
class A
{
void* pVtbl;
int value;
public:
A(int v):value(v) {};
};
then:
A temp(123);
A* pBuffer = (A*)malloc(sizeof(A)*500);
for(int i=0;i<500;++i)
{
mempcy(pBuffer++, &temp, sizeof(A));
}
这个问题应该是让你模仿vector去实现类似功能(不是直接让你用vector)

这个array还是没法用
t****t
发帖数: 6806
16
来自主题: Programming版 - How to check the virtual function table size?
this is of course implementation defined... the standard doesn't even say
there exists a vtable or vptr.
but,
1. layout of the object is implementation defined, but *usually* vptr is the
first entry.
2. ???
3. you can use sizeof(any_pointer_to_function) instead.
4. this is indeed a big problem.
W*********g
发帖数: 409
17
来自主题: Programming版 - How to check the virtual function table size?
不可行。首先vptr在哪不清楚,微软是放在前面,但是更直接的方法是在后面,如果是
这样,对于derived class来说vptr就在中间了,这还没有考虑multiple inherit(多
个vtbl) 和virtual inherit(virtual base class有自己的vtbl)。
其次vtbl里还有其他东西,比如typeid。有些实现会把this pointer adjustment
offsets放在vtbl里。
r*******y
发帖数: 290
18
来自主题: Programming版 - 问个虚函数的作用
because virtual function is bound at runtime
non-virtual func is bound at compilation time
in other words, virtual call is something like this:
(vptr+some address offset)->func, the vptr belongs to the actual class
non-virtual call is something like class->func, class is determined after
compilation

+
d****n
发帖数: 130
19
effective C++专门有一节讲这个问题。

can
b******k
发帖数: 1
E*****7
发帖数: 128
z****e
发帖数: 2024
22
来自主题: Programming版 - some c++ question.
怎么又来了,
不是不crash就没事,因为undefined, 什么都可能发生。
这个红猪侠解释的比较好。
我的一点愚见,因为vptr木有了,dereference 一个木有了的指针显然不行。
但是 concrete object 可能没事,因为不动用dynamic binding, 不查vtable,不调vptr。故而,一个普通函数在栈上调用而已。
这个要大牛来确认。我不知道C++ under the hood,因为木有时间看了。。。。
面试被问这种,就认栽了。
g**********1
发帖数: 1113
23
来自主题: Programming版 - some c++ question.
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.
h****8
发帖数: 599
24
来自主题: Programming版 - some c++ question.
A1.test()和A2->test()调用的都是同一个函数virtual test()
不同的是,对这个函数的地址的解析。
对于A1.test(),在编译的时候编译器已经确定了函数地址
对于A2->test(),需要在runtime,通过vptr来找到函数地址。然而vptr已经是空指针,
所以runtime出错

it
j***i
发帖数: 1278
25
来自主题: Programming版 - Question about data member offset
I read the insider the c++ Object model 3.6
It says
class Point3d {
public:
virtual ~Point3d();
// ...
protected:
static Point3d origin;
float x, y, z;
};
The physical offset of the three coordinate members within the class
layout are, respectively, either 0, 4, and 8 if the vptr is placed at the
end or 4, 8, and 12 if the vptr is placed at the start of the class. The
value returned from taking the member's address, however, is always bumped
up by 1. Thus the actual values are 1, 5,
z****e
发帖数: 2024
26
我还不是太懂你的意思。
我也知道b的静态类型是Base*,但是这个和虚函数返回类型有什么联系呢?
难道,虚函数返回值的类型,不是决定于虚函数本身的签名么?
而是决定于调用该虚函数的指针的类型吗?
既然已经找到了derived的虚指针,查虚表,发现要返回一个derived*,就返回,这难道
有问题?
vptr并没有因为b的静态类型是Base*,而改变vptr本身的行为啊?
P********e
发帖数: 2610
27
suppose we have:
class A
{
public:
virtual ~A(){}
void x(){}
virtual void y(){}
};
A * a = new A();
a->x();
a->y();
因为x不在v table里面.
在runtime的时候,linker怎么知道y在vptr,而x不在vtable里面
也就是,他们怎么知道a->y();要转换成 (*a->vptr[1])();
a->x()就不需要呢

发帖数: 1
28
来自主题: Military版 - 半导体这种材料只能两种状态吗

=====================
C++ 工程实践(4):二进制兼容性
原创 2011年03月09日 10:46:00 标签:c++ /library /interface /mfc /class /编译
器 22578
陈硕 (giantchen_AT_gmail)
Blog.csdn.net/Solstice
本文主要讨论 Linux x86/x86-64 平台,偶尔会举 Windows 作为反面教材。
C/C++ 的二进制兼容性 (binary compatibility) 有多重含义,本文主要在“头文件和
库文件分别升级,可执行文件是否受影响”这个意义下讨论,我称之为 library (主
要是 shared library,即动态链接库)的 ABI (application binary interface)。至
于编译器与操作系统的 ABI 留给下一篇谈 C++ 标准与实践的文章。
什么是二进制兼容性
在解释这个定义之前,先看看 Unix/C 语言的一个历史问题:open() 的 flags 参数的
取值。open(2) 函数的原型是
int open(cons... 阅读全帖
a****n
发帖数: 1887
29
来自主题: JobHunting版 - 一道 C++ 的题。
对了一半, 只分配了内存块, 如果class不是POD(有vptr)或者含有动态内存指针, 这个array还是没法用
H*M
发帖数: 1268
30
来自主题: JobHunting版 - a virtual table question by JP morgan
不是很确定,但是intuitively没有理由有多个vptr啊
a*****p
发帖数: 189
31
Understanding the Linux kernel
Linux device drivers
另外有时间再看:
Linux kernel development
Programming for embedded systems
Embedded Linux primer : a practical, real-world approach
汇编就看ARM的吧,网上资料很多。关键别死看书,多结合一些C,C++和编译器特性和
具体一些技术,比如汇编是如何实现VPTR的,Call Stack等,如果面试的时候你能解释
到汇编级,还是很能impress你的interviewer的。

Device
l*****a
发帖数: 14598
32
来自主题: JobHunting版 - 问一个c++ virtual base class的问题
每个类有自己的Vtable,然后constructor会生成自己的Vptr指向自己的Vtable。
j***i
发帖数: 1278
33
来自主题: JobHunting版 - C++ Q52: (C6)
It is not clear, seem only choice is e
but the statement is wrong, it is allowed to all get_name, but it will not
get the result you want, calling virtual function in the destructor and
constructor the vptr will not always point to corret class
r***u
发帖数: 241
34
来自主题: JobHunting版 - Apple的一些C++概念题

应该是2次,先从vptr查表,再调用表中存的函数指针
都是8
v***n
发帖数: 5085
35
题目主要有
哲学
编译
C++
数据结构
算法
编程(C)
第一题就是哲学
问为什么要OO,OO和实际世界之间的关系
然后改问class, object, instance之间的联系与区别
第二题问编译
几个文件里都有static的变量那么哪个先哪个后被编译
第三题问C++
vtable以及在有继承时候的vtable以及vptr
第四题问数据结构
stl里面的set vector list
实现方法 优缺点 有效存储空间比
第五题算法
问基于以上几个ds的一些搜索 删除 插入的时间复杂度
第六题编程
编写一个linked list测节点数量的小程序
n******n
发帖数: 49
36
来自主题: JobHunting版 - 攒人品 报BB面经
昨天早晨电面BB,no luck.
主要是对印度人的口音还是有些不习惯,而且不明原因昨天有一段时间电话背景噪音非
常大,非常影响面试。
题目如下:
1. 打印Hello World字符串。这题看起来简单,但是如果概念不清,很容易弄错或者绕
很久。
回忆了一下,回头又编了一次,代码应该是
void foo(char** a){
*a = "hello world";
}
int main(){
char* c;
foo(&c);
printf("%s",c);
return 0;
}
这题的trap就在char* c之后,只是声明了c但并没有定义c。如果用debugger trace,就
会发现&c = 0x0012ff40但是c是一个bad pointer,即里面是garbage value, 这时候如
果直接用foo(c),编译器会提示错误: the variable "c" can't be used without
definition. 这和下面这段程序是一个道理:
void bar(int a){
cout << a << end... 阅读全帖
z***e
发帖数: 5393
37
1. effective C++没必要看,我其实没遇到问C++细节的,一般了解virtual table/
vptr就足够了。而且effective c++ 3rd edition里面太多TR01等内容,不会有人问。
career 150绝对是必读。
2. 如果你离开书就说不清楚,那就是你没真正搞懂。你举个例来看看?
3. 根据公司和职位,这个很不好说,光看一个java没用,你要根据整个Job
description来判断。
我加过那个俱乐部的,你要推荐?那里也没啥意思,现在没几个人发言了。

种情况要不要投简历:
是俱乐部成员的话 能不能推荐我一
d********t
发帖数: 9628
38
VTable是一个class一个,VPtr是每个object一个对吧?
s****a
发帖数: 528
39
Wrong, still have only one Vptr to the virtual table

three
l*****a
发帖数: 14598
40
来自主题: JobHunting版 - 现在招个会C++的人真难
基本C++指什么程度呢?
我知道virtual function,vTable,vPtr.
还知道virtual dtor
你看够吗?
g********r
发帖数: 89
41
来自主题: JobHunting版 - Pure Storage面经
已挂。
如果某Class有很多个instance,每个instance都会有一个vptr占用memory,请问有没
有办法改进?
j********x
发帖数: 2330
42
来自主题: JobHunting版 - Pure Storage面经
项改进什么?
一个vptr的空间也要抠?profiling的结果是什么?
那为什么要用虚函数?
看这面试题,这公司好像比较不入流啊
a****r
发帖数: 87
43
来自主题: JobHunting版 - Pure Storage面经
个人理解:如果这个class的vptr真的是overhead. 那就没有必要把设置virtual
function了。这样可以省去不必要的memory.
d**********n
发帖数: 2031
44
来自主题: Joke版 - 攒人品 报BB面经 (转载)
【 以下文字转载自 JobHunting 讨论区 】
发信人: nancylin (nancylin), 信区: JobHunting
标 题: 攒人品 报BB面经
关键字: phone interview Bloomberg
发信站: BBS 未名空间站 (Thu Oct 28 15:47:29 2010, 美东)
昨天早晨电面BB,no luck.
主要是对印度人的口音还是有些不习惯,而且不明原因昨天有一段时间电话背景噪音非
常大,非常影响面试。
题目如下:
1. 打印Hello World字符串。这题看起来简单,但是如果概念不清,很容易弄错或者绕
很久。
回忆了一下,回头又编了一次,代码应该是
void foo(char** a){
*a = "hello world";
}
int main(){
char* c;
foo(&c);
printf("%s",c);
return 0;
}
这题的trap就在char* c之后,只是声明了c但并没有定义c。如果用debugger trace,就
会发现&c = 0x0012ff40但是c是... 阅读全帖
t****t
发帖数: 6806
45
来自主题: Programming版 - Re: VC里面的stl支持是不是很弱?
You are not supposed to CALL a ctor directly.
you should use initializer to "call" it.
i.e., if you don't write a initializer
PointGeneric3D::PointGeneric3D(T xx, T yy, T zz)
{ ... }
then compiler will automatically generate a initializer as if you wrote
PointGeneric3D::PointGeneric3D(....) : PointGeneric()
{ ... }
this implicit initializer will call ctor of base class and set the vptr
to the vtbl of base class.
after this implicit initializer, the ctor of derived class is called and
th
t****t
发帖数: 6806
46
来自主题: Programming版 - Re: VC里面的stl支持是不是很弱?
vptr and vtbl is the basis of the whole virtual function system,
I assume you know it...

I know you wanted to call that! And I thought you know how to call that...
your way of calling base class ctor is INCORRECT and initializer should
be used. And initializer CAN be used to call ctor with parameter, just
write the parameter down, what do you expect... and pptwo already showed
you how to do that.
it probably looks fine, but it is incorrect, period.
base class ctor with parameter should not be c
q*****g
发帖数: 72
47
来自主题: Programming版 - 一个c++问题 (转载)
如果有virtual funciton,“通常”会在头上加个RTTS和vptr
o*o
发帖数: 404
48
来自主题: Programming版 - copy constructor问题。
A default copy constructor does not do a Bitwise Copy under the following
circumstances
* When the class contains a member object of a class for which a copy
assignment operator exists
* When the class is derived from a base class for which a copy
assignment operator exists
* When the class declares one or more virtual functions (we must not
copy the vptr address of the right-hand class object, since it might be a
derived class object)
* When the class inherits from a virtual bas
z***e
发帖数: 5393
49
来自主题: Programming版 - How to check the virtual function table size?
Is there any way to get vptr or check the size of virtual function table?
thx.
z***e
发帖数: 5393
50
来自主题: Programming版 - C++小插曲
?这有什么问题?
p->foo() 相当于 p->vptr[1](),本来就是放的这个地址。
1 2 下页 末页 (共2页)