void
x_bzero(s, n)
void *s;
unsigned int n;
{
long long *ptr = (long long *) s;
unsigned int d;
n /= sizeof(long long);
d %= sizeof(long long);
while (n--) *ptr++ = 0;
}
Dear programmers, hi
I have a question about the function above. I'm calling that function from main with this x_bzero(str, 21);
with str is an array of char with 21 elements in there (Last one is '\0'
) char str[21]
That function zeros out the buffer.
I don't want to use char instead of void on my function because I want it to be type less :) and before I was casting s to char which is one byte and I went to the loop and zero out each bye.
Now I know that the above way can be slow if I have a large array, and I tried to improve it by zeroing multiple bytes at the time (That is why I'm casting my buffer s to long long which is 8 bytes in 32bits).
My question is:
1. since long long is 8 bytes and I have 21 bytes, 21 / 8 == 2 which I go to while loop and zero out the 2 -> 8bytes, and I'll be let by 5bytes. I'm not zeroing out that 5 bytes but why my buffer is zero (nothing in there)?
Is that even a good way to do it or there is better way?
Since void is 1bytes and I have 21 of them, when I cast that buffer to long long I need to have 24 bytes (3 * 8) and I don't, does the compiler adds more bytes to it or does not use the left over bytes? (which in my case the left over is 5 bytes)
Thanks in Advance
Regards
Mark