'memset' vs '= {0}'
-
(more of a curiousity question than anything) I've seen both this:
SHELLEXECUTEINFO sei = {0};
and this:
SHELLEXECUTEINFO sei;
memset(&sei, 0, sizeof(sei));and even: (but this is Win32 specific I think)
SHELLEXECUTEINFO sei;
::ZeroMemory(&sei, sizeof(sei));Which is "better"? Is there *really* any difference? Personally, I've always used the first one, but I've seen many people using the second one, and was wondering if I should switch over. :-D Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. [Rich Cook]
-
(more of a curiousity question than anything) I've seen both this:
SHELLEXECUTEINFO sei = {0};
and this:
SHELLEXECUTEINFO sei;
memset(&sei, 0, sizeof(sei));and even: (but this is Win32 specific I think)
SHELLEXECUTEINFO sei;
::ZeroMemory(&sei, sizeof(sei));Which is "better"? Is there *really* any difference? Personally, I've always used the first one, but I've seen many people using the second one, and was wondering if I should switch over. :-D Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. [Rich Cook]
I would have thought the first two would be the same and they nearly are. Under VS.NET 2003, however, with the first, the compiler first stores a 0 at the first DWORD then does a stosd for the remaining while memset does a stosd for the entire structure. The result is a very slight, but measurable, performance penalty for the first. Don't know why it's being so dumb but there you are. ZeroMemory just uses memset. (I believe it's a macro.) It was for use when NT actually ran on non-Intel processors.
-
(more of a curiousity question than anything) I've seen both this:
SHELLEXECUTEINFO sei = {0};
and this:
SHELLEXECUTEINFO sei;
memset(&sei, 0, sizeof(sei));and even: (but this is Win32 specific I think)
SHELLEXECUTEINFO sei;
::ZeroMemory(&sei, sizeof(sei));Which is "better"? Is there *really* any difference? Personally, I've always used the first one, but I've seen many people using the second one, and was wondering if I should switch over. :-D Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. [Rich Cook]
Well (as I found out the hard way) the
MY_STRUCT foo = {0};
is only usable (in terms of zeroing out the whole structure) on MS compilers. So if you want to write correct, portable code you should use memset (AFAIK). ¡El diablo está en mis pantalones! ¡Mire, mire! Real Mentats use only 100% pure, unfooled around with Sapho Juice(tm)!
-
Well (as I found out the hard way) the
MY_STRUCT foo = {0};
is only usable (in terms of zeroing out the whole structure) on MS compilers. So if you want to write correct, portable code you should use memset (AFAIK). ¡El diablo está en mis pantalones! ¡Mire, mire! Real Mentats use only 100% pure, unfooled around with Sapho Juice(tm)!
What sort of error does this produce on non-Microsoft compilers?
-
Well (as I found out the hard way) the
MY_STRUCT foo = {0};
is only usable (in terms of zeroing out the whole structure) on MS compilers. So if you want to write correct, portable code you should use memset (AFAIK). ¡El diablo está en mis pantalones! ¡Mire, mire! Real Mentats use only 100% pure, unfooled around with Sapho Juice(tm)!
MY_STRUCT foo = {0};
Jim Crafton wrote: is only usable (in terms of zeroing out the whole structure) on MS compilers Was the compiler(s) compliant with the C or C++ standard? I have not read the standard for C in years but if I remember it right then the above initialization should work on all compliant compilers. If you are sure I am wrong about this then send a reply and I'll go look it up to verify what the standard has to say on the subject of initializing Aggregate Types. Trust in the code Luke. Yea right!
-
I would have thought the first two would be the same and they nearly are. Under VS.NET 2003, however, with the first, the compiler first stores a 0 at the first DWORD then does a stosd for the remaining while memset does a stosd for the entire structure. The result is a very slight, but measurable, performance penalty for the first. Don't know why it's being so dumb but there you are. ZeroMemory just uses memset. (I believe it's a macro.) It was for use when NT actually ran on non-Intel processors.
Thanks! I figured ZeroMemory and memset were basically the same. Weird that = {0} works different on VS.NET. I'm still using VC6, so that doesn't affect me now. Joe Woodbury wrote: stosd wtf? :wtf: I prefer to wear gloves when using it, but that's merely a matter of personal hygiene [Roger Wright on VB] Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. [Rich Cook]
-
Well (as I found out the hard way) the
MY_STRUCT foo = {0};
is only usable (in terms of zeroing out the whole structure) on MS compilers. So if you want to write correct, portable code you should use memset (AFAIK). ¡El diablo está en mis pantalones! ¡Mire, mire! Real Mentats use only 100% pure, unfooled around with Sapho Juice(tm)!
Thanks! So far, I've only done dev work in Windows, using DevStudio, so I haven't run into anything like that. I prefer to wear gloves when using it, but that's merely a matter of personal hygiene [Roger Wright on VB] Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. [Rich Cook]
-
Thanks! I figured ZeroMemory and memset were basically the same. Weird that = {0} works different on VS.NET. I'm still using VC6, so that doesn't affect me now. Joe Woodbury wrote: stosd wtf? :wtf: I prefer to wear gloves when using it, but that's merely a matter of personal hygiene [Roger Wright on VB] Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. [Rich Cook]
Assembly instruction: Store String Data: DWord http://www.online.ee/~andre/i80386/Opcodes/STOS-STOSB-STOSW-STOSD.html[^]
-
Well (as I found out the hard way) the
MY_STRUCT foo = {0};
is only usable (in terms of zeroing out the whole structure) on MS compilers. So if you want to write correct, portable code you should use memset (AFAIK). ¡El diablo está en mis pantalones! ¡Mire, mire! Real Mentats use only 100% pure, unfooled around with Sapho Juice(tm)!
Jim Crafton wrote: s only usable (in terms of zeroing out the whole structure) on MS compi No, that syntax for initializing a struct is inherited from C. It sets the first member to 0, then by definition sets all remaining members to 0. So if you write
= {1}
that sets the first member to 1, and all remaining members to 0. --Mike-- "So where does that leave us? Well, it leaves us right back where we started, only more confused than before." -- Matt Gullett Ericahist | Homepage | RightClick-Encrypt | 1ClickPicGrabber -
Jim Crafton wrote: s only usable (in terms of zeroing out the whole structure) on MS compi No, that syntax for initializing a struct is inherited from C. It sets the first member to 0, then by definition sets all remaining members to 0. So if you write
= {1}
that sets the first member to 1, and all remaining members to 0. --Mike-- "So where does that leave us? Well, it leaves us right back where we started, only more confused than before." -- Matt Gullett Ericahist | Homepage | RightClick-Encrypt | 1ClickPicGrabberCrap!! I swear to god I ran into this, using GCC (I think it was the 2.9x series) but I just tried it (using GCC 3.2) and it works perfectly! My apologies to all! Sorry :) ¡El diablo está en mis pantalones! ¡Mire, mire! Real Mentats use only 100% pure, unfooled around with Sapho Juice(tm)!