Paul Herring wrote:
> On 3/21/07, ranjan kumar ojha <[EMAIL PROTECTED]> wrote:
>> Hi friends,
>>
>>          I got perplexed because of memset. look
>>
>>         int a[100];
>>         memset(a,0,sizeof(a));
>>         then it is filling 0 in array.
>>
>>        int a[100];
>>        memset(a,-1,sizeof(a));
>>        then it is filling -1 in array.
>>
>>        but
>>        int a[100];
>>        memset(a,1,sizeof(a));
>>        then it is filling array by a big number. can  any one explain this 
>> abnormal behaviour of memset? plz also tell me  internal  implementation of 
>> memset.
> 
> memset() acts on a byte by byte basis, but you're reading the data
> back in sizeof(int) blocks. These are typically (but not necessarily)
> 4 bytes long.
> 
> So your first snippit would set all the ints to:
> 0b00000000 00000000 00000000 00000000 == 0
> 
> The second:
> 0b11111111 11111111 11111111 11111111 == -1
> 
> The third:
> 0b00000001 00000001 00000001 00000001 which unsurprisingly isn't 1.

(This notation confused me for a bit...)


>>        if we fill 0 in an array using for loop &  using memse also, which 
>> one will be faster or
>> both will take same time.
> 
> memset typically uses a for loop anyway. And for the number of times
> you'd typically use it, the difference would be neglegable compared to
> the rest of the program/the time spent changing it.

Huh?  Most implementations of memset() that I've seen are usually done 
in assembler to take advantage of the multi-byte, aligned, 
processor-specific instructions that are available (e.g. assign 32-bits 
at a time instead of 8-bits on 32-bit Intel-based architectures).  The 
optimizations are not always useful, though.  memset() is usually only 
used for zeroing out memory.

-- 
Thomas Hruska
CubicleSoft President
Ph: 517-803-4197

*NEW* VerifyMyPC 2.2
Change tracking and management tool.
Reduce tech. support times from 2 hours to 5 minutes.

Free for personal use, $10 otherwise.
http://www.CubicleSoft.com/VerifyMyPC/

Reply via email to