-
memset() function
The prototype for the function goes like this and it fills the first n bytes of the memory area pointed to be s with the constant byte c.
void *memset(void *s, int c, size_t n);
If you look at the implementation of the function, the second parameter is converted to ‘unsigned char’ so as to fill the memory with byte-wise.
I have few questions regarding the design of this function:
-Why does the function take integer as the parameter and later converts it into unsigned char? The function could have accepted the parameter as ‘char’ directly.
-Does it not add confusion to the user of this function to look at the parameter as integer and then pass the integer and expect the same to be filled in the memory? For example considering integer of 4 bytes, the user might pass 0xAABBCCDD to be filled in the memory bytes but the function truncates and fill 0xDD in the memory.
-
It's probably a relic of the past, when C didn't have function prototypes, and every parameter was promoted to int.
As for the other problem, you shouldn't be calling the function without first reading the manual page for it.