Thomas Sampson


2 Comments

ZeroMemory – What and why

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.