querry与结构填充有关 [英] querry related to structure padding

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

问题描述

我想说我有一个结构

struct test {

int A;

char B [5];

int C;

};

这个上面的结构定义总是花费16个字节在

memeory中方式我们对齐成员变量,同时

为它声明一个变量。

因为变量''A''需要4个字节然后是4个字符
$由于编译器的四字节对齐特性,b $ b数组将占用另外4个字节,但剩余部分也需要4个字节
字节。

和最后四个字节乘以整数C.


我想要的是有什么方法可以在

之后开始存储整数C存储完成数组B'的最后一个元素即B [4 ],所以我

可以抑制填充。任何使内存管理器以上述方式存储在
中的程序都是最受欢迎的。

之前感谢,因为我相信我会得到无数的答案

it。

hi let''s say i have a structure
struct test {
int A;
char B[5];
int C;
};
this above structure defination always going to take 16 byte in
memeory in whatever manner we align the member variables while
declaring a variable to it .
because variable ''A'' going to take 4 byte then four charachter of
array gonna take another 4 bytes but the remaining will take also 4
bytes due to four byte alignment nature of compiler .
and last four byte by integer C .

what i want is there any way i can start storing integer C just after
the storing completion of array B''s last element i.e. B[4] , so that i
can suppress padding . Any program which make memory manager store in
the above manner is most welcome.
thanks priorly because i am sure i am gonna get innumerable answer to
it .

推荐答案

Lalatendu Das写道:
Lalatendu Das wrote:
嗨我想说我有一个结构
结构测试{
int A;
char B [5];
int C;
};
这个结构定义总是要采取在我们对齐成员变量的同时以任何方式声明一个变量,然后以任意方式记录16个字节。
因为变量A将占用4个字节,然后是4个字符的
数组将需要另外4个字节,但由于编译器的四字节对齐性质,其余的也将占用4个字节。
和最后四个字节由整数C组成。

我是什么想要有什么方法可以开始存储在存储完成阵列B'的最后一个元素即B [4]之后的整数C,这样我就可以抑制填充。任何使内存管理器以上述方式存储的程序都是非常受欢迎的。
先谢谢,因为我相信我会得到无数的答案
它。
hi let''s say i have a structure
struct test {
int A;
char B[5];
int C;
};
this above structure defination always going to take 16 byte in
memeory in whatever manner we align the member variables while
declaring a variable to it .
because variable ''A'' going to take 4 byte then four charachter of
array gonna take another 4 bytes but the remaining will take also 4
bytes due to four byte alignment nature of compiler .
and last four byte by integer C .

what i want is there any way i can start storing integer C just after
the storing completion of array B''s last element i.e. B[4] , so that i
can suppress padding . Any program which make memory manager store in
the above manner is most welcome.
thanks priorly because i am sure i am gonna get innumerable answer to
it .




不,不便携。请参阅: http://www.c-faq.com/struct/ padding.html


Robert Gamble



No, not portably. See: http://www.c-faq.com/struct/padding.html

Robert Gamble


2006年3月18日19:34:22 - 0800,Lalatendu Das < la ****** @ gmail.com>

在comp.lang.c中写道:
On 18 Mar 2006 19:34:22 -0800, "Lalatendu Das" <la******@gmail.com>
wrote in comp.lang.c:
嗨我想说我有一个结构
struct test {
int A;
char B [5];
int C;
};
这个结构定义总是要去以我们对齐成员变量的任何方式在
memeory中取16个字节,同时向其声明变量。


不,你完全错了。它将在内存中占用sizeof(struct

test)字节,不多也不少。


我使用的两个不同的编译器将是正好是7个字节。在

我使用的其他几个编译器将是9个字节,而在

其他编译器将是10.

因为变量''A ''需要4个字节然后4个字符


变量''''将占用sizeof(int)字节。在我使用的各种

编译器上,它们在1到4个字节之间变化。在''A''之后可能还是

可能不是填充字节。

数组将需要另外4个字节,但剩下的还需要4个字节到期编译器的四字节对齐性。
和整数C的最后四个字节。


不,数组''B''不会占用4个字节,另外4个字节,即8

字节。与你帖子中的其他内容不同,这是一个绝对的
的东西。数组''''将占用正好5个字节,在你的编译器和每个曾经存在过的C编译器上的



可能有填充字节在'B'之后,在下一个

成员开始之前。也许这就是你所说的。那些填充

字节不会改变''B'的大小,5 char的数组总是

的大小正好是5字节。

我想要的是有什么方法可以在存储完成数组B'的最后一个元素即B [4]之后开始存储整数C,这样我就可以抑制填充。任何使内存管理器以上述方式存储的程序都是最受欢迎的。


什么是内存管理器?没有记忆管理器。在C.

编译器允许在结构的任何成员之后插入填充

以保持对齐。在某些硬件架构上,错误的

对齐将导致硬件陷阱关闭程序。

之前感谢,因为我相信我会得到无数的答案
它。
hi let''s say i have a structure
struct test {
int A;
char B[5];
int C;
};
this above structure defination always going to take 16 byte in
memeory in whatever manner we align the member variables while
declaring a variable to it .
No, you are completely wrong. It is going to occupy sizeof(struct
test) bytes in memory, no more and no less.

