가상 기본 클래스에서 상속 된 파생 클래스 개체의 메모리 레이아웃에 액세스하려고 할 때 문제가 발생했습니다.
프로그래밍 환경 : 3.19.0-32 제네릭 GNU/리눅스, x86_64에
컴파일러 :가상 기본 클래스에서 상속하는 가상 함수의 "가상 썽크"란 무엇입니까?
400c12
Derived::f()
400c3c
segment fault
: 나는이 코드를 실행하면 GCC는 4.8.4
//virtual base class
class Base {
public :
virtual void f() {
cout << "Base::f()" << endl;
}
private:
long x;
};
//derived class
class Derived : public virtual Base {
public:
virtual void f() {
cout << "Derived::f()" << endl;
}
private:
long y;
};
int main() {
typedef void (*FUNC)(void);
Derived d;
//In my machine, sizeof(long) == sizeof(pointers). My code below is neither portable nor concise. You can just read the annotation.
//dereference the first element of the first virtual function table(equals to *(vptr1->slot[0]))
cout << hex << *((long*)*((long*)(&d) + 0) + 0) << endl;
((FUNC)*((long*)*((long*)(&d) + 0) + 0))();//invoke Derived::f()
//dereference the first element of the second virtual function table(equals to *(vptr2->slot[0]))
cout << hex << *((long*)*((long*)(&d) + 2) + 0) << endl;
((FUNC)*((long*)*((long*)(&d) + 2) + 0))();//maybe Derived::f()?
return 0;
}
, 나는 "세그먼트 오류"를 가지고
그래서 실행 파일을 분해합니다. 내 터미널에서 심볼 디맹 글링
0000000000400c3c <_ZTv0_n24_N7Derived1fEv>:
400c3c: 4c 8b 17 mov (%rdi),%r10
400c3f: 49 03 7a e8 add -0x18(%r10),%rdi
400c43: eb cd jmp 400c12 <_ZN7Derived1fEv>
400c45: 90 nop
:
내가 0x400c3c의 기능 < _ZTv0_n24_N7Derived1fEv>를 발견?
> c++filt _ZTv0_n24_N7Derived1fEv
virtual thunk to Derived::f()
그리고 파생 :: F()에 가상 썽크은 무엇 왜이다 그곳에?
'가상 썽크 무엇입니까?'또는 '왜 쎄그 폴트입니까?' – xtofl
@xtofl 전자. – linvoker
왜 이것을하려고합니까? –