Re: memset question!
- From: Robby <Robby@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Thu, 10 Sep 2009 08:36:03 -0700
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
- Follow-Ups:
- Re: memset question!
- From: Igor Tandetnik
- Re: memset question!
- From: jayachandran kamaraj
- Re: memset question!
- References:
- memset question!
- From: Robby
- Re: memset question!
- From: Nathan Mates
- memset question!
- Prev by Date: Re: wh_callwndProc hook problem
- Next by Date: Re: manifest vc8.0 problem
- Previous by thread: Re: memset question!
- Next by thread: Re: memset question!
- Index(es):
Relevant Pages
|