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

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -