memset的()或值初始化为零出一个结构? [英] memset() or value initialization to zero out a struct?

查看:225
本文介绍了memset的()或值初始化为零出一个结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Win32 API的编程这是典型的用C 结构 s的多个字段。通常只有几个人有有意义的价值观和所有其他人都被清零。这可以在任一两种方式来实现:

In Win32 API programming it's typical to use C structs with multiple fields. Usually only a couple of them have meaningful values and all others have to be zeroed out. This can be achieved in either of the two ways:

STRUCT theStruct;
memset( &theStruct, 0, sizeof( STRUCT ) );

STRUCT theStruct = {};

第二个变型看起来更清洁 - 这是一个一行,它没有可能被错误地输入的任何参数,并导致正在播种的错误

The second variant looks cleaner - it's a one-liner, it doesn't have any parameters that could be mistyped and lead to an error being planted.

它确实有比第一个变种什么缺点?它的变体使用,为什么?

Does it have any drawbacks compared to the first variant? Which variant to use and why?

推荐答案

这两个构造一个的非常的在它们的含义不同。第一个使用 memset的功能,其目的是的设置的内存缓冲区一定值的。第二要的初始化对象的。让我有点code的解释:

Those two constructs a very different in their meaning. The first one uses a memset function, which is intended to set a buffer of memory to certain value. The second to initialize an object. Let me explain it with a bit of code:

让我们假设你有一个具有成员的唯一的POD类型的结构

Lets assume you have a structure that has members only of POD types

struct POD_OnlyStruct
{
    int a;
    char b;
};

POD_OnlyStruct t = {};  // OK

POD_OnlyStruct t;
memset(&t, 0, sizeof t);  // OK as well

在这种情况下,写了 POD_OnlyStruct T = {} POD_OnlyStruct吨; memset的(& T公司,0,sizeof的T)并没有太大的区别,因为我们这里唯一的区别是对齐的字节被设置为零值使用 memset的的情况。既然你没有进入正常的字节,还有对你没有什么区别。

In this case writing a POD_OnlyStruct t = {} or POD_OnlyStruct t; memset(&t, 0, sizeof t) doesn't make much difference, as the only difference we have here is the alignment bytes being set to zero-value in case of memset used. Since you don't have access to those bytes normally, there's no difference for you.

在另一方面,因为你已经标记为C ++你的问题,让我们尝试另一个例子,与会员的类型从不同POD 的:

On the other hand, since you've tagged your question as C++, let's try another example, with member types different from POD:

struct TestStruct
{
    int a;
    std::string b;
};

TestStruct t = {};  // OK

{
    TestStruct t1;
    memset(&t1, 0, sizeof t1);  // ruins member 'b' of our struct
}  // Application crashes here

在这种情况下,使用前pression像 TestStruct T = {} 还是不错的,并使用 memset的它会导致崩溃。下面是如果你使用会发生什么 memset的 - 类型的对象被创建 TestStruct ,从而创造类型的对象的std ::字符串,因为它是我们的结构中的一员。接下来, memset的设置该对象 B 位于特定值的记忆,说为零。现在,一旦我们TestStruct对象超出范围,它会被销毁,当轮到它的成员的std ::是字符串b 你会看到一个崩溃,因为所有对象的内部结构被破坏了 memset的

In this case using an expression like TestStruct t = {} is good, and using a memset on it will lead to crash. Here's what happens if you use memset - an object of type TestStruct is created, thus creating an object of type std::string, since it's a member of our structure. Next, memset sets the memory where the object b was located to certain value, say zero. Now, once our TestStruct object goes out of scope, it is going to be destroyed and when the turn comes to it's member std::string b you'll see a crash, as all of that object's internal structures were ruined by the memset.

所以,现实情况是,的那些东西有很大的不同的,虽然你有时需要 memset的的整体结构,在某些情况下零,它总是重要的是要确保你知道你在做什么,而不是犯了一个错误在我们的第二个例子。

So, the reality is, those things are very different, and although you sometimes need to memset a whole structure to zeroes in certain cases, it's always important to make sure you understand what you're doing, and not make a mistake as in our second example.

我的投票 - 使用 memset的的对象的如果需要,并使用的默认的初始化 X = {} 在所有其他情况。

My vote - use memset on objects only if it is required, and use the default initialization x = {} in all other cases.

这篇关于memset的()或值初始化为零出一个结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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