I find it difficult to understand what is better in C ++:
I use struct to manage clients in the message queue, the structure is as follows:
typedef struct _MsgClient { int handle; int message_class; void *callback; void *private_data; int priority; } MsgClient;
All of them are objects of POD.
Now I have an array of these structures where I store my clients (I use an array for memory limits, I need 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 about SO that using memset is bad in C ++ and that I would rather use a constructor for my structure. I figured out something like this:
typedef struct _MsgClient { int handle; int message_class; void *callback; void *private_data; int priority;
... will eliminate the need for memset . But I'm afraid that when initializing foo, the constructor will be called 32 times, instead of optimizing it as a simple zero from the memory received by the array.
What is your opinion on this?
I just found this: Can a member structure be null-init from the constructor initializer list without calling memset? , is this appropriate in my case (this is different: I have an array, not one instance of the structure)?
Also, according to this post , adding a constructor to my structure will automatically convert it to a structure other than POD, is this correct?
source share