The manpage says about memset
:
#include <string.h> void *memset(void *s, int c, size_t n)
The
memset()
function fills the firstn
bytes of the memory area pointed to bys
with the constant bytec
.
It is obvious that memset
can't be used to initialize int
array as shown below:
int a[10]; memset(a, 1, sizeof(a));
it is because int
is represented by 4 bytes (say) and one can not get the desired value for the integers in array a
.
But I often see the programmers use memset
to set the int
array elements to either 0
or -1
.
int a[10]; int b[10]; memset(a, 0, sizeof(a)); memset(b, -1, sizeof(b));
As per my understanding, initializing with integer 0
is OK because 0
can be represented in 1 byte (may be I am wrong in this context). But how is it possible to initialize b
with -1
(a 4 bytes value)?
memset allows you to fill individual bytes as memory and you are trying to set integer values (maybe 4 or more bytes.) Your approach will only work on the number 0 and -1 as these are both represented in binary as 00000000 or 11111111 . Show activity on this post. Because memset works on byte and set every byte to 1.
memset predates (by quite a bit) the addition of function prototypes to C. Without a prototype, you can't pass a char to a function -- when/if you try, it'll be promoted to int when you pass it, and what the function receives is an int .
Memset() converts the value ch to unsigned char and copies it into each of the first n characters of the object pointed to by str[]. If the object is not trivially-copyable (e.g., scalar, array, or a C-compatible struct), the behavior is undefined.
memset is O(n). O(n2) means at most some number of steps proportional to n2 must be performed. memset is better than just O(n2) because setting 2n bytes takes only twice as much work as n bytes, not four times as much work, in general.
Oddly, the reason this works with -1
is exactly the same as the reason that this works with zeros: in two's complement binary representation, -1
has 1
s in all its bits, regardless of the size of the integer, so filling in a region with bytes filled with all 1
s produces a region of -1
signed int
s, long
s, and short
s on two's complement hardware.
On hardware that differs from two's complement the result will be different. The -1
integer constant would be converted to an unsigned char
of all ones, because the standard is specific on how the conversion has to be performed. However, a region of bytes with all their bits set to 1
would be interpreted as integral values in accordance with the rules of the platform. For example, on sign-and-magnitude hardware all elements of your array would contain the smallest negative value of the corresponding type.
0
, its value is also 0. However, if all bits are 1
the value is -1.If we write int a[2]
, 4x2 bytes of memory is allocated which contains random/garbage bits-
00110000 00100101 11100011 11110010 11110101 10001001 00111000 00010001
Then, we write memset(a, 0, sizeof(a))
. Now, memset()
works byte by byte, and one byte representation (unsigned char
) of 0 is 00000000
. So, it becomes-
00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
Therefore, both a[0]
and a[1]
are initialized with 0.
Now, lets see memset(a, -1, sizeof(a))
: one byte for -1 is 11111111
. And, we get-
11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111
Here, both a[0]
and a[1]
will have the value -1.
However, for memset(a, 1, sizeof(a))
: 1 in a byte is 00000001
-
00000001 00000001 00000001 00000001 00000001 00000001 00000001 00000001
So, the value will be- 16843009.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With