Re: Checking memset



"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.
.



Relevant Pages

  • Re: comparing strings
    ... As others have pointed out, strcmp returns negative, zero, or positive. ... Does getpass return a pointer to a static chararray? ...
    (comp.lang.cpp)
  • Re: (part 10) More Schildt-like errors in Dicky Heathens book
    ... like mallocif mallocreturns a non-NULL pointer. ... If one is using a debugging malloc which checks that all accesses are ... when sz is zero, it gives the debugging allocator an opportunity to ...
    (comp.lang.c)
  • Re: Triplet 630APL meter movement adjustment
    ... was the same whether the meter was standind up or laying down. ... mechanical zero with the unit in the vertical position and took a ... There are three weights on the bottom of the meter pointer; ...
    (sci.electronics.repair)
  • Re: (MS-)DOS PC on a microcontroller??
    ... memory block of zero bytes. ... The pointer returned if the ... Each such allocation shall yield a pointer to ... support malloc/calloc requests for 0 bytes and whether or not the ...
    (comp.arch.embedded)
  • Re: Apple III Business BASIC Invokables
    ... I'm using Apple II Pascal 1.3 to assemble to a Pascal object module, then transfer to a physical /// disk... ... It pulls this off with extra hardware to snoop zero page address read/writes used by indirect indexed addressing mode instructions. ... When you get a 16 bit pointer to a buffer, it appears that you have to determine if it is an absolute pointer to somewhere in the current 64K, or an indirect pointer that has been already set up in the zero page/X-byte page. ...
    (comp.sys.apple2.programmer)