通过指针铸造创伤 [英] traumatized by pointer casting

查看:62
本文介绍了通过指针铸造创伤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读unix网络编程的第720页,

第一卷,第二版。在这个udp_write函数中

他执行以下操作:


void udp_write(char * buf,<其他一切都省略了)


struct udpiphdr * ui;

struct ip * ip;


ip =(struct ip *)buf;

ui =(struct udpiphdr *)buf;


我不敢相信我最初看到的东西。

当我检查标题时类型定义

其中一个包含结构中的位字段

而另一个结构只包含一些unsigned short。


A指向char的指针不能保证为指向struct的指针正确对齐。实际上

我不知道这甚至可以在工作期间如何,即使在最不正常的系统上也是如此。我说得对,对吧?这真的不会起作用吗?

I was reading page 720 of unix network programming,
volume one, second edition. In this udp_write function
he does the following:

void udp_write(char *buf, <everything else omitted)

struct udpiphdr *ui;
struct ip *ip;

ip = (struct ip *) buf;
ui = (struct udpiphdr *) buf;

I couldn''t believe what I was seeing at first.
When I checked the headers for their type definitions
one of them contained bit fields in the struct
and the other struct just contained some unsigned shorts.

A pointer to char is not guaranteed to be
properly aligned for a pointer to struct. In fact
I don''t see how this could even work period, even on the
most perverse of systems. I''m right, right? This really
shouldn''t work?

推荐答案

2004年7月9日星期五,j0mbolar写道:
On Fri, 9 Jul 2004, j0mbolar wrote:
无法保证指向char的指针正确对齐指向struct的指针。事实上,我甚至没有看到它甚至可以在工作期间如何工作,即使是在系统最不正常的情况下也是如此。我说得对,对吧?这真的不行吗?
A pointer to char is not guaranteed to be
properly aligned for a pointer to struct. In fact
I don''t see how this could even work period, even on the
most perverse of systems. I''m right, right? This really
shouldn''t work?




任何标准都不保证,但实际上,

假设相同的编程模型,指针是指针

(至少就其大小而言)。也就是说,32位或64位地址与任何其他32位
或64位地址相同。


-

Rich Teer,SCNA,SCSA

总裁,

Rite Online Inc.


语音:+1(250)979-1638

网址: http://www.rite-online.net


2004年7月9日17:45:54 -0700 < a href =mailto:j0 ****** @ engineer.com> j0 ****** @ engineer.com (j0mbolar)写道:
On 9 Jul 2004 17:45:54 -0700 j0******@engineer.com (j0mbolar) wrote:
指向对于指向struct的指针,不保证char正确对齐。事实上,我甚至没有看到它甚至可以在工作期间如何工作,即使是在系统最不正常的情况下也是如此。我说得对,对吧?这真的不行吗?
A pointer to char is not guaranteed to be
properly aligned for a pointer to struct. In fact
I don''t see how this could even work period, even on the
most perverse of systems. I''m right, right? This really
shouldn''t work?




对!这怎么办?


对于那些没有得到它的人:


char buf [100];

struct foo {

长a;

长b;

char c;

int d ;

};

struct foo * foo_p =(struct foo *)buf;


有buf有字节的问题-alignment,而struct foo有长期对齐



foo_p-> a不保证是可引用的。在某些拱门上它会导致一个未对齐的读取(慢速),对某些它根本就不起作用。


/ fc



Right! How can this work?

For the folks who didn''t get it:

char buf[100];
struct foo {
long a;
long b;
char c;
int d;
};
struct foo *foo_p = (struct foo *) buf;

has the problem that buf has byte-alignment, whereas struct foo has
long alignment.

foo_p->a is not guaranteed to be referenceable. On some arches it would
cause an unaligned read (slow), on some it just wouldn''t work at all.

/fc


Rich Teer< ri ******* @ rite-group.com>写道:
Rich Teer <ri*******@rite-group.com> writes:
2004年7月9日星期五,j0mbolar写道:
On Fri, 9 Jul 2004, j0mbolar wrote:
指向char的指针不能保证正确对齐指向struct的指针。事实上,我甚至没有看到它甚至可以在工作期间如何工作,即使是在系统最不正常的情况下也是如此。我说得对,对吧?这真的不行吗?
A pointer to char is not guaranteed to be
properly aligned for a pointer to struct. In fact
I don''t see how this could even work period, even on the
most perverse of systems. I''m right, right? This really
shouldn''t work?



它不是任何标准的保证,但实际上,
假设相同的编程模型,指针是指针
(至少就其大小而言)。也就是说,一个32位或64位地址与任何其他32位或64位地址的大小相同。



It''s not guaranteed by any standard, but in practical terms,
assuming the same programming model, a pointer is a pointer
(at least as far as their size is concerned). That is, one
32-bit or 64-bit address is the same size as any other 32-bit
or 64-bit address.




不,不是真的。所有指针类型都具有相同的

大小和表示形式,但是有对齐要求。

例如:


char buf [100];

char * ptr = buf + 1;


*((int *)ptr)= 42; < br $> b $ b很可能会在许多系统上造成陷阱。


-

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

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

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



No, not really. It''s common for all pointer types to have the same
size and representation, but there are alignment requirements.
For example:

char buf[100];
char *ptr = buf + 1;

*((int*)ptr) = 42;

is likely to cause a trap on many systems.

--
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.


这篇关于通过指针铸造创伤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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