为什么打电话给segfault

问题描述 投票:0回答:1
int ** MakeMoveSet(Comparison_t * comp) { int **moveset, *path; int64_t n = comp->file1LineCount, m = comp->file2LineCount; int64_t max_tries = n + m; int64_t max_index = 2 * max_tries + 1; int64_t prev, next; int x,y; fprintf(stderr, "max_tries=%ju, max_index=%ju\n", max_tries, max_index); path = malloc(sizeof(int) * max_index); //Array of ints; moveset = malloc(sizeof(path) * max_tries); //Array of paths path[1] = 0; for (int d = 0; d < max_tries; d++) { for (int k = -d; k <=d; k+=2) { fprintf(stderr, "d=%d,k=%d\n", d, k); prev = k - 1; next = k + 1; // Determine whether to move up or down if (k == -d || (k != d && path[prev] < path[next])) { x = path[next]; } else { x = path[prev] + 1; } y = x - k; // Check diagonals to settle x at deepest point while ((x < n && y < m) && (strcmp(comp->file1Lines[x], comp->file2Lines[y]) == 0)) { x ++; y ++; } path[k] = x; fprintf(stderr, "x = %d, y=%d\n", x, y); if (x >= n && y >= m) { free(path); //Segfault occurs here fprintf(stderr, "Bailing\n"); comp->shortest_edit_path = d; fprintf(stderr, "Shortest edit path = %d\n", d); return moveset; } } } return NULL; }

为什么当使用malloc完成分配给路径的内存时,这是出现的,而自由呼叫进行时,路径不是null?

	

没有用于调用此功能的结构和代码的支持代码,我使用了一些艺术许可来定义文件路径比较结构。

typedef struct cmp { int64_t file1LineCount; char file1Lines[20][100]; int64_t file2LineCount; char file2Lines[20][100]; int64_t shortest_edit_path; } Comparison_t;
c algorithm diff
1个回答
0
投票
,然后在“主”函数中构建了一个非常简单的函数调用。

int main() { Comparison_t * work = malloc(sizeof(Comparison_t)); work->file1LineCount = 2; strcpy(work->file1Lines[0], "C_Programs/Console/FileCompare/bin/Release"); work->file2LineCount = 2; strcpy(work->file2Lines[0], "C_Programs/Console/FileCompare/bin/Test"); work->shortest_edit_path = 55; int ** test = MakeMoveSet(work); printf("Address of test: %p\n", test); return 0; }

最后,添加了一个简单的“ printf”语句,该语句重点放在可能的错误上,如良好的评论中所指出的有关数据可能分配的数组中的数组。
path[k] = x; printf("k value: %d\n", k); /* Visual debug print statement */ fprintf(stderr, "x = %d, y=%d\n", x, y); if (x >= n && y >= m) { free(path); //Segfault occurs here fprintf(stderr, "Bailing\n"); comp->shortest_edit_path = d; fprintf(stderr, "Shortest edit path = %d\n", d); return moveset; }

在执行代码测试的情况下,表明确实有一些数据分配到“路径”阵列中的某些数据分配是出于界限(例如,“ k”的值是一个负值),这随后会导致不确定的行为。
craig@Vera:~/C_Programs/Console/FileCompare/bin/Release$ ./FileCompare 
max_tries=4, max_index=9
d=0,k=0
k value: 0
x = 0, y=0
d=1,k=-1
k value: -1
x = 0, y=1
d=1,k=1
k value: 1
x = 1, y=0
d=2,k=-2
k value: -2
x = 0, y=2
d=2,k=0
k value: 0
x = 2, y=2
free(): invalid pointer
Aborted (core dumped)

这里的简单答案是,需要进行进一步的范围测试,以便不会发生分配的内存或数组边界之外的数据分配。 作为简单的重构,添加了以下边界测试。

if (k >= 0 && k < max_tries) /* Boundary testing */ path[k] = x; printf("k value: %d\n", k); /* Visual debug print statement */ fprintf(stderr, "x = %d, y=%d\n", x, y); if (x >= n && y >= m) { free(path); //Segfault occurs here fprintf(stderr, "Bailing\n"); comp->shortest_edit_path = d; fprintf(stderr, "Shortest edit path = %d\n", d); return moveset; }
这将导致以下终端输出。

craig@Vera:~/C_Programs/Console/FileCompare/bin/Release$ ./FileCompare max_tries=4, max_index=9 d=0,k=0 k value: 0 x = 0, y=0 d=1,k=-1 k value: -1 x = 0, y=1 d=1,k=1 k value: 1 x = 1, y=0 d=2,k=-2 k value: -2 x = 0, y=2 d=2,k=0 k value: 0 x = 2, y=2 Bailing Shortest edit path = 2 Address of test: 0x563c29420280

不知道程序的完整上下文,边界测试可能需要更复杂;但是,确实需要添加边界测试。
couth可能是在创建,使用和交易中的更多教程以及调试工具的使用情况和/或添加临时打印声明以磨损可能的问题。
    

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.