开发者

How to find the byte offset of an object in C++?

开发者 https://www.devze.com 2022-12-29 16:57 出处:网络
Let\'s say I create 5 objects, all from the same class.Would the byte offset of t开发者_高级运维he first object be 0?How would I find out the byte offset of the other objects?\"Byte offset\" from what

Let's say I create 5 objects, all from the same class. Would the byte offset of t开发者_高级运维he first object be 0? How would I find out the byte offset of the other objects?


"Byte offset" from what? Are you creating an array of 5 such objects? In that case, sure, the byte offset of the first one (from the start of the array) is 0; as for other objects,

static_cast<char*>(&thearray[i]) - static_cast<char*>(&thearray[0])

is the byte offset of the ith one.


Regardless of what you mean by offset, you can always examine the address of the objects yourself:

printf("%p %p", (void *) &thingOne, (void *) &thingTwo);
0

精彩评论

暂无评论...
验证码 换一张
取 消