Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C

How to zero your memory?

5.00/5 (10 votes)
1 Aug 2011CPOL1 min read 82.5K  
Describing various method to zeroing the buffer in memory
Both WIN32 and CRT provide you with the different method to zero your memory, let me discuss same with you!-

1.	RTLZeroMemory
2.	RTLSecureZeroMemory
3.	RtlFillMemory
4.	ZeroMemory
5.	SecureZeroMemory
6.	FillMemory
7.	memset


Here RTLZeroMemory, RTLFillMemory (you have to specify what to fill, assuming zero) are macros which are internally calling memset to set buffer data equal to zero( these are defined in WinNT.h).

Here ZeroMemory, FillMemory (you have to specify what to fill, assuming zero) are macros which are internally calling RTLZeroMemory, RTLFillMemory respectively to set buffer data equal to zero i.e. macro calling macro ( these are defined in winbase.h).

RTLSecureZeroMemory is special function provided by windows to securely clear the buffer; also it should be taken in consideration above function might be optimize by compiler, if it consider the said memory will not be used again.

Read what MSDN says about same
“The effect of RtlSecureZeroMemory is identical to that of RtlZeroMemory, except that it is guaranteed to zero the memory location, even if it is not subsequently written to. (The compiler can optimize away a call to RtlZeroMemory, if it determines that the caller does not access that memory range again.).

Use RtlSecureZeroMemory to guarantee that sensitive information has been zeroed out. For example, suppose that a function uses a local array variable to store password information. Once the function exits, the password information can remain in the same memory location unless zeroed out by RtlSecureZeroMemory

SecureZeroMemory is a macro, which is internally calling RtlSecureZeroMemory to securely zeroing the memory.

memset, I believe now century old function used for setting up memory buffer :-).

C++
    const int Buffer_Size = 40;
char szBuffer[Buffer_Size];

    strcpy_s(szBuffer,Buffer_Size,"visual cpp zero memory test");
RtlZeroMemory(szBuffer,Buffer_Size *sizeof(char));

    strcpy_s(szBuffer,Buffer_Size,"visual cpp zero memory test");
ZeroMemory(szBuffer,Buffer_Size * sizeof(char));

strcpy_s(szBuffer,Buffer_Size,"visual cpp zero memory test");
memset(szBuffer,0,Buffer_Size*sizeof(char));

strcpy_s(szBuffer,Buffer_Size,"visual cpp zero memory test");
RtlSecureZeroMemory(szBuffer,Buffer_Size * sizeof(char));

strcpy_s(szBuffer,Buffer_Size,"visual cpp zero memory test");
SecureZeroMemory(szBuffer,Buffer_Size * sizeof(char));

strcpy_s(szBuffer,Buffer_Size,"visual cpp zero memory test");
RtlFillMemory(szBuffer,Buffer_Size * sizeof(char),0);

strcpy_s(szBuffer,Buffer_Size,"visual cpp zero memory test");
FillMemory(szBuffer,Buffer_Size * sizeof(char),0);

strcpy_s(szBuffer,Buffer_Size,"visual cpp zero memory test");
FillMemory(szBuffer,Buffer_Size * sizeof(char),0);

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)