is it correct to use sizeof(loc_buf[0])
instead of sizeof(int)
?
Technically, this is correct, because it's the same thing: sizeof(loc_buf[0])
, sizeof(*loc_buf)
, and sizeof(int)
are all the same. However, since in both cases the size is taken to deal with the same dynamically allocated buffer, this is inconsistent. One should rewrite both sizeof
s in the same way of your choice; it does not matter which one you prefer.
Will this memset
resets all loc_buf
to -1
?
Yes, it would. The situation is not entirely straightforward, though: the value that you supply gets converted to unsigned char
before being set to the elements of the memory block. In two's complement representation -1
consists of all bits set to 1
. this gets converted to an unsigned char
with all ones, which gets set in all bytes in the block. Now the entire block consists of bytes with all their bits set to ones. When these bytes get re-interpreted as int
s, they become -1
s again.