结构成员之间的距离 [英] Distance between struct members

查看:50
本文介绍了结构成员之间的距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1。以下代码片段对两个指针使用减运操作

计算结构成员之间的距离。这是非法的,对吗?


2. s1和s2是相同结构S的类型。可以使用i3之间s1.i4

的距离推断s2.i4和s2.i3之间的距离?


感谢您的时间。

#include< stdio.h>

#include< stddef.h>


struct S {

/*...*/

int i3;

/*...*/

int i4;

};


int main(void)

{

struct S s1,s2;

ptrdiff_t distance;


距离=& s1.i4 - & s1.i3;

s1.i3 = 11;

s1.i4 = 12;

s2.i3 = 13;

s2.i4 = 14;

printf("%d,%d \ n",s2.i3,* (& s2.i3 +距离));

返回0;

}

解决方案

lovecreatesbea...@gmail.com写于10/18/07 12:47,:


1.以下代码片段使用减号OPE两个指针的配给

计算结构成员之间的距离。这是非法的,对吧?



是的。要知道为什么(或者无论如何为什么),请记住

指针算术以指向的

类型为单位运算。现在考虑

成员i3和i4之间的/*...*/可能存在的问题。如果那里的大小没有一个

的大小是int的大小的倍数,那么i3和i4被分开了

的东西和分数单位。指针算术不能
处理 - 和 - 小数部分。


2. s1和s2是同一类型struct S.可以用i3之间的s1.i4

的距离来推断s2.i4和s2.i3之间的距离吗?



是的,但是让我们收紧距离。手段。如果

你以字节为单位表示所有内容(而不是整数

或其他),一切都会好的。 C保证


(char *)& s1.i4 - (char *)& s1.i3

==(char *)& s2.i4 - (char *)& s2.i3



(char *)& s1.i3 - (char *)s2.i3


感谢您的时间。


#包括< stdio.h>

#include< stddef.h>


struct S {

/ * .. 。* /

int i3;

/*...*/

int i4;

};


int main(无效)

{

struct S s1,s2;

ptrdiff_t distance ;


距离=& s1.i4 - & s1.i3;

s1.i3 = 11;

s1 .i4 = 12;

s2.i3 = 13;

s2.i4 = 14;

printf("%d,%d \ n",s2.i3,*(& s2.i3 + distance));

返回0;

}


Eric Sosman< Er ********* @ sun.comwrites:

lovecreatesbea ... @ gmail.com写于10/18/07 12:47,:


> 1 。以下代码片段对两个指针使用减运操作来计算struct成员之间的距离。这是非法的,对吧?



是的。要知道为什么(或者无论如何为什么),请记住

指针算术以指向的

类型为单位运算。现在考虑

成员i3和i4之间的/*...*/可能存在的问题。如果那里的大小没有一个

的大小是int的大小的倍数,那么i3和i4被分开了

的东西和分数单位。指针算术不能
处理-and-a-fraction部分。



[...]


是的,但这只是一个原因,这取决于你是什么意思是

非法。


真正的原因是指针扣除会调用未定义的行为

如果两个指针指向不同的对象。见C99 6.5.6p9。这个

甚至适用于减少char *指针,这些指针不受影响

通过对齐。


(在典型的实现中) ,减法可能会给你一个有意义的结果。如果差异不是指向对象大小的倍数,那么余数是很可能被忽略了。但是当然,绝对没有保证。)


-

Keith Thompson (The_Other_Keith) ks***@mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *< http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。

- Antony Jay和Jonathan Lynn,是部长


10月18日星期四2007 15:21:21 -0700,Keith Thompson< ks *** @ mib.org>

在comp.lang.c中写道:


Eric Sosman< Er ********* @ sun.comwrites:


lovecreatesbea ... @ gmail.com写道10 / 18/07 12:47,:


1.以下代码片段对两个指针使用减号操作

计算之间的距离结构成员。这是非法的,对吧?



是的。要知道为什么(或者无论如何为什么),请记住

指针算术以指向的

类型为单位运算。现在考虑

成员i3和i4之间的/*...*/可能存在的问题。如果那里的大小没有一个

的大小是int的大小的倍数,那么i3和i4被分开了

的东西和分数单位。指针算术不能
处理-and-a-fraction部分。



[...]


是的,但这只是一个原因,这取决于你是什么意思是

非法。


真正的原因是指针扣除会调用未定义的行为

如果两个指针指向不同的对象。见C99 6.5.6p9。这个

甚至适用于减少char *指针,这些指针不受影响

通过对齐。



我不同意使用指向char的指针,特别是指向

unsigned的指针。


任意对象,包括OP的帖子中的结构,可以作为大小合适的unsigned char数组访问
。这是合法的,因此,

减去同一结构的两个成员的地址,

提供当然它们被转换为指向unsigned char的指针。


结果将是一个ptrdiff_t,表示第一个成员表示中第一个字节与第一个字节的
之间的字节数

在第二个成员的表示中。


我同意使用指向int的指针,无论是否对齐

问题,因为显然有两个不同的int成员结构是
不是相同数组的元素。


(在典型的实现中,减法可能会给你一个

有些有意义的结果。如果差异不是指向对象大小的倍数,那么其余的可能是
完全被忽略了。但当然,绝对没有保证。)



现在的问题是,任何人都可以在标准中找到措辞

(可能在多个地方分散很多)最终确定

用指针指向char或指向明确定义的签名字符的指针

,因为它是指向未签名字符的明确定义?


-

Jack Klein

主页: http://JK-Technology.Com

常见问题解答

comp.lang.c http://c-faq.com/

