字节地址算术辩论 [英] Byte Address Arithmetic Debate

查看:69
本文介绍了字节地址算术辩论的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



此新闻组目前有一个主题为:


"如何计算2个地址之间的差异?"


该线程用于计算两个

内存地址之间的距离(以字节为单位)。显然,只有当地址将

引用到同一对象(或基础对象等)的元素或成员时才能这样做。


John Carson我提出了两种不同的方法。


我不同意约翰的解决方案,约翰不同意我的意见。因此,

我想在这里展示他们并看看观众的想法。


首先,我们将从一个简单的POD类型开始:


struct MyPOD {

int a;

double b;

void * c;

短d;

bool e;

int f;

};


给定这种类型的对象,我们将计算b与b之间的距离(以字节为单位)

。成员和e会员。


我自己的方法如下:


reinterpret_cast< char const volatile *>(& obj.e)

- reinterpret_cast< char const volatile *>(& obj.b)


John的方法如下:

reinterpret_cast< long unsigned>(& obj.e)

- reinterpret_cast< long unsigned(& obj.b);


为了保护我自己的方法:


(1)任何字节地址都可以准确地存储在char *中。


攻击John方法:


(1)标准并不需要存在足够大的整数类型

以容纳内存地址。 br />
(2)即使存在这样的类型,减法也不需要产生

正确答案(例如,如果每个整数1代表半个字节,或者四分之一
一个字节)。


当然,看到_I_如何开始这个帖子,它可能有点偏颇

towa我自己的目的,但我希望我们客观地了解这一点。


-


Frederick Gotham

解决方案

On Sun,2006年11月19日20:05:11 GMT in comp.lang.c ++,Frederick Gotham

< fg ** *****@SPAM.comwrote,


>给定这种类型的对象,我们将计算距离,以字节为单位,
" b"成员和e会员。



#include< cstddef>

offsetof(MyPOD,e) - offsetof(MyPOD,b)


David Harmon:


On Sun,2006年11月19日20:05:11 GMT in comp.lang.c ++, Frederick Gotham
< fg ******* @ SPAM.comwrote,


>>鉴于此类型的对象,我们将计算b之间的距离,以字节为单位。成员和e会员。



#include< cstddef>

offsetof(MyPOD,e) - offsetof(MyPOD,b)



我会改写这个问题:


给出两个指针形式的内存地址 - 指针类型

可能不同 - 计算它们之间的字节距离。

指针指的是同一个对象的部分。


-


Frederick Gotham




Frederick Gotham写道:


David Harmon:


On Sun,2006年11月19日20:05:11 GMT in comp.lang.c ++,Frederick Gotham

< fg ******* @ SPAM。 comwrote,


>给定这种类型的对象,我们将计算b与b之间的距离,以字节为单位。成员和e会员。



#include< cstddef>

offsetof(MyPOD,e) - offsetof(MyPOD,b)




我会重新解释这个问题:


给出两个指针形式的内存地址 - 指针类型

可能不同 - 计算它们之间的距离(以字节为单位)。

指针指的是同一个对象的部分。


-


Frederick Gotham



不是我故意想在阁楼上做一个痛苦,但你用什么来表示

呢?

这与抵消不一样。


结构测试

{

int n;

int i;

};


测试instance.n和instance.i之间的距离以字节为单位

假设没有涉及填充。记住:假设==从你和我做出一个

ASS。



There is a thread currently active on this newsgroup entitled:

"how to calculate the difference between 2 addresses ?"

The thread deals with calculating the distance, in bytes, between two
memory addresses. Obviously, this can only be done if the addresses refer
to elements or members of the same object (or base objects, etc.).

John Carson and I proposed two separate methods.

I disagree with John''s solution, and John disagrees with mine. Therefore,
I''d like to present them both here and see what the audience thinks.

Firstly, we shall start off with a simple POD type:

struct MyPOD {
int a;
double b;
void *c;
short d;
bool e;
int f;
};

Given an object of this type, we shall calculate the distance, in bytes,
between the "b" member and the "e" member.

My own method is as follows:

reinterpret_cast<char const volatile*>(&obj.e)
- reinterpret_cast<char const volatile*>(&obj.b)

John''s method is as follows:

reinterpret_cast<long unsigned>(&obj.e)
- reinterpret_cast<long unsigned(&obj.b);

In defence of my own method:

(1) Any byte address can be accurately stored in a char*.

In attack of John''s method:

(1) The Standard doesn''t necessitate the existance of an integer type
large enough to accomodate a memory address.
(2) Even if such a type exists, the subtraction need not yield the
correct answer (e.g. if each integer 1 represents half a byte, or a quarter
of a byte).

Of course, seeing as how _I_ started this thread, it may be a little biased
toward my own ends, but I hope we get to the bottom of this objectively.

--

Frederick Gotham

解决方案

On Sun, 19 Nov 2006 20:05:11 GMT in comp.lang.c++, Frederick Gotham
<fg*******@SPAM.comwrote,

>Given an object of this type, we shall calculate the distance, in bytes,
between the "b" member and the "e" member.

#include <cstddef>
offsetof(MyPOD, e) - offsetof(MyPOD, b)


David Harmon:

On Sun, 19 Nov 2006 20:05:11 GMT in comp.lang.c++, Frederick Gotham
<fg*******@SPAM.comwrote,

>>Given an object of this type, we shall calculate the distance, in bytes,
between the "b" member and the "e" member.


#include <cstddef>
offsetof(MyPOD, e) - offsetof(MyPOD, b)


I''ll rephrase the question:

Given two memory addresses in the form of pointers -- pointer types which
may be different -- calculate the distance in bytes between them. The
pointers refer to parts of the same object.

--

Frederick Gotham



Frederick Gotham wrote:

David Harmon:

On Sun, 19 Nov 2006 20:05:11 GMT in comp.lang.c++, Frederick Gotham
<fg*******@SPAM.comwrote,

>Given an object of this type, we shall calculate the distance, in bytes,
between the "b" member and the "e" member.

#include <cstddef>
offsetof(MyPOD, e) - offsetof(MyPOD, b)



I''ll rephrase the question:

Given two memory addresses in the form of pointers -- pointer types which
may be different -- calculate the distance in bytes between them. The
pointers refer to parts of the same object.

--

Frederick Gotham

Not that i''m trying deliberately to be a pain in the attic, but what do
you mean by between them?
Thats not the same as offset.

struct test
{
int n;
int i;
};

The distance in bytes between a test instance.n and instance.i would be
zero assuming no padding is involved. Remember: To assume == makes an
ASS out of U and ME.


这篇关于字节地址算术辩论的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