From 17836832e85895e5ddc94e37bc5745f3677ac668 Mon Sep 17 00:00:00 2001 From: sahilk45 Date: Thu, 3 Apr 2025 01:11:33 +0530 Subject: [PATCH] fixing C lint error --- .../dabs2/benchmark/c/benchmark.length.c | 44 ++++++++++++++----- 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/lib/node_modules/@stdlib/math/strided/special/dabs2/benchmark/c/benchmark.length.c b/lib/node_modules/@stdlib/math/strided/special/dabs2/benchmark/c/benchmark.length.c index d8bca0c9d710..e5c8929d19f2 100644 --- a/lib/node_modules/@stdlib/math/strided/special/dabs2/benchmark/c/benchmark.length.c +++ b/lib/node_modules/@stdlib/math/strided/special/dabs2/benchmark/c/benchmark.length.c @@ -94,29 +94,49 @@ static double rand_double( void ) { * @param len array length * @return elapsed time in seconds */ -static double benchmark( int iterations, int len ) { +static double benchmark(int iterations, int len) { double elapsed; - double x[ len ]; - double y[ len ]; + double *x; + double *y; double t; int i; - for ( i = 0; i < len; i++ ) { - x[ i ] = ( rand_double()*200.0 ) - 100.0; - y[ i ] = 0.0; + // Allocate input array: + x = (double*) malloc(len * sizeof(double)); + if (x == NULL) { + printf("Error allocating memory.\n"); + exit(1); + } + + // Allocate output array: + y = (double*) malloc(len * sizeof(double)); + if (y == NULL) { + free(x); + printf("Error allocating memory.\n"); + exit(1); + } + + for (i = 0; i < len; i++) { + x[i] = (rand_double()*200.0) - 100.0; + y[i] = 0.0; } t = tic(); - for ( i = 0; i < iterations; i++ ) { - stdlib_strided_dabs2( len, x, 1, y, 1 ); - if ( y[ 0 ] != y[ 0 ] ) { - printf( "should not return NaN\n" ); + for (i = 0; i < iterations; i++) { + stdlib_strided_dabs2(len, x, 1, y, 1); + if (y[0] != y[0]) { + printf("should not return NaN\n"); break; } } elapsed = tic() - t; - if ( y[ 0 ] != y[ 0 ] ) { - printf( "should not return NaN\n" ); + if (y[0] != y[0]) { + printf("should not return NaN\n"); } + + // Free allocated memory: + free(x); + free(y); + return elapsed; }