-
February 10th, 2000, 02:42 PM
#1
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));
-
February 10th, 2000, 02:48 PM
#2
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 ? ***
-
February 10th, 2000, 03:21 PM
#3
Re: memset vs ZeroMemory
Thanks for that clear reply. That same question has been in my mind.
-
February 10th, 2000, 03:28 PM
#4
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;
}
-
February 10th, 2000, 03:43 PM
#5
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
-
February 10th, 2000, 03:59 PM
#6
Re: memset vs ZeroMemory
Thanks for all of your help!
-
January 6th, 2023, 09:22 AM
#7
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
-
January 6th, 2023, 10:11 AM
#8
Re: memset vs ZeroMemory
Originally Posted by TheCPUWizard
Nearly 23 years later
Today you can also do this,
Code:
char buffer[100] = {0};
-
January 6th, 2023, 11:22 AM
#9
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|