Re: Checking memset
- From: Keith Thompson <kst-u@xxxxxxx>
- Date: Thu, 29 Dec 2005 23:22:38 GMT
"Old Wolf" <oldwolf@xxxxxxxxxxxxxx> writes:
> Richard Bos wrote:
>> "Old Wolf" <oldwolf@xxxxxxxxxxxxxx> wrote:
[snip]
>>> Or for any pointer:
>>>
>>> memset( arr, 0, sizeof *arr );
>>
>> Yes, but that only sets a single member to all bits zero.
>
> It sets the entire object (*arr) to all-bits-zero. Examples:
>
> struct S { int a[20]; };
> S s, *p = &s;
> int (*q)[20] = &s.a;
>
> memset(p, 0, sizeof *p); // good
> memset(q, 0, sizeof *q); // also good
>
>> If you have a pointer, not an array, memset() cannot find out for itself
>> how many members there are
>
> If you have a pointer to the object, then you can zero it.
> Otherwise, you can't. (A pointer to part of the object doesn't count).
A pointer to part of the object does count if you have additional
information. A pointer to the first element of an array can be used
to zero the entire array if you know (by some other means) how many
elements the array has. For example:
memset(ptr, 0, COUNT * sizeof *ptr);
Of course if you meant "doesn't count" literally, you're correct, but
I assumed it was just a figure of speech.
--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
.
- References:
- Re: Checking memset
- From: Tim Rentsch
- Re: Checking memset
- From: Keith Thompson
- Re: Checking memset
- From: Old Wolf
- Re: Checking memset
- From: Richard Bos
- Re: Checking memset
- From: Old Wolf
- Re: Checking memset
- Prev by Date: Re: memcpy junk at beginning of buffer
- Next by Date: Re: gets() - dangerous? (Are we having fun yet?)
- Previous by thread: Re: Checking memset
- Next by thread: Re: Checking memset
- Index(es):
Relevant Pages
|