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. Null'ing structures

Null'ing structures

Scheduled Pinned Locked Moved C / C++ / MFC
6 Posts 3 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.
  • M Offline
    M Offline
    Marc Richarme
    wrote on last edited by
    #1

    In most source code, when initializing structures to zero, you mostly find something like this:

    MYSTRUCT mystruct;
    ZeroMemory(&mystruct,sizeof(mystruct));

    I was wondering... what's wrong with a simple

    MYSTRUCT mystruct = {0};

    :confused:

    Cheers,
    Marc

    :beer: Click to see my *real* signature :beer:

    G S 2 Replies Last reply
    0
    • M Marc Richarme

      In most source code, when initializing structures to zero, you mostly find something like this:

      MYSTRUCT mystruct;
      ZeroMemory(&mystruct,sizeof(mystruct));

      I was wondering... what's wrong with a simple

      MYSTRUCT mystruct = {0};

      :confused:

      Cheers,
      Marc

      :beer: Click to see my *real* signature :beer:

      G Offline
      G Offline
      gekoscan
      wrote on last edited by
      #2

      Nothing what you are doing is just fine. It's all about what way you were taught. Peace

      1 Reply Last reply
      0
      • M Marc Richarme

        In most source code, when initializing structures to zero, you mostly find something like this:

        MYSTRUCT mystruct;
        ZeroMemory(&mystruct,sizeof(mystruct));

        I was wondering... what's wrong with a simple

        MYSTRUCT mystruct = {0};

        :confused:

        Cheers,
        Marc

        :beer: Click to see my *real* signature :beer:

        S Offline
        S Offline
        Scot Brennecke
        wrote on last edited by
        #3

        It's all about making readable code for yourself and others, weeks after you write it. = {0} works. But it's not as obvious that the entire structure will be zeroed. ZeroMemory usually gets compiled as memset with 0, but ZeroMemory is much clearer to read. Back in the days when drive space and memory was expensive, it paid to write very small source code. We learned to be VERY thrifty with every byte, at the expense of readability. But the cost of maintenance should not be underestimated. If you can save another programmer just 2 minutes by making the code easier to read and understand, you've justified the use of a few extra bytes of source code. Scot Brennecke Software Developer VC++ MVP

        M 1 Reply Last reply
        0
        • S Scot Brennecke

          It's all about making readable code for yourself and others, weeks after you write it. = {0} works. But it's not as obvious that the entire structure will be zeroed. ZeroMemory usually gets compiled as memset with 0, but ZeroMemory is much clearer to read. Back in the days when drive space and memory was expensive, it paid to write very small source code. We learned to be VERY thrifty with every byte, at the expense of readability. But the cost of maintenance should not be underestimated. If you can save another programmer just 2 minutes by making the code easier to read and understand, you've justified the use of a few extra bytes of source code. Scot Brennecke Software Developer VC++ MVP

          M Offline
          M Offline
          Marc Richarme
          wrote on last edited by
          #4

          I got a bit curious, and fetched the decompiler... = {0} is translated to 20 bytes of inline machine code in my test program whereas the ZeroMemory approach is worth 16 bytes of inline machine code which includes a call to a more complex function with several jump instructions. Scot Brennecke wrote: but ZeroMemory is much clearer to read Not in assembly ;P

          Cheers,
          Marc

          :beer: Click to see my *real* signature :beer:

          S 1 Reply Last reply
          0
          • M Marc Richarme

            I got a bit curious, and fetched the decompiler... = {0} is translated to 20 bytes of inline machine code in my test program whereas the ZeroMemory approach is worth 16 bytes of inline machine code which includes a call to a more complex function with several jump instructions. Scot Brennecke wrote: but ZeroMemory is much clearer to read Not in assembly ;P

            Cheers,
            Marc

            :beer: Click to see my *real* signature :beer:

            S Offline
            S Offline
            Scot Brennecke
            wrote on last edited by
            #5

            OK, since you want to "fight" about it... ;) = {0} is not strictly equivalent to the ZeroMemory approach. Most significantly, if your struct is declared on the stack, you do not get what you want: "If initializer-list has fewer values than an aggregate type, the remaining members or elements of the aggregate type are initialized to 0 for external and static variables. The initial value of an automatic identifier not explicitly initialized is undefined." Scot Brennecke Software Developer VC++ MVP

            M 1 Reply Last reply
            0
            • S Scot Brennecke

              OK, since you want to "fight" about it... ;) = {0} is not strictly equivalent to the ZeroMemory approach. Most significantly, if your struct is declared on the stack, you do not get what you want: "If initializer-list has fewer values than an aggregate type, the remaining members or elements of the aggregate type are initialized to 0 for external and static variables. The initial value of an automatic identifier not explicitly initialized is undefined." Scot Brennecke Software Developer VC++ MVP

              M Offline
              M Offline
              Marc Richarme
              wrote on last edited by
              #6

              damn.. well ok, you win, you got the docs on your side :-D MSDN wrote: The initial value of an automatic identifier not explicitly initialized is undefined ..if this is true I think my code needs a little rewrite, but I'm quite sure this isn't true (at least for MS Visual C++).

              Cheers,
              Marc

              :beer: Click to see my *real* signature :beer:

              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