Click here to Skip to main content
15,880,608 members
Articles / Programming Languages / C

How to zero your memory?

Rate me:
Please Sign up or sign in to vote.
4.25/5 (3 votes)
23 Aug 2011CPOL 9.9K  
I always use a home-made macro to zero my struct's and class's:#define ZEROMEMORY ZeroMemory (this, sizeof (*this))Sample1:struct S1 { double x; long i; char s[255+1]; S1 () { ZEROMEMORY; } };Sample2:class foo { private: double x[16]; char a,b,c; foo () {...

Alternatives

Members may post updates or alternatives to this current article in order to show different approaches or add new features.

Please Sign up or sign in to vote.
1 Aug 2011ThatsAlok 3 alternatives  
Describing various method to zeroing the buffer in memory
Please Sign up or sign in to vote.
9 Aug 2011YvesDaoust
What about this, using SSE2 assembly (32 bits):void Zero(void* Buffer, int Count){ char* Cur= (char*)Buffer; char* End= (char*)Buffer + Count; // Clear the initial unaligned bytes while (Cur < End && (Cur - (char*)0) & 0xf) { *Cur++= 0; } // Clear...
Please Sign up or sign in to vote.
2 Aug 2011trotwa
char szTest[128] = { 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) Freelancer
Spain Spain
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions