After AllocArrayResult(), initialize to NULL

Hi

I've seen examples in C++ of array initialization like this (initialize array to 0):
int nScores[100] = {0};

This is faster than using a loop to initialize all elements to 0.

I would like to create an array in an AFL plugin, then initialize it to NULL (EMPTY_VAL), without using a loop. Is there a similar way to do it? I did check the documentation and it does not say if AllocArrayResult() initializes it to anything.

No this is not faster. Compiler would still need to call

memset( nScores, 0, sizeof( nScores ) );

behind scenes. There is no free lunch. The loop will take pretty much the same time on any modern compiler.

This is actual output of Visual C++ 2017 compiler. As you can see entire loop that fills single value is actually compiled to single assembly instruction (rep stos). That is exactly the same what memset() does.

 mov         edx,eax  ; pointer to beginning of the array
 mov         ecx,2710h ; number of elements
 mov         eax,0D01502F9h  ; this is hex encoding for EMPTY_VAL
 mov         edi,edx  
 rep stos    dword ptr es:[edi]  ; this single instruction fills all array elements

	for ( int i = 0; i < iSize; i++ )
	{
		array[ i ] = EMPTY_VAL;
	}
4 Likes