0

Is there any difference between using ZeroMemory(&buffer, SIZE_OF_BUFFER) and buffer = { 0 } in C++ on windows?

snzm
  • 139
  • 1
  • 1
  • 10
  • 1
    Depends on the type of `buffer` and the value of `SIZE_OF_BUFFER`. It's certainly possible to construct a program where the two have different meaning. – Igor Tandetnik Jan 13 '19 at 14:57
  • Let's say you have a `char buffer[256];` – snzm Jan 13 '19 at 15:00
  • Then the second won't compile. – Igor Tandetnik Jan 13 '19 at 15:01
  • If you do it from the get-go it will – snzm Jan 13 '19 at 15:03
  • I'm not sure what you mean by "get-to" here. I'm going by the code you've shown, not by some different code you may have in your mind. – Igor Tandetnik Jan 13 '19 at 15:04
  • The real point is which one is a Windows specific code and which one is C++ standard code? – john Jan 13 '19 at 15:08
  • From the get-go means if you do it like this `char buffer[256] = { 0 };`; it will compile. My question is if doing `char buffer[256] = { 0 };` or using `ZeroMemory(&buffer, 256);` after initializing makes any difference. – snzm Jan 13 '19 at 15:25
  • Then why don't you edit your question and say so? Anyway - the end result is the same, but the compiler may be able to produce a better-optimized code for the former. `ZeroMemory` is a macro that ultimately expands to `memset` call, which the compiler may or may not be able to inline. – Igor Tandetnik Jan 13 '19 at 15:29
  • It was pretty implicit that's what I meant when writing telling you about the char buffer since that's the only way it will compile. Anyways, thanks. – snzm Jan 13 '19 at 15:31
  • https://stackoverflow.com/questions/3038302/why-do-zeromemory-etc-exist-when-there-are-memset-etc-already – unlut Jan 13 '19 at 16:15

0 Answers0