Skip to content

Commit abc78da

Browse files
committed
fix(math/strided/special/dfloor): replace stack allocation with dynamic allocation in benchmark
Replace fixed-size array declarations with dynamic memory allocation in the dfloor benchmark to prevent segmentation faults when processing large input sizes. The benchmark previously failed with segmentation faults for arrays with 1,000,000 elements due to stack memory limitations. Changes: - Replace stack-allocated arrays with malloc/free for x and y arrays - Add proper error handling for memory allocation failures - Add memory cleanup to prevent memory leaks - Add error checking in main benchmark loop Fixes: #7224 --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: missing_dependencies - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent 398ac1c commit abc78da

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

lib/node_modules/@stdlib/math/strided/special/dfloor/benchmark/c/benchmark.length.c

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,24 @@ float rand_uniformf( float a, float b ) {
114114
*/
115115
static double benchmark( int iterations, int len ) {
116116
double elapsed;
117-
double x[ len ];
118-
double y[ len ];
117+
double *x;
118+
double *y;
119119
double t;
120120
int i;
121121

122+
// Allocate memory dynamically to avoid stack overflow
123+
x = malloc( len * sizeof(double) );
124+
if ( x == NULL ) {
125+
printf( "Error: failed to allocate memory for x array\n" );
126+
return -1.0;
127+
}
128+
y = malloc( len * sizeof(double) );
129+
if ( y == NULL ) {
130+
printf( "Error: failed to allocate memory for y array\n" );
131+
free( x );
132+
return -1.0;
133+
}
134+
122135
for ( i = 0; i < len; i++ ) {
123136
x[ i ] = rand_uniform( -10.0, 10.0 );
124137
y[ i ] = 0.0;
@@ -136,6 +149,9 @@ static double benchmark( int iterations, int len ) {
136149
if ( y[ 0 ] != y[ 0 ] ) {
137150
printf( "should not return NaN\n" );
138151
}
152+
// Free allocated memory
153+
free( x );
154+
free( y );
139155
return elapsed;
140156
}
141157

@@ -162,6 +178,10 @@ int main( void ) {
162178
count += 1;
163179
printf( "# c::%s:len=%d\n", NAME, len );
164180
elapsed = benchmark( iter, len );
181+
if ( elapsed < 0.0 ) {
182+
printf( "not ok %d benchmark failed - memory allocation error\n", count );
183+
continue;
184+
}
165185
print_results( iter, elapsed );
166186
printf( "ok %d benchmark finished\n", count );
167187
}

0 commit comments

Comments
 (0)