0

こんにちは 私は現在、ファイルを読み込んで情報を使用し、別のファイルを出力するという割り当てを行っています。すべて双方向リンクリストを使用しています。現在、ファイルを二重にリンクされたリストに読み込み、画面とファイルに出力し、最後にリストを削除してプログラムを閉じようとしています。文字列を削除するはずの dlist_distroy 関数を呼び出さない限り、プログラムは正常に動作します。私がそれを行うとすぐに、プログラムが実行を開始し、ウィンドウがポップアップして、

"Windows has triggered a breakpoint in tempfilter.exe.

This may be due to a corruption of the heap, which indicates a bug in tempfilter.exe or any of the DLLs it has loaded.

This may also be due to the user pressing F12 while tempfilter.exe has focus.

The output window may have more diagnostic information."

破棄および削除機能を修正しましたが、問題を理解できません。私のプログラムは次のとおりです

main.c

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "dlinklist.h"
#include "DlistElmt.h"
#include "Dlist.h"
#include "dlistdata.h"

/****************************************************************************/

int main (int argc, char *argv[])
{
    FILE *ifp, *ofp;
    int hour, min;
    Dlist *list;
    DlistElmt *current = NULL, *current2 = NULL;
    float temp;

    list  = (Dlist *)malloc(sizeof(list));
    element  = (DlistElmt *)malloc(sizeof(element));

    if (argc != 3) { /* argc should be 3 for correct execution */
        /* We print argv[0] assuming it is the program name */

        /* TODO: This is wrong, it should be: usage: %s inputfile outputfile */
        printf( "usage: %s filename", argv[0] );
    } else {
        // We assume argv[1] is a filename to open
        ifp = fopen(argv[1], "r");
        if (ifp == 0) {
            printf("Could not open file\n");
        } else {
            ofp = fopen(argv[2], "w");
            dlist_init(list);//, (destroy)(hour, min, temp));
            while (fscanf(ifp, "%d:%d %f ", &hour, &min, &temp) == 3) {
                current=list->tail;
                if (dlist_size(list) == 0) {
                    dlist_ins_prev(list, current, hour, min, temp);
                } else {
                    dlist_ins_next(list, current, hour, min, temp);
                }
            }
            current = list->head;
            while (current != NULL) {
                if (current==list->head) {
                    current=current->next;
                } else
                    if ((current->temp > (current->prev->temp +5)) || 
                            (current->temp < (current->prev->temp -5))) {
                        current2 = current->next
                            dlist_remove(list, current);
                        current = current2;
                    } else
                        current=current->next;
            }

            current = list->head;
            while(current != NULL) {
                printf("%d:%d %2.1lf\n",
                    current->time, 
                    current->time2, 
                    current->temp
                );
                fprintf(ofp, "%d:%d %2.1lf\n", 
                    current->time, 
                    current->time2, 
                    current->temp
                );
                current = current->next;
            }
            //dlist_destroy(list);
            //}

            fclose(ifp);
            fclose(ofp);
        }
    }

    getchar();
}

dlistdata.c

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "dlinklist.h"
#include "DlistElmt.h"
#include "dlistdata.h"

/****************************************************************************/

void dlist_init(Dlist *list)
{
    list->size = 0;
    list->head = NULL;
    list->tail = NULL;
    return;
}

void dlist_destroy(Dlist *list) {
    while (dlist_size(list) > 0) {
        dlist_remove(list, list->head);
    }
    memset(list, 0, sizeof(Dlist));

    return;
}

int dlist_ins_next(Dlist *list, DlistElmt *element, const int time, 
        const int time2, const float temp)
{
    DlistElmt *new_element;

    if (element == NULL && dlist_size(list) != 0)
        return -1;
    if ((new_element = (DlistElmt *)malloc(sizeof(new_element))) == NULL)
        return -1;

    new_element->time  = (int)time;
    new_element->time2 = (int)time2;
    new_element->temp  = (float)temp;

    if (dlist_size(list) == 0) {
        list->head = new_element;
        list->head->prev = NULL;
        list->head->next = NULL;
        list->tail = new_element;
    } else {
        new_element->next = element->next;
        new_element->prev = element;

        if (element->next == NULL)
            list->tail = new_element;
        else
            element->next->prev = new_element;
        element->next = new_element;
    }

    list->size++;

    return 0;
}

int dlist_ins_prev(Dlist *list, DlistElmt *element, const int time, 
        const int time2, const float temp)
{
    DlistElmt *new_element;

    if (element == NULL && dlist_size(list) != 0)
        return -1;

    if ((new_element = (DlistElmt *)malloc(sizeof(new_element))) == NULL)
        return -1;

    new_element->time  = (int)time;
    new_element->time2 = (int)time2;
    new_element->temp  = (float)temp;

    if (dlist_size(list) == 0){
        list->head = new_element;
        list->head->prev = NULL;
        list->head->next = NULL;
        list->tail = new_element;
    } else {
        new_element->next = element;
        new_element->prev = element->prev;

        if (element->prev == NULL)
            list->head = new_element;
        else
            element->prev->next = new_element;
        element->prev = new_element;
    }

    list->size++;

    return 0;
}

int dlist_remove(Dlist *list, DlistElmt *element)
{ /*, int time, int time2, float temp){ */

    if (element == NULL || dlist_size(list) == 0)
        return -1;
    if (element == list->head) {
        list->head = element->next;
        if (list->head == NULL)
            list->tail = NULL;
        else
            element->next->prev = NULL;
    } else {
        element->prev->next = element->next;
        if (element->next == NULL)
            list->tail = element->prev;
        else
            element->next->prev = element->prev;
    }

    free(element);

    list->size--;

    return 0;
}
4

2 に答える 2

3

この行は悪いニュースです:

if (element->next = NULL)( の下部付近dlistdata.c)

かどうかをチェックNULLする代わりに、をに割り当てます。element->nextNULL

===

于 2012-10-09T03:36:20.813 に答える
1

これは課題なので、私の答えは正しい方向を示すことですが、完全に説明することはしません。

ここで何が起こると思いますか?

dlist_remove(list, current);
current = current->next;
于 2012-10-09T03:39:42.617 に答える