Click here to Skip to main content
Click here to Skip to main content

Tagged as

How to zero your memory?

, 1 Aug 2011
Rate this:
Please Sign up or sign in to vote.
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 Smile | :) .
 
        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)

Share

About the Author

ThatsAlok
Software Developer (Senior)
India India
He used to have biography here Smile | :) , but now he will hire someone (for free offcourse Big Grin | :-D ), Who writes his biography on his behalf Smile | :)
 
He is Great Fan of Mr. Johan Rosengren (his idol),Lim Bio Liong, Nishant S and DavidCrow and Believes that, he will EXCEL in his life by following there steps!!!
 

For good 8 years he was Visual CPP MSMVP!

Comments and Discussions

 
GeneralRe: perfect!! PinmemberGPUToaster16-Aug-11 22:34 
GeneralRe: nice explanation of some old stuff! :-) +5 PinmemberThatsAlok16-Aug-11 20:42 
GeneralRe: reported the user as a troll... it does look like the accoun... PinmemberAlbert Holguin29-Aug-11 7:32 
GeneralRe: great idea for next tip :-) PinmemberThatsAlok10-Aug-11 2:44 
GeneralReason for my vote of 3 nothing special PinmemberMember 813673716-Aug-11 0:13 
GeneralRe: you created account just to vote me down!!! i am surprised! ... PinmemberThatsAlok16-Aug-11 8:02 
GeneralReason for my vote of 5 Good tips, but have one question, wh... PinmemberPriyank Bolia10-Aug-11 22:15 
GeneralRe: yeah right, however RTLSecureZeroMemory is a function! PinmemberThatsAlok11-Aug-11 1:37 
GeneralRe: `RtlZeroMemory()` evaluates to a `memset()` call. The reaso... Pinmembermwb110016-Aug-11 14:55 
GeneralReason for my vote of 5 Nice memory optimization tip. PinmemberWonde Tadesse9-Aug-11 2:07 
GeneralPerformance comparisons for various block lengths would be w... PinmemberYvesDaoust8-Aug-11 21:21 
GeneralRe: Yes, great idea! :) PinmemberAlbert Holguin9-Aug-11 7:40 
GeneralReason for my vote of 5 Good point about optimization. PinmemberAlbert Holguin2-Aug-11 11:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140821.2 | Last Updated 1 Aug 2011
Article Copyright 2011 by ThatsAlok
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid