# Visual C++ & C++ Programming > Visual C++ Programming >  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));

----------


## Feng Yuan

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 ? ***

----------


## Bill Heffner

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

----------


## 

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


#include &lt;iostream.h&gt;
#include &lt;windows.h&gt;

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 &lt; 256; index)
	{
		for(int z = 0; z &lt; 10; z++)
		{
			// why don't I have to use the -&gt; to access the variables?
			lookup[index].variable1[z] = 'A';
			lookup[index].variable2[z] = 'A';
		}
	}

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

----------


## ChrisD

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 &lt; LookupSize; index)
	{
		for(int z = 0; z &lt; 10; z++)
		{
		// why don't I have to use the -&gt; 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 &lt;&lt; lookup[index].variable1 
		&lt;&lt; " " &lt;&lt; lookup[index].variable2 &lt;&lt; endl;
}

HTH,
chris

----------


## 

Thanks for all of your help!

----------


## TheCPUWizard

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



```
//
// Constants
//
#define MoveMemory RtlMoveMemory
#define CopyMemory RtlCopyMemory
#define FillMemory RtlFillMemory
#define ZeroMemory RtlZeroMemory
```

minwinbase.h



```
#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

----------


## wolle

> Nearly 23 years later


Today you can also do this,


```
char buffer[100] = {0};
```

----------


## 2kaud

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



```
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.

----------

