Here is the code:
#include <iostream>
#include <cstring>
int main()
{
int win[11];
std::memset(win, 1, sizeof(win));
for (int i = 0; i < 11; ++i)
std::cout << win[i] << ' ';
std::cout << std::endl;
return 0;
}
I think there is no logic flaw here? But instead of getting a string of 1
printed, I got 16843009
.
If I change the memset
to std::memset(win, 0, sizeof(win))
. Then everything is as expected. The content of win is all zeros.
Am I missing something here?
I'm using g++ 4.7.3
on Ubuntu 13.04
.
Sorry for this duplicated question.
Here is the answer. Thanks
Why does "memset(arr, -1, sizeof(arr)/sizeof(int))" not clear an integer array to -1?
int
s are usually four bytes long. But memset
sets the value of individual bytes, so you're setting each value to 0x01010101, which just so happens to equal 16843009.