Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. 'memset' vs '= {0}'

'memset' vs '= {0}'

Scheduled Pinned Locked Moved C / C++ / MFC
questionvisual-studio
10 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Atlantys
    wrote on last edited by
    #1

    (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]

    J J 2 Replies Last reply
    0
    • A Atlantys

      (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]

      J Offline
      J Offline
      Joe Woodbury
      wrote on last edited by
      #2

      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.

      A 1 Reply Last reply
      0
      • A Atlantys

        (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]

        J Offline
        J Offline
        Jim Crafton
        wrote on last edited by
        #3

        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)!

        D J A M 4 Replies Last reply
        0
        • J Jim Crafton

          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)!

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          What sort of error does this produce on non-Microsoft compilers?

          1 Reply Last reply
          0
          • J Jim Crafton

            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)!

            J Offline
            J Offline
            John R Shaw
            wrote on last edited by
            #5

            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!

            1 Reply Last reply
            0
            • J Joe Woodbury

              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.

              A Offline
              A Offline
              Atlantys
              wrote on last edited by
              #6

              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]

              J 1 Reply Last reply
              0
              • J Jim Crafton

                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)!

                A Offline
                A Offline
                Atlantys
                wrote on last edited by
                #7

                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]

                1 Reply Last reply
                0
                • A Atlantys

                  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]

                  J Offline
                  J Offline
                  Joe Woodbury
                  wrote on last edited by
                  #8

                  Assembly instruction: Store String Data: DWord http://www.online.ee/~andre/i80386/Opcodes/STOS-STOSB-STOSW-STOSD.html[^]

                  1 Reply Last reply
                  0
                  • J Jim Crafton

                    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)!

                    M Offline
                    M Offline
                    Michael Dunn
                    wrote on last edited by
                    #9

                    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

                    J 1 Reply Last reply
                    0
                    • M Michael Dunn

                      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

                      J Offline
                      J Offline
                      Jim Crafton
                      wrote on last edited by
                      #10

                      Crap!! 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)!

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups