...
For historical reasons, certain C Standard functions accept an argument of type int
and convert it to either unsigned char
or plain char
. This conversion can result in unexpected behavior if the value cannot be represented in the smaller type. This noncompliant solution unexpectedly clears the arrayThe second argument to memset()
is an example; it indicates what byte to store in the range of memory indicated by the first and third arguments. If the second argument is outside the range of a signed char
or plain char
, then its higher order bits will typically be truncated. Consequently, this noncompliant solution unexpectedly sets all elements in the array to 0, rather than 4096:
Code Block | ||||
---|---|---|---|---|
| ||||
#include <string.h> #include <stddef.h> int *init_memory(int *array, size_t n) { return memset(array, 4096, n); } |
...