简体   繁体   English

将结构初始化为 0

[英]Initializing a struct to 0

If I have a struct like this:如果我有这样的结构:

typedef struct
{
    unsigned char c1;
    unsigned char c2;
} myStruct;

What would be the easiest way to initialize this struct to 0?将此结构初始化为 0 的最简单方法是什么? Would the following suffice?以下就够了吗?

myStruct _m1 = {0};

or Would I need to explicitly init each member to 0?或者我是否需要将每个成员显式初始化为 0?

myStruct _m2 = {0,0};

The first is easiest( involves less typing ), and it is guaranteed to work, all members will be set to 0 [Ref 1] .第一个是最简单的(涉及较少的输入),并且保证可以工作,所有成员都将设置为0 [Ref 1]
The second is more readable.第二个更具可读性。

The choice depends on user preference or the one which your coding standard mandates.选择取决于用户偏好或您的编码标准要求的偏好。

[Ref 1] Reference C99 Standard 6.7.8.21: [参考 1]参考 C99 标准 6.7.8.21:

If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.如果花括号括起来的列表中的初始值设定项少于聚合的元素或成员,或者用于初始化已知大小数组的字符串文字中的字符少于数组中的元素,则聚合的其余部分应隐式初始化与具有静态存储持续时间的对象相同。

Good Read:好读:
C and C++ : Partial initialization of automatic structure C和C++:自动结构的部分初始化

If the data is a static or global variable, it is zero-filled by default, so just declare it myStruct _m;如果数据是静态或全局变量,默认情况下是零填充的,所以只需将其声明为myStruct _m;

If the data is a local variable or a heap-allocated zone, clear it with memset like:如果数据是局部变量或堆分配区域,请使用memset清除它,例如:

memset(&m, 0, sizeof(myStruct));

Current compilers (eg recent versions of gcc ) optimize that quite well in practice.当前的编译器(例如gcc最新版本)在实践中对其进行了很好的优化。 This works only if all zero values (include null pointers and floating point zero) are represented as all zero bits, which is true on all platforms I know about (but the C standard permits implementations where this is false; I know no such implementation).这仅在所有零值(包括空指针和浮点零)都表示为全零位时才有效,这在我所知道的所有平台上都是正确的(但C标准允许实现为假;我不知道这样的实现) .

You could perhaps code myStruct m = {};您也许可以编写myStruct m = {}; or myStruct m = {0};myStruct m = {0}; (even if the first member of myStruct is not a scalar). (即使myStruct的第一个成员不是标量)。

My feeling is that using memset for local structures is the best, and it conveys better the fact that at runtime, something has to be done (while usually, global and static data can be understood as initialized at compile time, without any cost at runtime).我的感觉是对局部结构使用memset是最好的,它更好地传达了这样一个事实,即在运行时必须做一些事情(而通常,全局和静态数据可以理解为在编译时初始化,在运行时没有任何成本) )。

See §6.7.9 Initialization:见§6.7.9 初始化:

21 If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration. 21 如果花括号括起来的列表中的初始值设定项少于集合的元素或成员数,或者用于初始化已知大小数组的字符串文字中的字符少于数组中的元素数,则集合的剩余部分应与具有静态存储持续时间的对象一样隐式初始化。

So, yes both of them work.所以,是的,他们都工作。 Note that in C99 a new way of initialization, called designated initialization can be used too:请注意,在 C99 中也可以使用一种新的初始化方式,称为指定初始化:

myStruct _m1 = {.c2 = 0, .c1 = 1};

I also thought this would work but it's misleading:我也认为这会起作用,但它具有误导性:

myStruct _m1 = {0};

When I tried this:当我尝试这个时:

myStruct _m1 = {0xff};

Only the 1st byte was set to 0xff , the remaining ones were set to 0 .只有第一个字节被设置为0xff ,其余的被设置为0 So I wouldn't get into the habit of using this.所以我不会养成使用它的习惯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM