结构体 [英] structure
问题描述
亲爱的所有人!!
在我开始讨价还价之前,我想问一下我的分析和我的
知识
>
结构a {
int raw;
char数据;
};
上面结构的大小是8字节...正确(我说的是
32位处理器)
结构a {
int raw;
char data_1;
char data_2;
};
再次上面结构的大小是8个字节。
意味着填充的结构正在进行......正确...
现在假设我必须按顺序分配4位,
然后我要做的是
strcut a {
cahr a:4;
}
现在理想情况下我应该得到上面结构的大小
4比特,但实际上我将获得1个字节,再次我运行的情况
,我不想分配额外的4位(默认情况下
struc ture正在填充),
现在我的问题来了。
首先
我将如何分配仅4位,
秒(不直接与上面相关)
我将如何保证结构与机器长度对齐?
我必须在结构中做些什么才能确保它是
分配到机器字长度。
我支持使用Strcuture填充来快速完成艺术表演....... mi正确,因为它与机器长度相符
。
>
请让我知道上面的querry
在此先感谢
Ranjeet
Dear All !!
Before i qoute my querry, I will like to qoute my analysis and my
Knowledge
Struct a {
int raw;
char data;
};
the size of above struct is 8 byte ... correct (I am talking of
32 bit procesor)
Struct a {
int raw;
char data_1;
char data_2;
};
again the size of above structure is 8 bytes.
Means the structure of padding is going on... right...
Now suppose I have to specefically allocate the 4 bits,
then What i will do is
strcut a {
cahr a:4;
}
now ideally speaking i should get the size of the above struct as
4 bits, But infact i will get the 1 byte, Again i run in the situation
that i dont want to allocate the extra 4 bits (which by default
structure is doing padding),
now my question comes.
First
how I am going to allocate the 4 bits only,
second (not directly realated to the above)
how i will assure that structure is alligned to the machine lenght ?
what i have to do in the structure that make me sure that it is
alligned to the machine word lenght.
I supposse that Strcuture padding is done to get the exectution of
Arthematic execution fast.... m i correct as it is alligned
to the machine length.
Please let me know about the above querry
Thanks In Advance
Ranjeet
推荐答案
1。通过说,
char a:4;
你不只是分配4位,相反,你要求
用于逐位分配。假设,你知道你有三个
属性,已知范围,你可以说,
struct bitwise_alloc
{
char a:4; (范围0到15)
char b:2; (范围0到3)
char c:1; (范围0到1)
char d:1;
};
大小仍然是1个字节(如果填充不存在)。没有分配
低于1个字节是可能的(对于一个对象/变量)。
2.你可以使用编译器指令(#pragma pack)来做到这一点,
但是它可能不是一般的实现。
1. by saying,
char a:4;
you are not just allocating 4 bits, instead, you are requesting
for a bit-wise allocation. Suppose, you know that you have three
attributes, with known ranges, you could say,
struct bitwise_alloc
{
char a:4; (range 0 to 15)
char b:2; (range 0 to 3)
char c:1; (range 0 to 1)
char d:1;
};
The size would still be 1 byte (if padding is not there). No allocation
below 1 byte is possible (for an object/variable).
2. you could do this using the compiler directive (#pragma pack),
but then it might not be a generic implementation.
ra *********** @ gmail.com 写道:
亲爱的所有人!!
在我讨论我的问题之前,我想问一下我的分析和我的知识
结构{
int raw ;
char数据;
};
上面结构的大小是8字节...正确(我说的是32位处理器)
结构一个{
int raw;
char data_1;
char data_2;
};
再次上面结构的大小是8个字节。
意味着填充的结构正在进行......正确...
现在假设我必须按顺序分配4位,
然后我是什么将要做的是
strcut a {
cahr a:4;
}
位域适用于* int * s
我不想分配额外的4 bits(默认情况下
结构正在填充),
您可以获得结构的大小(实际上是任何数据类型或变量)
至少是字节的倍数。
现在我的问题来了。
首先
我将如何分配4位,
我认为,分配4位内存是不可能的。但是你可以指定__attribute __((__ packed__))以及结构定义来
打包到字节对齐的分配。
second(不是直接的)如上所述)
我将如何确保结构与机器长度相对应?
我必须在结构中做些什么才能确保它与机器字对齐lenght。
__attribute __((__ align__))可能对你有帮助。
-
Mohan
我认为Strcuture填充是为了快速实现Arthematic执行的执行.... mi正确,因为它与机器长度相对应。
请让我知道上面的问题
在此先感谢
Ranjeet
Dear All !!
Before i qoute my querry, I will like to qoute my analysis and my
Knowledge
Struct a {
int raw;
char data;
};
the size of above struct is 8 byte ... correct (I am talking of
32 bit procesor)
Struct a {
int raw;
char data_1;
char data_2;
};
again the size of above structure is 8 bytes.
Means the structure of padding is going on... right...
Now suppose I have to specefically allocate the 4 bits,
then What i will do is
strcut a {
cahr a:4;
} bitfield are appropriate for *int*s
now ideally speaking i should get the size of the above struct as
4 bits, But infact i will get the 1 byte, Again i run in the situation
that i dont want to allocate the extra 4 bits (which by default
structure is doing padding),
You can get size of a structure (infact any data type or variable)
atleast in multiples of byte.
now my question comes.
First
how I am going to allocate the 4 bits only, I think, allocating a memory of 4bits is not possible. However you can
specify __attribute__((__packed__)) along with structure definition to
pack the allocation to byte aligned.
second (not directly realated to the above)
how i will assure that structure is alligned to the machine lenght ?
what i have to do in the structure that make me sure that it is
alligned to the machine word lenght.
__attribute__ ((__aligned__)) might help you.
--
Mohan
I supposse that Strcuture padding is done to get the exectution of
Arthematic execution fast.... m i correct as it is alligned
to the machine length.
Please let me know about the above querry
Thanks In Advance
Ranjeet
Mukul Pandey写道:
Mukul Pandey wrote:
1.通过说,
char a:4;
你不仅仅是相反,分配4位,您要求进行逐位分配。假设,你知道你有三个
属性,已知范围,你可以说,
struct bitwise_alloc
{
char a:4; (范围0到15)
char b:2; (范围0到3)
char c:1; (范围0到1)
char d:1;
};
大小仍然是1个字节(如果没有填充)。没有分配
低于1个字节是可能的(对于一个对象/变量)。
是的,你说的是对的。
2.你可以使用编译器指令(#pragma pack)来做到这一点,
但是然后它可能不是一般的实现。
1. by saying,
char a:4;
you are not just allocating 4 bits, instead, you are requesting
for a bit-wise allocation. Suppose, you know that you have three
attributes, with known ranges, you could say,
struct bitwise_alloc
{
char a:4; (range 0 to 15)
char b:2; (range 0 to 3)
char c:1; (range 0 to 1)
char d:1;
};
The size would still be 1 byte (if padding is not there). No allocation
below 1 byte is possible (for an object/variable).
yes you are correct what you are saying.
2. you could do this using the compiler directive (#pragma pack),
but then it might not be a generic implementation.
如果你给我一些指针,这对我有帮助
(任何网站链接/ Pseduo代码)
It will be help me if you give me some pointer on this
(Any web link / Pseduo code)
这篇关于结构体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!