结构大小指令 [英] Structure size directives

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

问题描述

由于标准C没有为程序员提供任何方式来指导编译器如何布局结构,大多数编译器都提供了一些

的方法来做这个,虽然形式不同。


微软(和lcc-win)使用

#pragma pack(1)


Gcc使用

__attribute__ {(packed)}


有没有人在其他编译器中看过其他指令?


具体来说,微软允许用一些整数打包结构

常量(例如pack(2)),但是gcc似乎不允许这样。


那里可能存在哪些其他语义差异?


我正在编写本教程的这一部分,我想制作一个表格

关于各种编译器使用的构造。请注意,在

windows下你必须知道这种东西,因为windows广泛使用了打包的

结构。

-

jacob navia

jacob at jacob point remcomp point fr

logiciels / informatique
http://www.cs.virginia.edu/~lcc-win32

解决方案

在文章< fj ********** @ aioe.org> ;, jacob navia< ja *** @nospam.orgwrote:


>由于标准C没有为程序员提供任何方式指导编译器如何布局结构,大多数编译器都提供了一些方法来执行此操作虽然形式各异。


> Microsoft(和lcc-win)使用
#pragma pack(1)


> Gcc使用
__attribute__ {(packed)}


>有没有人见过其他其他编译器中的指令?

http://techpubs.sgi.com/library/tpl/...04.html#Z26959


SGI IRIX


#pragma pack [n]


其中[n]表示可选数字1,2,4,8,或者.. []

不是语法的一部分。


这与Microsoft格式的不同之处在于没有()和

a空格后包装。

SGI包括以下注释:


o SGI强烈反对使用#pragma pack,因为它

是一个不可移植的功能,该指令的语义

可能会在将来的编译器版本中发生变化。


o结构声明必须相同

所有文件中#pragma pack指令的实例,否则

未对齐的内存访问和错误的结构成员
可以随后解除引用。


o对打包结构中的字段的引用可能效率低于对解压缩结构中字段的引用。

-

历史是一堆碎片 - Laurie Anderson


Walter Roberson写道:


文章< fj ****** ****@aioe.org> ;, jacob navia< ja *** @ nospam.orgwrote:


>由于标准C没有提供任何程序员指导编译器如何布局结构的方式,大多数编译器提供了一些方法来做到这一点,尽管形式不同。


> Microsoft(和lcc-win)使用
#pragma pack(1)


> Gcc使用
__attribute__ {(packed)}


>有没有人见过其他其他编译器中的指令?


http://techpubs.sgi.com/library/tpl/...04.html#Z26959


SGI IRIX


#pragma pack [n]


其中[n]表示可选数字1,2, 4,8或16. [*

不是语法的一部分。


这与Microsoft格式的不同之处在于没有()和一个接一个地获得
a空间。



感谢Roberson先生


>

SGI包含以下注释:


o SGI强烈反对使用#pragma pack,因为它是
是一个不可移植的功能,此指令的语义

可能会在将来的编译器版本中发生变化。


o结构声明必须遵循相同的

实例所有文件中的#pragma pack指令,否则

未对齐的内存访问和错误的结构成员

解除引用可能随之发生。



这在windows下显然是一样的。 ALl文件必须看到相同的

指令。


o对打包结构中的字段的引用可能效率较低

比对解压缩结构中的字段的引用。



这也是显而易见的。


标准C会允许这样做是多么好的

用语言定义,具有明确定义的语义。

-

jacob navia

jacob at jacob point remcomp point fr

logiciels / informatique
http:/ /www.cs.virginia.edu/~lcc-win32


2007年12月3日21:11,jacob navia写道:
< blockquote class =post_quotes>


> o对压缩结构中的字段的引用可能比对解压缩结构中的字段的引用效率低。



这也是显而易见的。


标准的C会允许这样做会有什么好处

在语言中定义,具有明确定义的语义。



垃圾。结构包装是一个实现细节,依赖这些细节的任何

程序本质上都是不可移植的。


Since standard C doesn''t provide any way for the programmer to direct
the compiler as to how to layout structures, most compilers provide some
way to do this, albeit in different forms.

Microsoft (and lcc-win) uses
#pragma pack(1)

Gcc uses
__attribute__ {(packed)}

Has anyone seen other directives in other compilers?

Specifically, Microsoft allows to pack structures with some integer
constant (pack(2) for instance), but gcc doesn''t seem to allow this.

What other semantic differences could exist there?

I am writing this part of my tutorial, and I would like to make a table
about the constructs used by the various compilers. Note that under
windows you must know this kind of stuff since windows uses packed
structures extensively.
--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32

解决方案

In article <fj**********@aioe.org>, jacob navia <ja***@nospam.orgwrote:

>Since standard C doesn''t provide any way for the programmer to direct
the compiler as to how to layout structures, most compilers provide some
way to do this, albeit in different forms.

>Microsoft (and lcc-win) uses
#pragma pack(1)

>Gcc uses
__attribute__ {(packed)}

>Has anyone seen other directives in other compilers?

http://techpubs.sgi.com/library/tpl/...04.html#Z26959

SGI IRIX

#pragma pack [n]

where [n] indicates an optional number 1, 2, 4, 8, or 16. The []
are not part of the syntax.

This differs from the Microsoft format in not having () and in having
a space after pack.
SGI includes these notes:

o SGI strongly discourages the use of #pragma pack , because it
is a nonportable feature and the semantics of this directive
may change in future compiler releases.

o A structure declaration must be subjected to identical
instances of a #pragma pack directive in all files, or else
misaligned memory accesses and erroneous structure member
dereferencing may ensue.

o References to fields in packed structures may be less efficient
than references to fields in unpacked structures.
--
"History is a pile of debris" -- Laurie Anderson


Walter Roberson wrote:

In article <fj**********@aioe.org>, jacob navia <ja***@nospam.orgwrote:

>Since standard C doesn''t provide any way for the programmer to direct
the compiler as to how to layout structures, most compilers provide some
way to do this, albeit in different forms.

>Microsoft (and lcc-win) uses
#pragma pack(1)

>Gcc uses
__attribute__ {(packed)}

>Has anyone seen other directives in other compilers?


http://techpubs.sgi.com/library/tpl/...04.html#Z26959

SGI IRIX

#pragma pack [n]

where [n] indicates an optional number 1, 2, 4, 8, or 16. The []
are not part of the syntax.

This differs from the Microsoft format in not having () and in having
a space after pack.

Thanks Mr Roberson

>
SGI includes these notes:

o SGI strongly discourages the use of #pragma pack , because it
is a nonportable feature and the semantics of this directive
may change in future compiler releases.

o A structure declaration must be subjected to identical
instances of a #pragma pack directive in all files, or else
misaligned memory accesses and erroneous structure member
dereferencing may ensue.

This is obviously the same under windows. ALl files must see the same
directive.

o References to fields in packed structures may be less efficient
than references to fields in unpacked structures.

That is obvious too.

What would be nice is that standard C would allow this to be
defined in the language, with well defined semantics.
--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32


On 3 Dec 2007 at 21:11, jacob navia wrote:

> o References to fields in packed structures may be less efficient
than references to fields in unpacked structures.


That is obvious too.

What would be nice is that standard C would allow this to be
defined in the language, with well defined semantics.

Rubbish. Packing for structs is an implementation detail, and any
program relying on such details will be inherently non-portable.


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

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