Is it safe to memset bool to 0?

Is it guaranteed by the law? No. C++ says nothing about the representation of bool values. Is it guaranteed by practical reality? Yes. I mean, if you wish to find a C++ implementation that does not represent boolean false as a sequence of zeroes, I shall wish you luck. Given that false must implicitly convert … Read more

Should C++ programmer avoid memset?

In C++ std::fill or std::fill_n may be a better choice, because it is generic and therefore can operate on objects as well as PODs. However, memset operates on a raw sequence of bytes, and should therefore never be used to initialize non-PODs. Regardless, optimized implementations of std::fill may internally use specialization to call memset if … Read more

Why use bzero over memset?

I don’t see any reason to prefer bzero over memset. memset is a standard C function while bzero has never been a C standard function. The rationale is probably because you can achieve exactly the same functionality using memset function. Now regarding efficiency, compilers like gcc use builtin implementations for memset which switch to a … Read more

Why is memset() incorrectly initializing int?

memset sets each byte of the destination buffer to the specified value. On your system, an int is four bytes, each of which is 5 after the call to memset. Thus, grid[0] has the value 0x05050505 (hexadecimal), which is 84215045 in decimal. Some platforms provide alternative APIs to memset that write wider patterns to the … Read more

Using memset for integer array in C

No, you cannot use memset() like this. The manpage says (emphasis mine): The memset() function fills the first n bytes of the memory area pointed to by s with the constant byte c. Since an int is usually 4 bytes, this won’t cut it. If you (incorrectly!!) try to do this: int arr[15]; memset(arr, 1, … Read more

What is the equivalent of memset in C#?

You could use Enumerable.Repeat: byte[] a = Enumerable.Repeat((byte)10, 100).ToArray(); The first parameter is the element you want repeated, and the second parameter is the number of times to repeat it. This is OK for small arrays but you should use the looping method if you are dealing with very large arrays and performance is a … Read more