Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it preferred/okay to use structure-initialization ({...}) over memset, etc.?

Code:

WINDOWPLACEMENT wplcmt = {sizeof(WINDOWPLACEMENT)};

Looks so much cleaner than:

WINDOWPLACEMENT wplcmt;
memset(&wplcmt, 0, sizeof(WINDOWPLACEMENT));
wplcmt.length = sizeof(WINDOWPLACEMENT);

The assembly output of this thing is also pretty nice, for longer structures MSVC even uses memset instead of xor eax, eax and mov's. And from standard point of view it also looks ok. But I'm still scared about border cases where the structure is not tightly packed say #pragma pack(128), and windows suddenly decides to do a memcmp of the structure.

So is it good/bad to use such syntax? Is it good practice to use such initializations?

like image 696
Coder Avatar asked Dec 12 '10 23:12

Coder


People also ask

Can we use memset for structure in C?

memset will set the structure to all-bits-zero whereas value initialization will initialize all members to the value zero. The C standard guarantees these to be the same only for integral types, not for floating-point values or pointers. Also, some APIs require that the structure really be set to all-bits-zero.

Can you initialize variables in a struct C++?

// In C++ We can Initialize the Variables with Declaration in Structure.

How do you initialize a structure?

An initializer for a structure is a brace-enclosed comma-separated list of values, and for a union, a brace-enclosed single value. The initializer is preceded by an equal sign ( = ).


2 Answers

The second code you show,

WINDOWPLACEMENT wplcmt;
memset(&wplcmt, 0, sizeof(WINDOWPLACEMENT));
wplcmt.length = sizeof(WINDOWPLACEMENT);

is beyond horrible. Obfuscation, inefficiency, verbosity, you crammed it all into that.

The first code snippet,

WINDOWPLACEMENT wplcmt = {sizeof(WINDOWPLACEMENT)};

is, except for the obfuscation, the preferred way, unless you want to

  • spend more time needlessly writing more code,

  • have readers spending more time reading and needlessly analyzing your verbose code,

  • get less efficient execution, and

  • provide bug entry portals.

By the way, what's with the obfuscated name you used, wplcmt?

Why are you obfuscating names?

Is your question real or is it simply trolling?

Cheers & hth.,

EDIT: the question has been edited. The above questions were in response to the original title/question, "How evil is this structure allocation?". I'm leaving my answer as-is to provide context for the comments.

EDIT 2: the context has changed even further: the OP's nick changed from "Madman" to "Coder". So, while the original was about "How eveil is" normal code by "Madman", it's now about "Is it preferred..." by "Coder". Oh well, I mean, I'm not calling him "Madman" in the commentary, as it would appear now; it's what he called himself, his nick at the time.

like image 169
Cheers and hth. - Alf Avatar answered Sep 20 '22 15:09

Cheers and hth. - Alf


Use memset. Then everyone immediately sees what the code does and it's very unlikely to have any unexpected side-effects.

like image 29
ThiefMaster Avatar answered Sep 22 '22 15:09

ThiefMaster