Archived

This topic is now archived and is closed to further replies.

Possibility

What does ZeroMemory actually do?

Recommended Posts

Possibility    122
You have to do this for many of the DirectX stuff, like when creating a new lightsource, you have to zeromemory it or it wont work. So what is going on when I use it? Possibility

Share this post


Link to post
Share on other sites
Martee    476
It basically fills the entire chunk of data with ''0''.

Looking at the #defines, we see that ZeroMemory is defined as RtlZeroMemory, and RtlZeroMemory(Destination,Length) is defined as memset((Destination),0,(Length)).

~~~~~~~~~~
Martee

Share this post


Link to post
Share on other sites
Martee    476
From MSDN:

"I often get DDERR_INVALIDPARAMS or similar error codes. What should I do?
You are probably forgetting to correctly initialize a structure you're passing. In particular, ensure that the dwSize field is correctly filled out with the structure size. Also, make sure that unused fields are zeroed. It's usually a good idea to do a ZeroMemory() or memset() to clear the structure before you use it. Another useful trick is to write code like:

DDSURFACEDESC2 desc = {sizeof(desc)};

This has the effect of zeroing the structure and setting the first member (dwSize is always the first member) to zero."

So I guess you don't _have_ to zero things, but it's a good idea.

~~~~~~~~~~
Martee

Edited by - Martee on July 1, 2001 12:03:41 AM

Share this post


Link to post
Share on other sites
Kylotan    9853
Some DirectX functions look at the structure you pass in before deciding what to do. Zeros indicate ''do nothing'' in most cases. And I think they standardised the ''zero the struct before you pass it'' concept across everything as it helps with error checking (to ensure you''re not passing uninitialised memory to a DirectX function, etc).

Share this post


Link to post
Share on other sites
Krippy2k    134
It''s always a good idea to zero your variables and structures anyways. It''s amazing how much easier some bugs are to find when you do that.



Seeya
Krippy

Share this post


Link to post
Share on other sites
DoctorK    122
Zeroing also helps to put the reserved stuff in a non-hurting state. Otherwise, an entire program could work fine now, but not at all in a year.

----------

Some guy would say:

Don''t be a fool
Zeroing is cool!


By the way, to zero is one of my favourite english verbs!

--DK
--H. Hernán Moraldo

Share this post


Link to post
Share on other sites
outRider    852
To put it another way, when you create those DX structures a spot in memory is allocated for them, then you personally go in and set the desired member to what you want. You probably don''t set every member, right? Well, the members you didn''t set could have any value, they''re not necessarily 0, they can be anything, whatever happens to be in your structure''s block of memory when it was allocated to you. Zeroing it takes that chunk of memory that belongs to your structure and makes every byte 0, thereby making it blank, except what you set yourself.

Share this post


Link to post
Share on other sites
EbonySeraph    122
Just reiterating what everyone else said. It sets everything(all memeber variables) in a struct to zero!

Purpose? Well its similar to how you use normal variables...when you declare one without initializing it, problems could spawn later in your problem if you try to do something like variable++. If that peice of memory is containing crap. You can''t add one to crap and your problem will generate some sort of stack error during runtime. You would be lucky if this kind of error happens the first time you run your program because if that peice of memory grabbed was an integer you may not find the error for a while. It would be funny if there was a problem like that in a real game sold by a big company like EA and everytime THEY tested it the error never came up and when they released it, it occured to mot people!


"Ogun''s Laughter Is No Joke!!!" - Ogun Kills On The Right, A Nigerian Poem.

Share this post


Link to post
Share on other sites
Atavist    122
ZeroMemory is simply a macro that uses memset.
And memset is a highly optimized routine for memory.
Sure you could write a faster one but its a negligible difference in the end.

Just because the church was wrong doesn''t mean Galileo wasn''t a heretic.
It just means he was a heretic who was right.

Share this post


Link to post
Share on other sites