打包结构与未打包结构:有什么区别? [英] Packed structs vs. unpacked structs: what's the difference?

查看:214
本文介绍了打包结构与未打包结构:有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




打包和拆包结构有什么区别?

-

Daniel Rudy


电子邮件地址已经过base64编码以减少垃圾邮件

解码电子邮件地址使用b64decode或uudecode -m


为什么极客喜欢电脑:看看聊天日期触摸grep make unzip

strip view finger mount fcsk more fcsk yes spray umount sleep



What is the difference between packed and unpacked structs?
--
Daniel Rudy

Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m

Why geeks like computers: look chat date touch grep make unzip
strip view finger mount fcsk more fcsk yes spray umount sleep

推荐答案

Daniel Rudy写道:
Daniel Rudy wrote:
打包和解包结构有什么区别?
What is the difference between packed and unpacked structs?



如果你有gcc,试试这个 - 在#pragma pack(1)中注释掉/行。


在我的机器/实现上 - 用pragma输出:


sizeof(s1)是10,sizeof(s2)是10

没有pragma:


sizeof(s1)是16,sizeof(s2)是12


足够的信息让你想出来?

#include< stdio.h>

#pragma pack(1)


typedef struct struct_1

{

char a;

int b;

char c;

int d;


} s1;

typedef struct struct_2

{

char a;

char c;

int b;

int d ;


} s2;

int main(无效)

{

printf(" sizeof(s1)是%d,sizeof(s2)是%d \ n,sizeof(s1),sizeof(s2));


返回0;

}


-

==============

不是一个学生

==============


If you have gcc, try this - comment out/in the #pragma pack(1) line.

On my machine/implementation - with the pragma it outputs:

sizeof(s1) is 10, sizeof(s2) is 10

without the pragma:

sizeof(s1) is 16, sizeof(s2) is 12

Enough info for you to figure it out??
#include <stdio.h>
#pragma pack(1)

typedef struct struct_1
{
char a;
int b;
char c;
int d;

} s1;
typedef struct struct_2
{
char a;
char c;
int b;
int d;

} s2;
int main(void)
{
printf("sizeof(s1) is %d, sizeof(s2) is %d\n", sizeof(s1), sizeof(s2));

return 0;
}

--
==============
Not a pedant
==============


2006-04-09,Daniel Rudy< sp******@spamthis.net>写道:
On 2006-04-09, Daniel Rudy <sp******@spamthis.net> wrote:


打包和解包结构有什么区别?


What is the difference between packed and unpacked structs?




一个是包装好了,另一个可能没包装好。根据我的知识,打包的

关键字会覆盖编译器优化

,这会导致一个或多个数据的自然边界对齐

结构中包含的元素。 包装结构将几乎

总是比其解包的兄弟拥有更小的内存占用。


见这里: http://tinyurl.com/ftv3u


这里就这些问题进行了热烈讨论。


我不知道标准 打包的但这是另一篇在Gnu C环境中解释它的文章:

http://grok2.tripod.com/structure_packing.html

-

Aspirat primo Fortuna labori。

- 维吉尔和所有好老师



One is packed and the other is, probably, not packed. The packed
keyword, to the best of my knowledge, overrides compiler optimisations
which lead to natural boundary alignment of one or more of the data
elements contained in the structure. A "packed" structure will almost
always have a smaller memory footprint than its unpacked brother.

See here: http://tinyurl.com/ftv3u

where there is a typically heated discussion on such issues.

I have no idea on the "standardness" of "packed". but here is another
article explaining it in the Gnu C environment:

http://grok2.tripod.com/structure_packing.html

--
Aspirat primo Fortuna labori.
-- Virgil, and all good teachers


Richard G. Riley写道:
Richard G. Riley wrote:


On 2006-04-09, Daniel Rudy <sp******@spamthis.net> wrote:


打包和解包结构有什么区别?

打包关键字


What is the difference between packed and unpacked structs?
The packed keyword



我不知道标准和标准。 打包。


I have no idea on the "standardness" of "packed".




这不是标准C.


-

pete



It''s not standard C.

--
pete


这篇关于打包结构与未打包结构:有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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