malloc:释放对象的校验和不正确
下面是进行合并排序的函数。但我在执行时遇到了错误。已分配内存(aux)每次在函数malloc:释放对象的校验和不正确,c,recursion,malloc,mergesort,C,Recursion,Malloc,Mergesort,下面是进行合并排序的函数。但我在执行时遇到了错误。已分配内存(aux)每次在函数merge中都被释放,为什么释放后会对其进行修改 malloc((hi-lo+1)*sizeof(int))为索引从0到hi-lo的元素分配空间,但是对于(int k=lo;kaux数组未正确初始化:对于(int k=lo;k=n|aux[i] a.out(65287,0x1112bedc0) malloc: Incorrect checksum for freed object 0x7ff9a4c05888: pr
merge
中都被释放,为什么释放后会对其进行修改
malloc((hi-lo+1)*sizeof(int))
为索引从0到hi-lo
的元素分配空间,但是对于(int k=lo;kaux
数组未正确初始化:对于(int k=lo;k=n|aux[i]
a.out(65287,0x1112bedc0) malloc: Incorrect checksum for freed object
0x7ff9a4c05888: probably modified after being freed. Corrupt value:
0xb00000003 a.out(65287,0x1112bedc0) malloc: *** set a breakpoint in
malloc_error_break to debug Abort trap: 6
void merge(int arr[], int lo, int mid, int hi) {
int i = lo;
int j = mid + 1;
int *aux = (int *)malloc((hi - lo + 1) * sizeof(int));
for (int k = lo; k <= hi; k++) {
aux[k] = arr[k];
}
for (int k = lo; k <= hi; k++) {
if (i > mid)
arr[k] = aux[j++];
else if (j > hi)
arr[k] = aux[i++];
else if (aux[i] > aux[j])
arr[k] = aux[j++];
else
arr[k] = aux[i++];
}
free(aux);
}
void mergesort1(int arr[], int lo, int hi) {
if (lo >= hi)
return;
int mid = lo + (hi - lo) / 2;
mergesort1(arr, lo, mid);
mergesort1(arr, mid + 1, hi);
merge(arr, lo, mid, hi);
}
mergesort1(arr, 0, 9);
for (int k = lo; k <= hi; k++) {
aux[k -lo] = arr[k];
}