@@ -104,30 +104,47 @@ static double identity( const double x ) {
104104* @param len array length
105105* @return elapsed time in seconds
106106*/
107- static double benchmark ( int iterations , int len ) {
108- double elapsed ;
109- double x [ len ];
110- double y [ len ];
111- double t ;
112- int i ;
113-
114- for ( i = 0 ; i < len ; i ++ ) {
115- x [ i ] = ( rand_double ()* 200.0 ) - 100.0 ;
116- y [ i ] = 0.0 ;
117- }
118- t = tic ();
119- for ( i = 0 ; i < iterations ; i ++ ) {
120- stdlib_strided_dmap ( len , x , 1 , y , 1 , identity );
121- if ( y [ i %len ] != y [ i %len ] ) {
122- printf ( "should not return NaN\n" );
123- break ;
124- }
125- }
126- elapsed = tic () - t ;
127- if ( y [ i %len ] != y [ i %len ] ) {
128- printf ( "should not return NaN\n" );
129- }
130- return elapsed ;
107+ static double benchmark (int iterations , int len ) {
108+ // Initialize all variables at declaration
109+ double elapsed = 0.0 ;
110+ double * x = malloc (len * sizeof (double )); // Use dynamic allocation instead of VLA
111+ double * y = malloc (len * sizeof (double ));
112+ double t = 0.0 ;
113+ int i = 0 ;
114+
115+ // Check if memory allocation was successful
116+ if (x == NULL || y == NULL ) {
117+ // Handle allocation failure
118+ free (x );
119+ free (y );
120+ return -1.0 ;
121+ }
122+
123+ // Initialize arrays
124+ for (i = 0 ; i < len ; i ++ ) {
125+ x [i ] = (rand_double () * 200.0 ) - 100.0 ;
126+ y [i ] = 0.0 ;
127+ }
128+
129+ t = tic ();
130+ for (i = 0 ; i < iterations ; i ++ ) {
131+ stdlib_strided_dmap (len , x , 1 , y , 1 , identity );
132+ if (y [i % len ] != y [i % len ]) {
133+ printf ("should not return NaN\n" );
134+ break ;
135+ }
136+ }
137+ elapsed = tic () - t ;
138+
139+ if (y [i % len ] != y [i % len ]) {
140+ printf ("should not return NaN\n" );
141+ }
142+
143+ // Free allocated memory
144+ free (x );
145+ free (y );
146+
147+ return elapsed ;
131148}
132149
133150/**
0 commit comments