C++:使用 memset 还是结构构造函数?什么是最快的?
我无法确定 C++ 中哪个更好:
我使用一个结构来管理消息队列中的客户端,该结构如下所示:
typedef struct _MsgClient {
int handle;
int message_class;
void *callback;
void *private_data;
int priority;
} MsgClient;
所有这些都是 POD 实体。
现在,我有一个由这些结构组成的数组,用于存储我的客户端(我使用数组来限制内存,我必须限制碎片)。所以在我的课堂上,我有这样的内容:
class Foo
{
private:
MsgClient _clients[32];
public:
Foo()
{
memset(_clients, 0x0, sizeof(_clients));
}
}
现在,我到处读到,在 C++ 中使用 memset 是不好的,而且我宁愿为我的结构使用构造函数。 我想了这样的事情:
typedef struct _MsgClient {
int handle;
int message_class;
void *callback;
void *private_data;
int priority;
// struct constructor
_MsgClient(): handle(0), message_class(0), callback(NULL), private_data(NULL), priority(0) {};
} MsgClient;
...将消除对memset的需要。但我担心的是,当初始化 foo 时,结构构造函数将被调用 32 次,而不是将其优化为简单的从数组占用的内存中清零。
您对此有何看法?
我刚刚发现这个: 可以在不调用 memset 的情况下从构造函数初始值设定项列表中对成员结构进行零初始化吗?,它适合我的情况吗(这是不同的:我有一个数组,而不是该结构的单个实例)?
另外,根据 这篇文章,向我的结构会自动将其转换为非POD结构,是吗?
I'm having trouble figuring out which is better in C++:
I use a struct to manage clients in a message queue, the struct looks like this:
typedef struct _MsgClient {
int handle;
int message_class;
void *callback;
void *private_data;
int priority;
} MsgClient;
All of these being POD entities.
Now, I have an array of these structs where I store my clients (I use an array for memory constraints, I have to limit fragmentation). So in my class I have something like this:
class Foo
{
private:
MsgClient _clients[32];
public:
Foo()
{
memset(_clients, 0x0, sizeof(_clients));
}
}
Now, I read here and there on SO that using memset is bad in C++, and that I'd rather use a constructor for my structure.
I figured something like this:
typedef struct _MsgClient {
int handle;
int message_class;
void *callback;
void *private_data;
int priority;
// struct constructor
_MsgClient(): handle(0), message_class(0), callback(NULL), private_data(NULL), priority(0) {};
} MsgClient;
...would eliminate the need of the memset
. But my fear is that when foo is initialized, the struct constructor will be called 32 times, instead of optimizing it as a simple zero out of the memory taken by the array.
What's your opinion on this?
I just found this: Can a member struct be zero-init from the constructor initializer list without calling memset? , is it appropriate in my case (which is different: I have an array, not a single instance of the structure)?
Also, according to this post, adding a constructor to my structure will automatically convert it into a non-POD structure, is it right?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在一致的实现上,使用空成员初始值设定项对构造函数初始值设定项列表中的数组进行值初始化是完全有效的。对于数组成员,这将产生每个数组元素的成员零初始化的效果。
编译器应该能够使其非常高效,并且您无需向
struct
添加构造函数。例如
On a conforming implementation, it's perfectly valid to value-initialize an array in the constructor initializer list with an empty member initializer. For your array members, this will have the effect of zero-initializing the members of each array element.
The compiler should be able to make this very efficient and there's no need for you to add a constructor to your
struct
.E.g.
即使在 C++ 中,只要您了解自己在做什么,您也可以自由地
memset
。关于性能 - 查看哪种方式真正更快的唯一方法是在发布配置中构建程序,然后在反汇编器中查看生成的代码。
使用 memset 听起来比每个对象初始化要快一些。然而编译器有可能生成相同的代码。
You can you
memset
freely even in C++, as long as you understand what you are doing.About the performance - the only way to see which way is really faster is to build your program in release configuration, and then see in the disassembler the code generated.
Using
memset
sounds somewhat faster than per-object initialization. However there's a chance the compiler will generated the same code.