# Platform Independence (ZeroMemory!?)

## Recommended Posts

Okay, so this post is actually about the ZeroMemory macro, but bare with me here... When thinking about the architecture, I’m not really trying to achieve a cross-platform architecture, but generally try to put up a layer of abstraction between OS specific APIs like DirectX and the OS (WINAPI) itself. Not that difficult really, but good if you decide to port your game at one point. But when I looked closer at the ZeroMemory (winbase.h) function in WINAPI I noticed it’s a macro for RtlZeroMemory which I only seem to find in the kernel driver documentation... and there is this other method RtlZeroBytes but obsolete, for better performance use RtlZeroMemory. I'm awfully confused why not memset would accomplish the exact same thing, obviously that seems to not be the case... Anyone care to shed some light?

##### Share on other sites
memset() is the portable way to zero memory. Microsoft started out defining Windows as a Pascal API, where they didn't have the standard C library.

##### Share on other sites
thanks, helps a lot

futher digging showed that the RtlZeroMemory was another macro defined as:

memset(Destination, 0, Length)

at this point is even more confusing as to why it's 2 macros for doing memset and that there's really no documentation on this detail...

i this just due to history or what?

##### Share on other sites
Yes it's history. ZeroMemory is a Win32 function. RtlZeroMemory is, strictly speaking, not a Win32 function.

The original NT kernel was designed to run several different subsystem platforms - notably OS/2, Posix, and of course Win32. The kernel has it's own API and RtlZeroMemory is one of them. Win32 API's wrap the native NT API's. In some cases the wrapper is extremely thin.

##### Share on other sites
In C++, prefer std::fill:

// Omitted all the relevant header declarations, except the important one:#include <algorithm> // This is where std::fill comes fromclass Foo {  // lots of complicated stuff here  Foo(); // the class does have a default constructor at least, but we'll say  // for the sake of argument that there are virtual functions, members of  // non-primitive types, pointers etc. such that simply zeroing out the memory  // of a Foo object is a REALLY BAD idea.  // The proper way to "clear" an array of Foos is to put a default-constructed  // Foo into each spot, which std::fill can do...};int main() {  std::vector<Foo> fooContainer;  int x[42];  char y[23];  // lots of magical code here that puts stuff in the containers.  // std::fill can zero out the arrays regardless of their type:  std::fill(x, x + 42, 0);  std::fill(y, y + 23, '\0');  // And it can blank the contents of the vector, too:  std::fill(fooContainer.begin(), fooContainer.end(), Foo());  // Although you would usually just .clear() the container and re-fill it :)}

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627682
• Total Posts
2978614

• 13
• 12
• 10
• 12
• 22