Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Checking memset

Thread Tools

Checking memset

Keith Thompson
Posts: n/a
"Old Wolf" <(E-Mail Removed)> writes:
> Richard Bos wrote:
>> "Old Wolf" <(E-Mail Removed)> wrote:

>>> 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) Removed) <>
San Diego Supercomputer Center <*> <>
We must do something. This is something. Therefore, we must do this.
Reply With Quote
Richard Bos
Posts: n/a
"Old Wolf" <(E-Mail Removed)> wrote:

> Richard Bos wrote:
> > "Old Wolf" <(E-Mail Removed)> wrote:
> >>> Tim Rentsch <(E-Mail Removed)> writes:
> >>>>
> >>>> int arr[20];
> >>
> >> memset( &arr, 0, sizeof arr );

> >
> > Yes, but that only sets the pointer _itself_ to all-bits-zero. Since
> > that is not guaranteed to be a valid pointer, not even a null pointer,
> > it's a useless operation.

> What pointer? arr is an array, and this memset sets all of
> its members to 0 (all-bits-0 for ints must be the value 0).

I was responding to your response to this:

> >>> (It could still cause problems if arr is changed
> >>> to a pointer rather than an array.)

You may not have meant it as a response to that parenthetical, but
that's what it looked like to me, because that was the last bit of text
before your reply.

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: "memset" vs "= {0}"...Are they equivalent if your initializing variables? C++ 1 09-23-2004 02:03 PM
Re: "memset" vs "= {0}"...Are they equivalent if your initializing variables? C++ 0 09-23-2004 01:28 PM
"memset" vs "= {0}"...Are they equivalent if your initializing variables? C++ 17 09-22-2004 06:06 PM
memset vs fill and iterators vs pointers Joe C C++ 5 08-24-2004 11:51 AM
2 questions: speed of memset() and pointer to multi-arrays k-man C++ 4 12-18-2003 08:52 PM