Checking memset

K

Keith Thompson

Old Wolf said:
Richard said:
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.
 
R

Richard Bos

Old Wolf said:
Richard said:
Old Wolf said:
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:

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.

Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

memset 24
a = b or memset/cpy? 17
memset 21
Adding adressing of IPv6 to program 1
memset() on a struct or union 21
Filter sober in c++ don't pass test 0
JavaScript String Syntax Checking 5
memset + free 17

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top