CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Guest

    memset vs ZeroMemory

    Which is better to use and why?

    Are all of these the same?
    char buffer[100];
    memset(&buffer, 0, sizeof(buffer));
    memset(&buffer, '\0', sizeof(buffer));
    ZeroMemory(&buffer, sizeof(buffer));


  2. #2
    Join Date
    Oct 1999
    Location
    WA
    Posts
    2,393

    Re: memset vs ZeroMemory

    The first two are the same, the third is different. They have the same effect, but different performance.

    memset will be inlined in release mode, but the code may not be optimized. I guess ZeroMemory will be optimized for large piece of data, but there is a Win32 API call overhead.

    So call memset for small objects, and ZeroMemory for large chunk of memory.

    ** Get a real hard Windows graphics programming question, can't find answer in FAQ, in MSDN, in Petzold's book ? ***

  3. #3
    Join Date
    Jan 2000
    Location
    San Diego, CA
    Posts
    1,334

    Re: memset vs ZeroMemory

    Thanks for that clear reply. That same question has been in my mind.


  4. #4
    Guest

    Re: memset vs ZeroMemory

    Why doesn't the below work? It crashes in the SomeFunction! Thanks for any advice and your help already!


    #include <iostream.h>
    #include <windows.h>

    struct LOOKUP
    {
    char variable1[100];
    char variable2[5];
    };

    LOOKUP *lookup;

    void SomeFunction(LOOKUP *);

    int main()
    {
    lookup = new LOOKUP[256];

    SomeFunction(lookup);

    delete lookup;

    return 0;
    }

    void SomeFunction(LOOKUP *lookup)
    {
    // null out lookup variable1 and variable2
    ZeroMemory(&lookup, sizeof(&lookup));

    // assign values to the variable1 and variable2
    for(int index = 0; index < 256; index)
    {
    for(int z = 0; z < 10; z++)
    {
    // why don't I have to use the -> to access the variables?
    lookup[index].variable1[z] = 'A';
    lookup[index].variable2[z] = 'A';
    }
    }

    cout << lookup[index].variable1 << " " << lookup[index].variable2 << endl;
    }





  5. #5
    Join Date
    May 1999
    Posts
    667

    Re: memset vs ZeroMemory

    Change your function to
    void SomeFunction(LOOKUP *lookup, int LookupSize)
    {
    // null out lookup (you were zeroing out the pointer itself
    // you need to pass in size you cannot get size of Dyn alloc array
    ZeroMemory(lookup, LookupSize * sizeof(LOOKUP));
    // assign values to the variable1 and variable2
    for(int index = 0; index < LookupSize; index)
    {
    for(int z = 0; z < 10; z++)
    {
    // why don't I have to use the -> to access the variables?
    // when you use [] on a pointer you get object at
    // pointer + (i * sizeofobject pointed to)
    // the object can be a pointer but you declared objects for array
    lookup[index].variable1[z] = 'A';
    lookup[index].variable2[z] = 'A';
    }
    }
    cout << lookup[index].variable1
    << " " << lookup[index].variable2 << endl;
    }

    HTH,
    chris




  6. #6
    Guest

    Re: memset vs ZeroMemory

    Thanks for all of your help!


  7. #7
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: memset vs ZeroMemory

    Nearly 23 years later, but someone just showed me this thread.... and an error can not be left standing... here are the facts [Microsoft C++]

    Code:
    //
    // Constants
    //
    #define MoveMemory RtlMoveMemory
    #define CopyMemory RtlCopyMemory
    #define FillMemory RtlFillMemory
    #define ZeroMemory RtlZeroMemory
    minwinbase.h

    Code:
    #define RtlEqualMemory(Destination,Source,Length) (!memcmp((Destination),(Source),(Length)))
    #define RtlMoveMemory(Destination,Source,Length) memmove((Destination),(Source),(Length))
    #define RtlCopyMemory(Destination,Source,Length) memcpy((Destination),(Source),(Length))
    #define RtlFillMemory(Destination,Length,Fill) memset((Destination),(Fill),(Length))
    #define RtlZeroMemory(Destination,Length) memset((Destination),0,(Length))
    winnt.h
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  8. #8
    Join Date
    Feb 2017
    Posts
    677

    Re: memset vs ZeroMemory

    Quote Originally Posted by TheCPUWizard View Post
    Nearly 23 years later
    Today you can also do this,
    Code:
    char buffer[100] = {0};

  9. #9
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: memset vs ZeroMemory

    AFAIK, you always could with ={0}. With C++ you can now just have:

    Code:
    char buffer[100] {};
    In the MSDN documentation, ZeroMemory() is referred as a macro to RtlZeroMemory
    https://learn.microsoft.com/en-us/pr...66920(v=vs.85)

    RtlZeroMemory() is defined in both user and driver (kernel) modes.

    If you're trying to zero some memory after usage for security reasons, then SecureZeroMemory() (or memset_s() ) should be used as memset() could be optimised away by the compiler.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured