Search code examples
c++carraysmemorymemset

why memset fill 0 in 2d array correctly but fail to fill 1 in that array?


I try to fill a 2D array with 1 using memset.

int arr[3][3];
memset(arr,1,sizeof(arr));
Result :  
16843009 16843009 16843009   
16843009 16843009 16843009  
16843009 16843009 16843009

Solution

  • memset works on the byte level without regards to other step sizes. Your int is 4 bytes large and thus each of those bytes are set to 00000001b, and therefore

    00000001000000010000000100000001b = 16843009