Dave's Brain

Browse - Programming Tips - What's the best way (in win32) to clear memory?

Date: 2004aug22
Platform: win32
Level: beginnner

Q.  What's the best way (in win32) to clear memory?

A.

In ye olden days I used to code:

memset(&MyStruct, 0, sizeof(MyStruct));
Now I write:
ZeroMemory(&MyStruct, sizeof(MyStruct));
Which is on character longer but reads nicer. This is defined in the Win32 headers as:
#define ZeroMemory(Destination,Length) memset((Destination),0,(Length))
Linux has:
bzero(&MyStruct, sizeof(MyStruct));
Sadly bzero is deprecated so you can define it:
#ifndef bzero #define bzero(_d, _n) memset((_d), 0, (_n)) #endif

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.