memset trouble

can memset be used to fill a array with large numbers like INT_MAX, or it is just use to fill/initialise the array with 0/-1/1…
or is there limit to the number , with which array can be initialised…

2 Likes

Second parameter for memset() is unsigned char - it is the value of byte that each byte have to have. So if I’m right and you use for example 1 for unsigned int, value of int will be 1*256^3 + 1*256^2 + 1*256 + 1 (I cannot check it now).

edit:
here is proof:

#include <stdio.h>
#include <string>

int main() {
	unsigned int test[1];
	memset(test, 1, sizeof(test) );
	
	printf( "test[0]=%u\n", test[0] );
	printf( "test    %u\n", 1*256*256*256 + 1*256*256 + 1*256 + 1 );

	return 0;
}

that outputs:

test[0]=16843009
test    16843009

On the other hand, if you are familiar with with binary numbers, you can assign some special values, for example if you use 0xff you will have max unsigned int in array.

3 Likes