运行排序测试C程序触发Segmentation fault错误的技术求助
Let's break down why you're hitting that segmentation fault, starting with the most critical issue:
1. Mismatched Parameter Type in finput_array Calls
Your finput_array function is defined to take a pointer to int as its third parameter (int *n), because it needs to modify the value of n if the input file has fewer elements than expected. But in your main function, you're passing the integer n directly instead of its address:
In main:
finput_array(argv[2], a, n); // Wrong: passing int n instead of &n // ... finput_array(argv[3], oracle, n); // Same mistake here
When you pass n (a value like 12) where a pointer is expected, the function will treat that value as a memory address. Dereferencing it (*n inside finput_array) means you're trying to read/write to memory at address 0xC (since 12 in hex is 0xC), which is completely invalid—this is almost certainly the cause of your segmentation fault.
Fix for This Issue
Change the calls to pass the address of n using the & operator:
finput_array(argv[2], a, &n); // ... finput_array(argv[3], oracle, &n);
2. Potential Off-by-One Error in finput_array
Looking at the logic inside finput_array:
fscanf(fd, "%d", &a[i]); while(i<*n && !feof(fd)){ i++; fscanf(fd, "%d", &a[i]); }
If your input file has exactly n elements, this will try to read n+1 values: you read the first element into a[0], then increment i to 1 and read a[1], up to i = n where you'll try to write to a[n]—which is beyond the bounds of your array (since arrays are 0-indexed, valid indices are 0 to n-1). This could also cause a segmentation fault or corrupted data.
Fix for This Issue
Adjust the loop to read elements correctly without overwriting:
int i = 0; while (i < *n && fscanf(fd, "%d", &a[i]) == 1) { i++; }
This uses fscanf's return value (which is the number of successfully read items) to control the loop, avoiding relying solely on feof (which can have edge cases) and preventing the off-by-one write.
3. Other Minor Issues to Clean Up
- In
main, after callingcalloc, you shouldfreethe allocated arrays (aandoracle) to avoid memory leaks. - When checking
argc != 5, you should probably return an error code (likereturn 1;) instead of just printing a message, since the rest of the code will still execute if you don't exit. - In
finput_array, iffopenfails, you might want to add anexitor return early, since continuing to read from a NULL file pointer will cause issues.
Updated main with Fixes & Cleanup
#include <stdio.h> #include <stdlib.h> #include "vettore.h" int main(int argc, char *argv[]){ if(argc != 5) { printf("Incorrect parameters number\n"); return 1; // Exit with error code } int n = atoi(argv[1]); int *a = (int*) calloc(n, sizeof(int)); if(a == NULL) { printf("Insufficient memory\n"); return 1; } finput_array(argv[2], a, &n); bubblesort(a, n); foutput_array(argv[4], a, n); int *oracle = (int*) calloc(n, sizeof(int)); if(oracle == NULL) { printf("Insufficient memory\n"); free(a); // Clean up before exiting return 1; } finput_array(argv[3], oracle, &n); if(compare_array(a, oracle, n)) { printf("PASS\n"); } else { printf("FAIL\n"); } // Free allocated memory free(a); free(oracle); return 0; }
Start with fixing the parameter passing issue first—that's the immediate culprit for your segmentation fault. Once you fix that, test again and address any remaining issues if they pop up.
内容的提问来源于stack exchange,提问作者user11006304

