Click here to Skip to main content
15,867,686 members
Articles / Programming Languages / C
Tip/Trick

How to zero your memory?

Rate me:
Please Sign up or sign in to vote.
5.00/5 (10 votes)
1 Aug 2011CPOL1 min read 79.3K   7   14
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)


Written By
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!!!

He started with Visual C++ then moved to C# then he become language agnostic, you give him task,tell him the language or platform, he we start immediately, if he knows the language otherwise he quickly learn it and start contributing productively

Last but not the least, For good 8 years he was Visual CPP MSMVP!

Comments and Discussions

 
GeneralRe: perfect!! Pin
GPUToaster™16-Aug-11 22:34
GPUToaster™16-Aug-11 22:34 
GeneralRe: nice explanation of some old stuff! :-) +5 Pin
ThatsAlok16-Aug-11 20:42
ThatsAlok16-Aug-11 20:42 
GeneralRe: reported the user as a troll... it does look like the accoun... Pin
Albert Holguin29-Aug-11 7:32
professionalAlbert Holguin29-Aug-11 7:32 
GeneralRe: great idea for next tip :-) Pin
ThatsAlok10-Aug-11 2:44
ThatsAlok10-Aug-11 2:44 
GeneralReason for my vote of 3 nothing special Pin
Member 813673716-Aug-11 0:13
Member 813673716-Aug-11 0:13 
GeneralRe: you created account just to vote me down!!! i am surprised! ... Pin
ThatsAlok16-Aug-11 8:02
ThatsAlok16-Aug-11 8:02 
GeneralRe: you created account just to vote me down!!! i am surprised! ... Pin
The Ænema24-Sep-18 15:00
The Ænema24-Sep-18 15:00 
GeneralReason for my vote of 5 Good tips, but have one question, wh... Pin
Priyank Bolia10-Aug-11 22:15
Priyank Bolia10-Aug-11 22:15 
GeneralRe: yeah right, however RTLSecureZeroMemory is a function! Pin
ThatsAlok11-Aug-11 1:37
ThatsAlok11-Aug-11 1:37 
GeneralRe: `RtlZeroMemory()` evaluates to a `memset()` call. The reaso... Pin
mwb110016-Aug-11 14:55
mwb110016-Aug-11 14:55 
GeneralReason for my vote of 5 Nice memory optimization tip. Pin
Wonde Tadesse9-Aug-11 2:07
professionalWonde Tadesse9-Aug-11 2:07 
GeneralPerformance comparisons for various block lengths would be w... Pin
YvesDaoust8-Aug-11 21:21
YvesDaoust8-Aug-11 21:21 
GeneralRe: Yes, great idea! :) Pin
Albert Holguin9-Aug-11 7:40
professionalAlbert Holguin9-Aug-11 7:40 
GeneralReason for my vote of 5 Good point about optimization. Pin
Albert Holguin2-Aug-11 11:47
professionalAlbert Holguin2-Aug-11 11: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.