Valgrind在某些方面抱怨我的代码,我真的不知道为什么..我基本上已经完成了合并排序(降序)的代码并对其进行了调整,因为我希望以后不要有排序列表,而只是它的元素现在的顺序,所以如果我有:
5 8 1 2
[0] [1] [2] [3],
排序后是:
8 5 2 1
[1] [0] [3] [2]< ==这就是我想要的
所以我的代码分为三部分:
int main(int argc, char** argv) {
int *llist, *array_ordered;
int end,fst, i ; //will be set somehow
array_ordered = malloc(sizeof(int)*(end-fst));
llist = malloc(sizeof(int)*(end-fst)); //filled with some numbers
for(i =0; i < (end-fst); i++) array_ordered[i] = i;
sort_list(end-fst, llist, array_ordered);
}
void sort_list(int total, int *llist, int *array_ordered) {
int i, j, temp, *temp_array;
temp_array = malloc(sizeof(int)*total);
for (i = 0; i < total; i++) {
temp_array[i] = llist[i]; // copying the values, because I need my original list to keep
}
merge_sort(temp_array, total, array_ordered);
free(temp_array);
}
void merge_sort(int *A, int total_el, int *array_ordered) {
int i;
int *list_one, *list_two;
int *order_one, *order_two;
int num1, num2;
if (total_el < 2) return; /* the array is sorted when n=1.*/
/* divide A into two parths the list, and the positions (array_ordered)*/
num1 = total_el / 2; /* the number of elements in A1 */
num2 = total_el - num1; /* the number of elements in A2 */
list_one = malloc(sizeof(int)*num1);
list_two = malloc(sizeof(int)*num2);
order_one = malloc(sizeof(int)*num1);
order_two = malloc(sizeof(int)*num2);
/* move the first n/2 elements to A1 */
for (i =0; i < num1; i++) {
list_one[i] = A[i];
order_one[i] = array_ordered[i]; <=== invalid read of size 4
}
for (i = 0; i < num2; i++) {
list_two[i] = A[i+num1];
order_two[i] = array_ordered[i+num1]; <=== invalid read of size 4
}
merge_sort(list_one, num1, order_one);
merge_sort(list_two, num2, order_two);
merge(list_one, num1, list_two, num2, A, array_ordered, order_one, order_two);
free(list_one);
free(list_two);
free(order_one);
free(order_two);
}
void merge (int *A, int a, int *B, int b, int *C, int *array_ordered, int *order_one, int *order_two) {
int i,j,k;
i = j = k = 0;
while (i < a && j < b) {
if (A[i] >= B[j]) {
/* copy A[i] to C[k] and move the pointer i and k forward */
C[k] = A[i];
array_ordered[k] = order_one[i]; <=== Invalid write of size 4!
i++;
k++;
}
else {
/* copy B[j] to C[k] and move the pointer j and k forward */
C[k] = B[j];
array_ordered[k] = order_two[j];
j++;
k++;
}
}
/* move the remaining elements in A into C */
while (i < a) {
C[k]= A[i];
array_ordered[k] = order_one[i];
i++;
k++;
}
/* move the remaining elements in B into C */
while (j < b) {
C[k]= B[j];
array_ordered[k] = order_two[j];
j++;
k++;
}
}
我不知道,因为所有列表都有相同的总数,为什么我只会在非法位置列表中的某一点写:S
==1373== Invalid read of size 4
==1373== at 0x10001F1D5: merge_sort (main.c:5586)
==1373== by 0x10001D2E0: sort_list (main.c:5512)
==1373== by 0x10000A164: my_function (main.c:1014)
==1373== by 0x1000032E5: main (main.c:450)
==1373== Address 0x244fd9a70 is 0 bytes inside a block of size 1,512 free'd
==1373== at 0x4D9D: free (vg_replace_malloc.c:479)
==1373== by 0x10000A1A4: my_function (main.c:1020)
==1373== by 0x1000032E5: main (main.c:450)
and
==1373== Invalid write of size 4
==1373== at 0x10001F3BC: merge (main.c:5529)
==1373== by 0x10001F2C0: merge_sort (main.c:5602)
==1373== by 0x10001D2E0: sort_list (main.c:5512)
==1373== by 0x10000A164: my_function (main.c:1014)
==1373== by 0x1000032E5: main (main.c:450)
==1373== Address 0x244fd9a70 is 0 bytes inside a block of size 1,512 free'd
==1373== at 0x4D9D: free (vg_replace_malloc.c:479)
==1373== by 0x10000A1A4: my_function (main.c:1020)
==1373== by 0x1000032E5: main (main.c:450)