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. > 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. -- PJH Aio, quantitas magna frumentorum est
