检测到堆损坏 - 使用字符串实现mergesort

时间:2015-06-28 21:40:27

标签: c string sorting mergesort heap-corruption

之前我做了一个我自己的“经典”mergesort实现,它对int数组进行了排序。请遵循以下代码:

\documentclass{report}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}
\usepackage{titlesec}

\titleformat{\chapter}[hang]{\bf\huge}{\thechapter}{2pc}{}

\begin{document}

\fontsize{7}{9}\selectfont

\chapter{Example}

Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
Vivamus at lorem vel felis consectetur hendrerit ut nec neque.

\chapter{Example}

Sed eget nunc leo. Nulla efficitur neque diam, aliquet interdum dolor gravida nec.

\end{document}

现在,作为练习,我调整了代码以使其适用于char *数组,但我在某处有堆错误。我试着用Dr. Memory调试它,但是它在没有工作的情况下打破了我的视觉工作室2013,但这是另一个故事。

#include <stdio.h>
#include <stdlib.h>

void merge(int a[], int b[], int c[], int m, int n)
{
    int   i = 0, j = 0, k = 0;

    while (i < m && j < n)
        if (a[i] < b[j])
            c[k++] = a[i++];
        else
            c[k++] = b[j++];
    while (i < m)
        c[k++] = a[i++];
    while (j < n)
        c[k++] = b[j++];
}

void mergesort_pot2(int key[], int n)
{
    int    j, k, *w;

    w = calloc(n, sizeof(int));
    for (k = 1; k < n; k *= 2) {
        for (j = 0; j < n - k; j += 2 * k)
            merge(key + j, key + j + k, w + j, k, k);
        for (j = 0; j < n; ++j)
            key[j] = w[j];
    }
    free(w);
}

void mergesort(int key[], int n)
{
    int i, cnt = 0, *x, v, b = 0, vv = 0, bb;

    while (n - cnt > 0)
    {
        for (i = 1; i < n - cnt; i *= 2);
        if (i > n - cnt)
            i /= 2;

        x = calloc(i, sizeof(int));                     

        v = 0;
        while (v < i)
            x[v++] = key[b++];

        mergesort_pot2(x, i);                           

        bb = 0;
        while (bb < i)                                  
            key[vv++] = x[bb++];

        free(x);                                        
        cnt += i;                                       
    }
}

int main(void)
{
    int   i, key[] = { 4, 3, 1, 67, 55, 8, 0, 4,
        -5, 37, 7, 4, 2, 9, 1, -1, 100};

    printf("Before mergesort:\n");
    for (i = 0; i < 17; ++i)
        printf("%4d", key[i]);
    putchar('\n');

    mergesort(key, 17);

    printf("After mergesort:\n");
    for (i = 0; i < 17; ++i)
        printf("%4d", key[i]);
    putchar('\n');
    return 0;
}

1 个答案:

答案 0 :(得分:2)

void mergesort_pot2(char *key[], int n)
{
    int    j, k;
    char **w;

    w = calloc(n, sizeof(char*));
    for (k = 1; k < n; k *= 2) {
        for (j = 0; j < n - k; j += 2 * k)
            merge(key + j, key + j + k, w + j, k, k);
        for (j = 0; j < n; ++j)
            key[j] = w[j];
    }
    free(w);
}

void mergesort(char *key[], int n)
{
    int i, cnt = 0, v, b = 0, vv = 0, bb;
    char **x;

    while (n - cnt > 0)
    {
        for (i = 1; i < n - cnt; i *= 2);
        if (i > n - cnt)
            i /= 2;

        x = calloc(i, sizeof(char *));

        v = 0;
        while (v < i)
            x[v++] = key[b++];

        mergesort_pot2(x, i);                           

        bb = 0;
        while (bb < i)                                  
            key[vv++]= x[bb++];

        free(x);                                        

        cnt += i;
    }
}