Why do Microsoft code samples tend to use ZeroMemory instead of { 0 }?

Raymond Chen

If you go browsing around MSDN, you’ll find that code samples tend to call ZeroMemory explicitly rather than using “= { 0 }” notation. Why is that? To make it clearer for people who are used to other programming languages. Like it or not, a significant percentage of people who write programs for Windows do it in languages other than C and C++. Although those developers may have a basic understanding of C and C++, they don’t have all the language subtleties memorized. Compare the situation of speaking in English to a group of people where not everyone speaks the language fluently. If you’re considerate of your audience, you’re going to avoid the more esoteric grammatical constructions, the rare words, and the obscure idioms. Instead of saying, “Were it to rain, we will see that umbrellas be available,” you would use the simpler “If it rains, then we will make sure that umbrellas are available,” thereby avoiding the grammatical complexity of the implied conditional by inversion (“if”), the subjunctive of condition (“were”), the implied conclusion (“then”), and the subjunction of intention (“be”). Heck, even people who claim to know C and C++ don’t have all the language subtleties memorized. Some of them have false impressions of what “ = { 0 }” does. And who among us really has C/C++’s bizarre operator precedence rules committed to memory? Consequently, MSDN samples tend to use ZeroMemory to make it blindingly obvious what is being set to zero. One of the things we’ve learned over the years is that many people just copy/paste sample code without understanding it. If there are little things like ZeroMemory that can be done to make the intent of sample code clearer and reduce translation errors, then that’s a good thing.

If you prefer “ = { 0 }“, then go ahead and use it, secure in the knowledge that thousands of programmers aren’t going to read your code and try to translate it into Visual Basic because that’s the only language they know. But MSDN doesn’t have that luxury.

0 comments

Discussion is closed.

Feedback usabilla icon