I want to initialize my variable array “test” with memset( ).
int *test = NULL;
test = (int*)malloc(sizeof(int) *ijmax);
memset(test, 0, sizeof(int) * 100 );
I get correct value when I use 0 as initial value, but I get some strange values when I set initial value to be non-zero values. For example, if I set it to 1,
memset(test, 1, sizeof(int) * 100 );
I will get test[0]=test[1]=…test[99]=16843009. Is there any body know what the problem is? Thanks,
Wangda
[snapback]390978[/snapback]
The memset() function writes bytes, not words. So writing 1 to sizeof(int)*100 bytes writes 00000001 to every set of 8-bits. Thus, each integer in binary looks like the following:-
0000 0001 0000 0001 0000 0001 0000 0001 (1 int = 4 bytes)
which in decimal is, exactly, 16843009.
I’m not sure what the best way to set desired values is. Maybe use memcpy or a small kernel to do it?
Anjul