Kleuskes & Moos <> writes:
> On Fri, 09 Dec 2005 18:11:14 +0000, Christian Bau wrote:
>> In article <>,
>> Roberto Waltman <> wrote:
>>
>>> <> wrote:
>>> >int grid[1010][1010];
>>> >Which of the following is faster?
>>> >1. memset(grid,0,1010*1010);
>>> >2. memset(grid,0,sizeof(grid));
>>>
>>> You probably meant:
>>> 1. memset(grid,0,1010*1010*sizeof(int));
>>> Otherwise the two statements are not equivalent.
>>>
>>> I would expect a modern compiler to compute
>>> the result of "1010*1010*sizeof(int)" at compile
>>> time, therefore there would be no difference
>>> between 1. & 2.
>>
>> You are overlooking a small, but important detail, which doesn't only
>> affect execution speed, but also correctness.
>
> Great... I love puzzles.
>
> memset sets byte and will *incorrectly* chop 0x3F2 to 0xF2. Subsequent
> inspection of the array will then yield 0xF2F2F2F2 as raw integer value
> which (probably) isn't what the OP expects (assuming a 32 bit integer).
Nope. 0x3F2 is 1010 decimal, and is used in the third argument to
memset(), which is of type size_t. It's not truncated to a byte.
You'll find the answer in other responses in this thread. If you want
to figure it out for yourself, ask yourself why these two:
memset(grid,0,1010*1010);
memset(grid,0,sizeof(grid));
are *not* equivalent.
--
Keith Thompson (The_Other_Keith)
kst- <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.