Click here to Skip to main content
12,241,564 members (51,904 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

49.5K views
7 bookmarked
Posted

How to zero your memory?

, 1 Aug 2011 CPOL
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!

You may also be interested in...

Comments and Discussions

 
GeneralRe: perfect!! Pin
GPUToaster16-Aug-11 23:34
memberGPUToaster16-Aug-11 23:34 
GeneralRe: nice explanation of some old stuff! :-) +5 Pin
ThatsAlok16-Aug-11 21:42
memberThatsAlok16-Aug-11 21:42 
GeneralRe: reported the user as a troll... it does look like the accoun... Pin
Albert Holguin29-Aug-11 8:32
memberAlbert Holguin29-Aug-11 8:32 
GeneralRe: great idea for next tip :-) Pin
ThatsAlok10-Aug-11 3:44
memberThatsAlok10-Aug-11 3:44 
GeneralReason for my vote of 3 nothing special Pin
Member 813673716-Aug-11 1:13
memberMember 813673716-Aug-11 1:13 
GeneralRe: you created account just to vote me down!!! i am surprised! ... Pin
ThatsAlok16-Aug-11 9:02
memberThatsAlok16-Aug-11 9:02 
GeneralReason for my vote of 5 Good tips, but have one question, wh... Pin
Priyank Bolia10-Aug-11 23:15
memberPriyank Bolia10-Aug-11 23:15 
GeneralRe: yeah right, however RTLSecureZeroMemory is a function! Pin
ThatsAlok11-Aug-11 2:37
memberThatsAlok11-Aug-11 2:37 
GeneralRe: `RtlZeroMemory()` evaluates to a `memset()` call. The reaso... Pin
mwb110016-Aug-11 15:55
membermwb110016-Aug-11 15:55 
GeneralReason for my vote of 5 Nice memory optimization tip. Pin
Wonde Tadesse9-Aug-11 3:07
memberWonde Tadesse9-Aug-11 3:07 
GeneralPerformance comparisons for various block lengths would be w... Pin
YvesDaoust8-Aug-11 22:21
memberYvesDaoust8-Aug-11 22:21 
GeneralRe: Yes, great idea! :) Pin
Albert Holguin9-Aug-11 8:40
memberAlbert Holguin9-Aug-11 8:40 
GeneralReason for my vote of 5 Good point about optimization. Pin
Albert Holguin2-Aug-11 12:47
memberAlbert Holguin2-Aug-11 12:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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 | Terms of Use | Mobile
Web01 | 2.8.160426.1 | Last Updated 1 Aug 2011
Article Copyright 2011 by ThatsAlok
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid