Re: memset question!

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Hello Nathan and Vincent,

That'll fill all 400 *bytes* of the h array. memset's middle
parameter is also a *byte* fill pattern. So, filling an integer with
11, like above will fill it with hex 0x0B0B0B0B, or decimal
185273099. For that reason, memset's middle parameter is best passed
in as 0, 0xFF, or some other low-level known value.

I was wondering where that "185273099" was coming from.

So now I understand why everyone was saying that memset is best used with 0.
Or as you pointed out 0xFF (or 255 decimal)!

Thanks guys!

--
Best regards
Roberto


"Nathan Mates" wrote:

In article <C46E2F0C-BD8F-41E0-A3A1-373200000BF1@xxxxxxxxxxxxx>,
=?Utf-8?B?Um9iYnk=?= <Robby@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
I nevery really use memset() in my code , I usually innitialize all my array
values through a for-loop. But for curiosity purpose, why does memset work
only with char arrays?

Short summary: because you don't know how powerful & useful
sizeof() is.

char buf[100];
memset(buf, 'X', 100);

This works, because buf is exactly 100 bytes long. But, random
numbers like 100 in your code are BAD. If you must use 100, do it in
as few places as possible:

char buf[100];
memset(buf, 'X', sizeof(buf));

That has the compiler figure out how large buf is, in bytes,
and pass that to memset. sizeof() is done at *compile* time, not
run time, so there's no speed penalty for doing this.

int h[100];
memset(h,11,100);

That doesn't work, because ints are larger than bytes. But,
you only cleared the first 100 *bytes* of the h array. If you switch
to sizeof, it'll work!

int h[100];
memset(h, 11, sizeof(h));

That'll fill all 400 *bytes* of the h array. memset's middle
parameter is also a *byte* fill pattern. So, filling an integer with
11, like above will fill it with hex 0x0B0B0B0B, or decimal
185273099. For that reason, memset's middle parameter is best passed
in as 0, 0xFF, or some other low-level known value.

Nathan Mates

--
<*> Nathan Mates - personal webpage http://www.visi.com/~nathan/
# Programmer at Pandemic Studios -- http://www.pandemicstudios.com/
# NOT speaking for Pandemic Studios. "Care not what the neighbors
# think. What are the facts, and to how many decimal places?" -R.A. Heinlein

.



Relevant Pages

  • Re: memset question!
    ... So, filling an integer with ... 11, like above will fill it with hex 0x0B0B0B0B, or decimal ... For that reason, memset's middle parameter is best passed ... So now I understand why everyone was saying that memset is best used with 0. ...
    (microsoft.public.vc.language)
  • Re: memset question!
    ... parameter is also a *byte* fill pattern. ... So, filling an integer with ... 11, like above will fill it with hex 0x0B0B0B0B, or decimal ... If you ever get out of C woods back into the sunny land of C++, ...
    (microsoft.public.vc.language)
  • Re: [PATCH] proc: pagemap: Hold mmap_sem during page walk
    ... But the problem with the deadlock is not that "same physical page" issue, ... reliable in the array doesn't actually solve the bug I was pointing out. ... pattern is a valid pattern, but it is _only_ valid if you then do the ... So there is a very real reason for that pattern existing. ...
    (Linux-Kernel)
  • Re: YOUNG AVENGERS #11: Heinbergs Twins Arent Wandas
    ... Kaplans and Shepherds to name their boys Billy and Tommy, no reason ... think that Wanda can transfigure souls, no reason to think that the ... Those aren't her powers. ... the mystic energies as well as her hex powers ...
    (rec.arts.comics.marvel.universe)
  • Re: Thomas Covenant series
    ... reason not to continue modifiying it given the need, ... I don't see any reason, then, why the Earth could not be described as ... one would be hard-pressed to say they fit with reality. ... pattern of them, and lift that pattern up (shaken loose of anything ...
    (rec.arts.sf.composition)