将结构映射到内存块.... [英] Mapping a struct to a memory block....
问题描述
我有一个多字段结构和一个内存块(使用malloc创建),
包含结构化数据。如果我将结构映射到内存块,我是否保证结构字段将被正确填充或者我是否必须担心编译器填充结构?
示例:
typedef struct
{
int16 field1; // 2个字节
int32 field2; // 4个字节
} st;
// memblock正好包含6个字节
st * var =(st *) memblock;
barcaroller写道:
我有一个多-field struct和一个内存块(使用malloc创建)
包含结构化数据。如果我将结构映射到内存块,我是否保证结构字段将被正确填充或者我是否必须担心编译器填充结构?
示例:
typedef struct
{
int16 field1; // 2个字节
int32 field2; // 4个字节
} st;
// memblock正好包含6个字节
st * var =(st *) memblock;
在许多系统上,由于对齐填充,sizeof(struct st)将为8。
对齐和填充不是标准化,所以做你的建议是
不是个好主意。
-
Ian Collins。
" barcaroller" < ba ********* @ music.netwrites:
我有一个多字段结构和一个内存块(使用malloc创建)
包含结构化数据。如果我将结构映射到内存块,我是否保证结构字段将被正确填充或者我是否必须担心编译器填充结构?
允许编译器在结构成员之间插入填充
或在结构结束之后。 C语言没有提供
a方式来防止或控制这个(虽然你的实现可能是b $ b)。
typedef struct
{
int16 field1; // 2个字节
int32 field2; // 4个字节
} st;
这些类型的标准类型名称是int16_t和
int32_t,在< stdint.hin C99实现中声明
支持他们。
-
int main(void){char p [] =" ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\ n",* q =" kl BIcNBFr.NKEzjwCIxNJC" ;; int i = sizeof p / 2; char * strchr(); int putchar(\
); while(* q){i + = strchr(p,* q ++) - p; if(i> =(int)sizeof p)i- = sizeof p-1; putchar(p [i] \
);}返回0;}
" barcaroller" < ba ********* @ music.netwrites:
我有一个多字段结构和一个内存块(使用malloc创建)
包含结构化数据。如果我将结构映射到内存块,我是否保证结构字段将被正确填充或者我是否必须担心编译器填充结构?
[...]
您必须担心编译器填充结构。
请参阅comp.lang.c FAQ中的问题2.12,< http://www.c-faq.com/> ;.
-
Keith Thompson(The_Other_Keith) ks***@mib.org < http://www.ghoti .net / ~kst>
圣地亚哥超级计算机中心< *< http://users.sdsc.edu/~kst>
我们必须做点什么。这是事情。因此,我们必须这样做。
- Antony Jay和Jonathan Lynn,是部长
I have a multi-field struct and a memory block (created using malloc) that
contains structured data. If I map the struct to the memory block, am I
guaranteed that the struct fields will be filled properly or do I have to
worry about the compiler padding the struct?
Example:
typedef struct
{
int16 field1; // 2 bytes
int32 field2; // 4 bytes
} st;
// memblock contains exactly 6 bytes
st* var = (st*) memblock;
barcaroller wrote:I have a multi-field struct and a memory block (created using malloc) that
contains structured data. If I map the struct to the memory block, am I
guaranteed that the struct fields will be filled properly or do I have to
worry about the compiler padding the struct?
Example:
typedef struct
{
int16 field1; // 2 bytes
int32 field2; // 4 bytes
} st;
// memblock contains exactly 6 bytes
st* var = (st*) memblock;
On many systems, sizeof(struct st) will be 8 due to alignment padding.
Alignment and padding aren''t standardised, so doing what you propose is
not a good idea.
--
Ian Collins.
"barcaroller" <ba*********@music.netwrites:
I have a multi-field struct and a memory block (created using malloc) that
contains structured data. If I map the struct to the memory block, am I
guaranteed that the struct fields will be filled properly or do I have to
worry about the compiler padding the struct?Compilers are allowed to insert padding between structure members
or after the end of a structure. The C language doesn''t provide
a way to prevent or control this (although your implementation
might).
typedef struct
{
int16 field1; // 2 bytes
int32 field2; // 4 bytes
} st;The standard type names for these types would be int16_t and
int32_t, declared in <stdint.hin C99 implementations that
support them.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
"barcaroller" <ba*********@music.netwrites:I have a multi-field struct and a memory block (created using malloc) that
contains structured data. If I map the struct to the memory block, am I
guaranteed that the struct fields will be filled properly or do I have to
worry about the compiler padding the struct?[...]
You have to worry about the compiler padding the struct.
See question 2.12 in the comp.lang.c FAQ, <http://www.c-faq.com/>.
--
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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
这篇关于将结构映射到内存块....的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!