排序数组指针后释放内存时出错

时间:2017-05-11 10:02:15

标签: c arrays pointers memory-management

我如何能够保持已分配内存的位置,以便释放已排序数组的内存?

我正在尝试对指针数组进行排序。我注意到当我释放单词双指针变量时,它会给出错误 HEAP CORRUPTION DETECTED 。我输入的输入是“f ff 1”。

未分类:f ff 1 排序:1 f ff

我注意到,当我排序并释放它时,它会期望相同的顺序是“f ff 1”。这就是我收到错误的原因。

关于如何释放排序指针数组的任何建议?

#include <stdio.h>

/*
    A logical type
 */
typedef enum {
    false,
    true,
} bool;

/*
    Bubble Sort
*/
void sort(char *myargv[], int n)
{
    int i, j, cmp;
    char tmp[256];

    if (n <= 1)
        return; // Already sorted

    for (i = 0; i < n; i++)
    {
        for (j = 0; j < n-1; j++)
        {
            cmp = strcmp(myargv[j], myargv[j+1]);

            if (cmp > 0)
            {
                strcpy(tmp, myargv[j+1]);
                strcpy(myargv[j+1], myargv[j]);
                strcpy(myargv[j], tmp);
            }
        }
    }
}

void printArray(char *myargv[], int myargc)
{
    int i = 0;
        for (i = 0; i < myargc; ++i) {
            printf("myargc[%d]: %s\n",i , myargv[i]);
        }
}

int main (int argc, char *argv[])
{
    char text[256];
    char *myargv[256];
    char *myargvTemp[256];
    int myargc;
    int i = 0;
    int text_len;
    bool new_word = false;
    int index_start_word = 0;
    char **words;                                         //this will store the found word
    int count = 0;

    while(1){
        printf( "Enter text:\n");
        gets(text);                                       //get the input
        text_len = strlen(text);                          //get the length of the text
        words = (char **) malloc(text_len * sizeof(char));

        if (strlen(text) == 0 || text == '\0') exit(0);  //exit if text is empty


        for (i = 0; i < text_len ; ++i){
            if(text[i] != ' '){                          //if not space
                if(new_word == false){
                    new_word = true;
                    index_start_word = i;
                }
            } else {
                if (new_word == true) {
                    words[count] = (char *)malloc(i - index_start_word * sizeof(char)+1);   //memory allocation
                    strncpy(words[count], text + index_start_word, i - index_start_word);
                    words[count][i - index_start_word] = '\0';                              //place NULL after the word so no garbage
                    myargv[count] = words[count];
                    new_word = false;
                    count++;
                }

            }

            if (new_word == true && i == text_len-1){
                words[count] = (char *)malloc(i - index_start_word * sizeof(char)+2);
                strncpy(words[count], text + index_start_word, (i+1) - index_start_word);
                words[count][(i+1) - index_start_word] = '\0';
                myargv[count] = words[count];
                new_word = false;
                count++;
            }
        } 

        myargc = count;
        //not sorted
        printf("myargc is: %d\n", myargc);           
        printArray(myargv, myargc);

        //sorting happen
        sort(&myargv, myargc);
        printf("-----sorted-----\n");
        printf("myargc is: %d\n", myargc);
        printArray(myargv, myargc);

        memset(myargv, 0, 255);                         
        count = 0;
        i = 0; 


        //free the memory of words
        for (i=0; i<myargc; ++i) {
                free(words[i]);
        }
    }

    return 0;
}

2 个答案:

答案 0 :(得分:1)

您的代码中至少存在2个问题:

  • 您没有为指针数组分配足够的空间:将words = (char **) malloc(text_len * sizeof(char));更改为:

    words = malloc(text_len * sizeof(char *));
    

    这种分配实际上是错误的:你应该计算单词的数量并为指针数组分配正确的大小,或使用固定大小的数组。

  • 交换字符串的内容而不是交换指针。这是不正确的,因为各种字符串的长度不同。

以下是排序功能的更正版本:

void sort(char *myargv[], int n) {
    int i, j, cmp;

    if (n <= 1)
        return; // Already sorted

    for (i = 0; i < n; i++) {
        for (j = 0; j < n-1; j++) {
            cmp = strcmp(myargv[j], myargv[j+1]);
            if (cmp > 0) {
                char *tmp = myargv[j+1];
                myargv[j+1] = myargv[j];
                myargv[j] = tmp;
            }
        }
    }
}

答案 1 :(得分:0)

您希望单词包含指向char的指针,因此您需要更改

 words = (char **) malloc(text_len * sizeof(char)); //will allocate array of single byte

 words = (char **) malloc(text_len * sizeof(char *));// will allocate array of pointers