由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - Question about data member offset
相关主题
pointer overflowC++ 初学者请教一个 iostream 的问题
How to check the virtual function table size?虚函数access权限改变问题
C++小插曲a question about bitwise operation
问个虚函数的作用请教for_each
我有个很傻的问题,关于function call via pointer请教 C++的一个困惑 (operator delete)
问一个C++问题:default parameter and overriding/inheritanc (转载)[合集] question about a static pointer in a member function
question about shift关于数组
0 < -1 ? A c++ questionC的问题,困惑中
相关话题的讨论汇总
话题: point3d话题: member话题: pointer话题: offset话题: vptr
进入Programming版参与讨论
1 (共1页)
j***i
发帖数: 1278
1
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,
X****r
发帖数: 3557
2
You did it wrong when trying to print out these values of
pointer-to-member type. Try this way:
(assuming sizeof(float Point3d::*) == sizeof(unsigned long) here;
if not, change 'unsigned long' to appropriate integral type)
float Point3d::*p = &Point3d::x;
std::cout << "&Point3d::x = " << *(unsigned long*)&p << std::endl;
The reason why you got '1's if you try to print out
pointer-to-member values directly is because they can be
automatically convert to boolean (true for not null, false for
null)

【在 j***i 的大作中提到】
: 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;
: };

X****r
发帖数: 3557
3
Also, peeking at pointer-to-member is not a good way to
reveal memory layout of an object. For example, whether or not
the internal value of pointer-to-member addresses are bumped by
one are compiler-dependent -- this is used to allow a null pointer
to be converted to a 'null' pointer-to-member. But there other
ways to designate a 'null' pointer-to-member, e.g. ~0, in
which case bumping by 1 becomes unnecessary.
A simpler yet more reliable way to look at memory layout of
an object is to directly

【在 j***i 的大作中提到】
: 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;
: };

j***i
发帖数: 1278
4
Thanks it works
can you explain it a little
X****r
发帖数: 3557
5
Yes I know it is confusing, so I have modified my post to explain
a little more. Just take another look.

【在 j***i 的大作中提到】
: Thanks it works
: can you explain it a little

h****8
发帖数: 599
6
大哥你为什么这么牛啊
有什么推荐书目么
我现在只是看完了effective c++, primer, 还有modern c++ design
因为不是科班出身 还请大哥指点指点

【在 X****r 的大作中提到】
: Yes I know it is confusing, so I have modified my post to explain
: a little more. Just take another look.

t****t
发帖数: 6806
7
要一边看书一边写. 只看不写的话, 看了后面, 前面很快就忘光光了

【在 h****8 的大作中提到】
: 大哥你为什么这么牛啊
: 有什么推荐书目么
: 我现在只是看完了effective c++, primer, 还有modern c++ design
: 因为不是科班出身 还请大哥指点指点

h****8
发帖数: 599
8
是的是的 不过就拿楼主的这个问题来说,要不是如此熟悉stl的结构,怎么会知道cout
会把指针转换成bool呢

【在 t****t 的大作中提到】
: 要一边看书一边写. 只看不写的话, 看了后面, 前面很快就忘光光了
t****t
发帖数: 6806
9
cout(其实是<<)对数据有定义, 对别的有没有定义一般我是查手册的

cout

【在 h****8 的大作中提到】
: 是的是的 不过就拿楼主的这个问题来说,要不是如此熟悉stl的结构,怎么会知道cout
: 会把指针转换成bool呢

1 (共1页)
进入Programming版参与讨论
相关主题
C的问题,困惑中我有个很傻的问题,关于function call via pointer
C++里get array size的问题 (转载)问一个C++问题:default parameter and overriding/inheritanc (转载)
问题question about shift
is size_t recommended for 64-bit windows porting?0 < -1 ? A c++ question
pointer overflowC++ 初学者请教一个 iostream 的问题
How to check the virtual function table size?虚函数access权限改变问题
C++小插曲a question about bitwise operation
问个虚函数的作用请教for_each
相关话题的讨论汇总
话题: point3d话题: member话题: pointer话题: offset话题: vptr