comp.lang.c ++ http://www.parashift.com/c++-faq-lite/

alt.comp.lang.learn.c-c ++
http:// www .club.cc.cmu.edu / ~ajo / docs / FAQ-acllc.html


1. The following code snippet uses minus operation on two pointers to
calculate the distance between struct members. This is illegal, right?

2. s1 and s2 are type of the same struct S. Can the distance of s1.i4
between i3 be used to deduce the distance between s2.i4 and s2.i3?

Thank you for your time.
#include <stdio.h>
#include <stddef.h>

struct S {
/*...*/
int i3;
/*...*/
int i4;
};

int main(void)
{
struct S s1, s2;
ptrdiff_t distance;

distance = &s1.i4 - &s1.i3;
s1.i3 = 11;
s1.i4 = 12;
s2.i3 = 13;
s2.i4 = 14;
printf("%d, %d\n", s2.i3, *(&s2.i3 + distance));
return 0;
}

解决方案

lovecreatesbea...@gmail.com wrote On 10/18/07 12:47,:

1. The following code snippet uses minus operation on two pointers to
calculate the distance between struct members. This is illegal, right?

Yes. To see why (or one reason why, anyhow), remember
that pointer arithmetic operates in units of the pointed-to
type. Now consider what might lie in the /*...*/ between
members i3 and i4. If the size of what''s there is not an
exact multiple of the size of an int, i3 and i4 are separated
by something-and-a-fraction units. Pointer arithmetic can''t
handle the -and-a-fraction part.

2. s1 and s2 are type of the same struct S. Can the distance of s1.i4
between i3 be used to deduce the distance between s2.i4 and s2.i3?

Yes, but let''s tighten up what "distance" means. If
you express everything in units of bytes (rather than ints
or whatever), all will be well. C guarantees that

(char*)&s1.i4 - (char*)&s1.i3
== (char*)&s2.i4 - (char*)&s2.i3

However, there are no guarantees about

(char*)&s1.i3 - (char*)s2.i3

Thank you for your time.
#include <stdio.h>
#include <stddef.h>

struct S {
/*...*/
int i3;
/*...*/
int i4;
};

int main(void)
{
struct S s1, s2;
ptrdiff_t distance;

distance = &s1.i4 - &s1.i3;
s1.i3 = 11;
s1.i4 = 12;
s2.i3 = 13;
s2.i4 = 14;
printf("%d, %d\n", s2.i3, *(&s2.i3 + distance));
return 0;
}


Eric Sosman <Er*********@sun.comwrites:

lovecreatesbea...@gmail.com wrote On 10/18/07 12:47,:

>1. The following code snippet uses minus operation on two pointers to
calculate the distance between struct members. This is illegal, right?


Yes. To see why (or one reason why, anyhow), remember
that pointer arithmetic operates in units of the pointed-to
type. Now consider what might lie in the /*...*/ between
members i3 and i4. If the size of what''s there is not an
exact multiple of the size of an int, i3 and i4 are separated
by something-and-a-fraction units. Pointer arithmetic can''t
handle the -and-a-fraction part.

[...]

Yes, but that''s just one reason, and it depends on what you mean by
"illegal".

The real reason is that pointer subtraction invokes undefined behavior
if the two pointers point to distinct objects. See C99 6.5.6p9. This
applies even to subtraction of char* pointers, which are not affected
by alignment.

(In a typical implementation, the subtraction is likely to give you a
somewhat meaningful result. If the the difference is not a multiple
of the size of the pointed-to object, the remainder is likely to be
quitely ignored. But there are, of course, absolutely no guarantees.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"


On Thu, 18 Oct 2007 15:21:21 -0700, Keith Thompson <ks***@mib.org>
wrote in comp.lang.c:

Eric Sosman <Er*********@sun.comwrites:

lovecreatesbea...@gmail.com wrote On 10/18/07 12:47,:

1. The following code snippet uses minus operation on two pointers to
calculate the distance between struct members. This is illegal, right?

Yes. To see why (or one reason why, anyhow), remember
that pointer arithmetic operates in units of the pointed-to
type. Now consider what might lie in the /*...*/ between
members i3 and i4. If the size of what''s there is not an
exact multiple of the size of an int, i3 and i4 are separated
by something-and-a-fraction units. Pointer arithmetic can''t
handle the -and-a-fraction part.

[...]

Yes, but that''s just one reason, and it depends on what you mean by
"illegal".

The real reason is that pointer subtraction invokes undefined behavior
if the two pointers point to distinct objects. See C99 6.5.6p9. This
applies even to subtraction of char* pointers, which are not affected
by alignment.

I disagree about using pointer to char, specifically pointer to
unsigned.

Any object, including the structure in the OP''s post, can be accessed
as a suitably sized array of unsigned char. It is legal, therefore,
to subtract the addresses of two members of the same structure,
provided of course they are cast to pointers to unsigned char.

The result will be a ptrdiff_t representing the number of bytes
between the first byte in the representation of the first member and
the first byte in the representation of the second member.

I do agree about using pointers to int, regardless of alignment
issues, because clearly two different int members of a structure are
not elements of the same array of ints.

(In a typical implementation, the subtraction is likely to give you a
somewhat meaningful result. If the the difference is not a multiple
of the size of the pointed-to object, the remainder is likely to be
quitely ignored. But there are, of course, absolutely no guarantees.)

Now the question is, can anybody find wording in the standard
(probably scattered abound in multiple places) that definitively makes
doing this with pointer to char or pointer to signed char well-defined
because it is well-defined for pointer to unsigned char?

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html


这篇关于结构成员之间的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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