可以该结构填充安全地使用由用户code吗? [英] Can the struct padding be safely used by the user code?

查看:148
本文介绍了可以该结构填充安全地使用由用户code吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假如我有一个像下面这样的结构:

Assuming I have a struct like the following:

struct Struct {
    char Char;
    int Int;
};

的sizeof(int)的大于一,编译器添加填充为字符的成员变量 - 是编译器生成的code允许改变的填充字节的值?

and sizeof( int ) is greater than one and the compiler adds padding for the Char member variable - is the compiler-generated code allowed to change the values of the padding bytes?

我的意思是,如果我使用指针运算,写一些数据填充字节的字符的成员变量周边后来又做 variable.Char = 转让是有可能,编译器生成的code也将覆盖一些填充字节?

I mean if I use pointer arithmetic and write some data into the padding bytes surrounding the Char member variable and later do variable.Char = assignment is it possible that the code generated by the compiler will also overwrite some of the padding bytes?

推荐答案

以下句子的错误:不,它不会覆盖填充字节。但是,它可能不是使用一个很好的做法。如果你需要它,有添加成员变量。

The following sentence is wrong: No, it would not overwrite the padding bytes. But it probably is not a good practice to use that. If you need it, add member variables there.

我研究的基础上,表示意见(正确)我笨:

I researched based on comments indicating (correctly) that I am stupid:

C标准有一个附件J第 J.1不确定的行为。它说,存储在结构或联合值时的填充字节的值。其含义是,编译器可以生成它想将数据写入到结构,这可能允许它的成员后,覆盖填充任何说明。

The C Standard has an "Annex J" with section J.1 Unspecified behavior. It says, "The value of padding bytes when storing values in structures or unions". The implication is that the compiler can generate whatever instructions it wants to write the data into the structure, which may allow it to overwrite padding after a member.

这篇关于可以该结构填充安全地使用由用户code吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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