结构分配保证填充也相等 [英] Does struct assignment guarantee padding to also be equal

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

问题描述

说我有一个具有2个字段的结构,而C的实现在这些字段之间也有一些填充.

Say I have a struct with 2 fields and the implementation of C that I have also has some padding between these fields.

如果我创建了该结构的两个变量并将一个变量赋给另一个变量,是否将保证填充等于?

If I create two variables of the struct and assign one to another, will the padding be guaranteed to be equal?

我知道对于大多数编译器来说都是这样(因为它们只是调用memcpy),但是我想知道标准中关于填充的规定是什么?

I know that for most compilers it would be so (because they just call memcpy), but I want to know what is specified about the padding in the standard?

这个问题的目的是,我可以使用 memcmp 来检查结构是否相等.

Intention for this question is, can I use memcmp to check equality of structs.

说我有一个编译器,该编译器发出的代码仅分配结构的所有成员,而不执行 memcpy ,这将是对结构操作的分配的有效实现吗?

Say I have a compiler which emits code that just assigns all the members of the struct instead of doing memcpy, will it be a valid implementation of the assignment of struct operation?

推荐答案

该标准在 6.2.6.1常规 的注释51:

The standard says in a note 51 of 6.2.6.1 General:

因此,例如,结构分配不需要复制任何填充位.

Thus, for example, structure assignment need not copy any padding bits.

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

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