Thomas Sampson

ZeroMemory – What and why

2 Comments

Zero Memory is a Macro used within c++ programs to “Zero” some memory but what does this mean?!

Basically, here is an example of using ZeroMemory, it takes 2 parameters, a memory location and a length

ZeroMemory( &myVar, sizeof( myVar) );

So we pass in the memory location of a variable (using the & symbol to give the location of the variable and not the variable value itself) and a length (if myVar was a char lets say, then the value here would be 1).

For the example above, when this is run it will point to the location of myVar specified within memory and for the next 8 consecutive memory positions, store the value 0. so at memory position &myVar the contents of memory will now be

“00000000b”

Why?

This is mainly used when variables have not been initialised. For example if you declared a string variable in c++ but gave it no initial value, it will have a location in memory, but no value of its own, in some situations it will be pointing to random values held in ram which have not been cleared previously. Then when you try to cout << stringVar it will most likely crash depending on the data held at the position of the uninitialised variable.

Author: tomtech999

I have recently graduated with a 1st class degree in MComp Games Software Development at Sheffield Hallam University, focusing primarily on application development in C++, with experience in graphics programming, scripting languages, DVCS/VCS and web technology. In my spare time I enjoy Drumming, Reading and Snowboarding!

2 thoughts on “ZeroMemory – What and why

  1. Yes, this is a very interesting piece, but could you tell me if you can use zeromemory in C#, or if there is an equivilent?
    Thanks
    E.

  2. Is this something like reserve a memory location for the variable?