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

cudaMemset() – does it set bytes or integers?

The documentation is correct, and your interpretation of what cudaMemset does is wrong. The function really does set byte values. Your example sets the first 32 bytes to 0x12, not all 32 integers to 0x12, viz: #include <cstdio> int main(void) { const int n = 32; const size_t sz = size_t(n) * sizeof(int); int *dJunk; … 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

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 does memset take an int instead of a char?

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. It’s also worth noting that in C, (but not in … 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