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 SecureZeroMemory

memset vs SecureZeroMemory

Scheduled Pinned Locked Moved C / C++ / MFC
questionvisual-studiodata-structureshelp
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
    Adam Roderick J
    wrote on last edited by
    #1

    Hi All, i have a DLL, there is some code as mentioned below:-

    char szName[MAX_PATH];
    CString csName = "3424234"; // size can be judged only on run time
    wcstombs( szName, csName.operator LPCTSTR(), _tcslen( csName.operator LPCTSTR()));

    Now i want to initialize szName array to 0, to aviod the unexpected behaviour. Now my question is whether i use memset or SecureZeroMemory? Which is better, and why? Any help will be appreciated.

    Величие не Бога может быть недооценена.

    modified on Tuesday, February 9, 2010 12:39 AM

    K _ R L 4 Replies Last reply
    0
    • A Adam Roderick J

      Hi All, i have a DLL, there is some code as mentioned below:-

      char szName[MAX_PATH];
      CString csName = "3424234"; // size can be judged only on run time
      wcstombs( szName, csName.operator LPCTSTR(), _tcslen( csName.operator LPCTSTR()));

      Now i want to initialize szName array to 0, to aviod the unexpected behaviour. Now my question is whether i use memset or SecureZeroMemory? Which is better, and why? Any help will be appreciated.

      Величие не Бога может быть недооценена.

      modified on Tuesday, February 9, 2010 12:39 AM

      K Offline
      K Offline
      KingsGambit
      wrote on last edited by
      #2

      char szName[MAX_PATH] = {0};

      A 1 Reply Last reply
      0
      • A Adam Roderick J

        Hi All, i have a DLL, there is some code as mentioned below:-

        char szName[MAX_PATH];
        CString csName = "3424234"; // size can be judged only on run time
        wcstombs( szName, csName.operator LPCTSTR(), _tcslen( csName.operator LPCTSTR()));

        Now i want to initialize szName array to 0, to aviod the unexpected behaviour. Now my question is whether i use memset or SecureZeroMemory? Which is better, and why? Any help will be appreciated.

        Величие не Бога может быть недооценена.

        modified on Tuesday, February 9, 2010 12:39 AM

        _ Offline
        _ Offline
        _Superman_
        wrote on last edited by
        #3

        SecureZeroMemory may be a little slow on the x86 platform as it initializes each element of the array in a loop. But for x64 it uses an intrinsic, which will be very fast. You could also use the C-Runtime memcpy_s function. To initialize the array to zero at the time of creation, the reply by Rejeesh definitely works in Visual Studio.

        «_Superman_» I love work. It gives me something to do between weekends.
        Microsoft MVP (Visual C++)

        A C 2 Replies Last reply
        0
        • _ _Superman_

          SecureZeroMemory may be a little slow on the x86 platform as it initializes each element of the array in a loop. But for x64 it uses an intrinsic, which will be very fast. You could also use the C-Runtime memcpy_s function. To initialize the array to zero at the time of creation, the reply by Rejeesh definitely works in Visual Studio.

          «_Superman_» I love work. It gives me something to do between weekends.
          Microsoft MVP (Visual C++)

          A Offline
          A Offline
          Adam Roderick J
          wrote on last edited by
          #4

          Thanks for the reply. So you mean in 64 bit processor, SecureZeroMemory will not make much change in time consumption.

          Величие не Бога может быть недооценена.

          _ 1 Reply Last reply
          0
          • K KingsGambit

            char szName[MAX_PATH] = {0};

            A Offline
            A Offline
            Adam Roderick J
            wrote on last edited by
            #5

            But i am looking for details regarding SecureZeroMemory and memset or memcpy, any way thanks for your help :)

            Величие не Бога может быть недооценена.

            K 1 Reply Last reply
            0
            • A Adam Roderick J

              But i am looking for details regarding SecureZeroMemory and memset or memcpy, any way thanks for your help :)

              Величие не Бога может быть недооценена.

              K Offline
              K Offline
              KingsGambit
              wrote on last edited by
              #6

              I think _superman_ has already sent you a better answer which I think explains the differences. I usually initialize all arrays to zero as I posted before.

              1 Reply Last reply
              0
              • A Adam Roderick J

                Thanks for the reply. So you mean in 64 bit processor, SecureZeroMemory will not make much change in time consumption.

                Величие не Бога может быть недооценена.

                _ Offline
                _ Offline
                _Superman_
                wrote on last edited by
                #7

                That's correct. Intrinsics are similar to writing inline assembly instructions. x64 does not support inline assembly and so you have to use intrinsics. Read more about intrinsics in the following links - Intrinsic function[^] Compiler Intrinsics[^]

                «_Superman_» I love work. It gives me something to do between weekends.
                Microsoft MVP (Visual C++)

                1 Reply Last reply
                0
                • A Adam Roderick J

                  Hi All, i have a DLL, there is some code as mentioned below:-

                  char szName[MAX_PATH];
                  CString csName = "3424234"; // size can be judged only on run time
                  wcstombs( szName, csName.operator LPCTSTR(), _tcslen( csName.operator LPCTSTR()));

                  Now i want to initialize szName array to 0, to aviod the unexpected behaviour. Now my question is whether i use memset or SecureZeroMemory? Which is better, and why? Any help will be appreciated.

                  Величие не Бога может быть недооценена.

                  modified on Tuesday, February 9, 2010 12:39 AM

                  R Offline
                  R Offline
                  Rajesh R Subramanian
                  wrote on last edited by
                  #8

                  One thing others didn't point out: memset is a CRT call, and SecureZeroMemory is an API call. That's it. I don't think one would be "faster" over the other (practically) to initialize an array of size MAX_PATH. If you had considered ZeroMemory, there are chances where the compiler can completely optimize out (remove) the call to this function when it detects that the initialised buffer isn't being used anywhere. However, SecureZeroMemory would ensure that the buffer be initialised, even if it isn't being used at all (the optimiser won't rule this call out).

                  “Follow your bliss.” – Joseph Campbell

                  1 Reply Last reply
                  0
                  • A Adam Roderick J

                    Hi All, i have a DLL, there is some code as mentioned below:-

                    char szName[MAX_PATH];
                    CString csName = "3424234"; // size can be judged only on run time
                    wcstombs( szName, csName.operator LPCTSTR(), _tcslen( csName.operator LPCTSTR()));

                    Now i want to initialize szName array to 0, to aviod the unexpected behaviour. Now my question is whether i use memset or SecureZeroMemory? Which is better, and why? Any help will be appreciated.

                    Величие не Бога может быть недооценена.

                    modified on Tuesday, February 9, 2010 12:39 AM

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    Adam Roderick J 09 wrote:

                    Now i want to initialize szName array to 0, to aviod the unexpected behaviour.

                    1. You should be using the wcstombs_s() function. 2. You should use that function as documented here[^] to calculate the required size of your destination buffer, and allocating that appropriately. In your example above, if the length of your string exceeds MAX_PATH then you have an immediate memory leak. 3. Clearing the buffer to zeroes before copying the string provides no benefit.

                    MVP 2010 - are they mad?

                    1 Reply Last reply
                    0
                    • _ _Superman_

                      SecureZeroMemory may be a little slow on the x86 platform as it initializes each element of the array in a loop. But for x64 it uses an intrinsic, which will be very fast. You could also use the C-Runtime memcpy_s function. To initialize the array to zero at the time of creation, the reply by Rejeesh definitely works in Visual Studio.

                      «_Superman_» I love work. It gives me something to do between weekends.
                      Microsoft MVP (Visual C++)

                      C Offline
                      C Offline
                      Chris Losinger
                      wrote on last edited by
                      #10

                      (i think you've got your x86 and x64 reversed here)

                      image processing toolkits | batch image processing

                      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