c++ - Initializing entire array with memset -
i have initialised entire array value 1 output showing garbage value. program works correctly if use 0 or -1 in place of 1. there restrictions on type of values can initialised using memset.
int main(){ int a[100]; memset(a,1,sizeof(a)); cout<<a[5]<<endl; return 0; }
memset, other say, sets every byte of array @ specified value.
the reason works 0 , -1 because both use same repeating pattern on arbitrary sizes:
(int) -1 0xffffffff (char) -1 0xff
so filling memory region 0xff
fill array -1.
however, if you're filling 1
, setting every byte 0x01
; hence, same setting every integer value 0x01010101
, unlikely want.
Comments
Post a Comment