On two different compilers that I use that will be exactly 7 bytes. On
several other compilers that I use that will be 9 bytes, while on
others it will be 10.
because variable ''A'' going to take 4 byte then four charachter of
Variable ''A'' is going to occupy sizeof(int) bytes. On various
compilers that I use, that varies between 1 and 4 bytes. There may or
may not be padding bytes after ''A''.
array gonna take another 4 bytes but the remaining will take also 4
bytes due to four byte alignment nature of compiler .
and last four byte by integer C .
No, the array ''B'' will not take 4 bytes and another 4 bytes, that is 8
bytes in total. Unlike everything else in your post, this is one
thing that is absolute. The array ''B'' will occupy exactly 5 bytes, on
your compiler and on every C compiler that ever existed.

There might be padding bytes after ''B'' before the start of the next
member. Perhaps that is what you are talking about. Those padding
bytes do not change the size of ''B'', an array of 5 char will always
have a size of exactly 5 bytes.
what i want is there any way i can start storing integer C just after
the storing completion of array B''s last element i.e. B[4] , so that i
can suppress padding . Any program which make memory manager store in
the above manner is most welcome.
What "memory manager"? There is no "memory manager" in C. The
compiler is allowed to insert padding after any member of a structure
to maintain alignment. On some hardware architectures, incorrect
alignment will cause a hardware trap that will shut down a program.
thanks priorly because i am sure i am gonna get innumerable answer to
it .




C没有为程序员定义任何机制来覆盖

编译器的对齐决策。您的特定编译器可能会为/ b $ b提供一些非标准机制来执行此操作。您需要在

编译器特定支持组中询问是否是这样,或者研究编译器文档中的b $ b。即使有这样的非标准机制

可用,它也会大大减慢一些

架构的程序。


-

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.contrib.andrew.cmu.edu/~a...FAQ-acllc.html



C does not define any mechanism for a programmer to override the
compiler''s alignment decisions. Your particular compiler might
provide some non-standard mechanism to do this. You need to ask in a
compiler specific support group to find out if this is so, or study
your compiler''s documentation. Even if such a non-standard mechanism
is available, it can significantly slow down the program on some
architectures.

--
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.contrib.andrew.cmu.edu/~a...FAQ-acllc.html


- ---开始PGP签名消息-----

哈希:SHA1


Lalatendu Das写道:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Lalatendu Das wrote:
hi let'比方说我有一个结构
结构测试{
int A;
char B [5];
int C;
};
这个上面的结构定义总是以无论我们的方式在记忆中占用16个字节对齐成员变量,同时向其声明变量。


不在某些平台上。可以想象,在某些平台上,无论有没有紧密包装的成员,这种结构都不会超过6个字节。

因为变量''A''会需要4个字节


说谁?

然后四个字符串将需要另外4个字节,但剩下的还需要4个字节>字节由于编译器的四字节对齐性质。


再次,谁说?

和最后四个字节整数C。


再次,谁说?


字节大小(以位为单位),int大小(以字节为单位)和对齐要求是

编译器和平台特定的,我的编译器和你的编译器可能在任何和所有细节上都不同



我想要的是有什么方法我在阵列B'的最后一个元素即B [4]的存储完成之后,可以开始存储整数C,这样我就可以抑制填充。任何使内存管理器以上述方式存储的程序都是最受欢迎的。


这将了解你的编译器如何工作,以及它给你的是什么样的选项




你/可能/通过重新安排结构内的成员来获得一定的存储空间,以便利用编译器的内置路线

要求。例如,


struct test {

int A;

int C;

char B [5];

};


实际上可能占用的空间更少,即使没有编译器选项,也不会超出您建议的

组织。

之前感谢,因为我相信我会得到无数的答案
它。
hi let''s say i have a structure
struct test {
int A;
char B[5];
int C;
};
this above structure defination always going to take 16 byte in
memeory in whatever manner we align the member variables while
declaring a variable to it .
Not on some platforms. It is conceivable that this structure will take up no
more than 6 bytes on some platforms, with or without tightly packed members.
because variable ''A'' going to take 4 byte
Says who?
then four charachter of
array gonna take another 4 bytes but the remaining will take also 4
bytes due to four byte alignment nature of compiler .
Again, says who?
and last four byte by integer C .
Again, says who?

Byte size (in bits), int size (in bytes), and alignment requirements are
compiler and platform specific, and my compiler and your compiler may differ
in any and all of the specifics.
what i want is there any way i can start storing integer C just after
the storing completion of array B''s last element i.e. B[4] , so that i
can suppress padding . Any program which make memory manager store in
the above manner is most welcome.
That would take knowledge of how your compiler works, and what sort of options
it gives you.

You /might/ gain some economy of storage by re-arranging the members within
the structure so as to take advantage of your compiler''s built-in alignment
requirements. For instance,

struct test {
int A;
int C;
char B[5];
};

might actually take less space, even without compiler options, than the
organization you suggested.
thanks priorly because i am sure i am gonna get innumerable answer to
it .



- -

Lew Pitcher


Master Code Code& JOAT-in-training |可根据要求提供GPG公钥

注册Linux用户#112576( http:/ /counter.li.org/

Slackware - 因为我知道我在做什么。

----- BEGIN PGP SIGNATURE-- ---

版本:GnuPG v1.4.2.2(GNU / Linux)

iD8DBQFEHNiWagVFX4UWr64RAmzbAKDfsX5onft4vltCCqjHxl QuJCAX + wCg9lMY

a71cSjn13xF0Klbb2khYa + A =

= BJZs

-----结束PGP签名-----


- --
Lew Pitcher

Master Codewright & JOAT-in-training | GPG public key available on request
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I''m doing.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (GNU/Linux)

iD8DBQFEHNiWagVFX4UWr64RAmzbAKDfsX5onft4vltCCqjHxl QuJCAX+wCg9lMY
a71cSjn13xF0Klbb2khYa+A=
=BJZs
-----END PGP SIGNATURE-----


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

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