1

私のコードでは、変数を次のclass_tに進めようとしているにもかかわらず、検索と呼ばれる同じclass_t変数を出力する無限ループがあるようです。すべての class_t 構造体は (class_t*)0 を指すか (class_t* と void* を比較していたために NULL を使用するとコンパイラの警告が表示されたため)、または次の適切な class_t 構造体を指します。私は何を間違っていますか、それとも私の問題を別の場所で探す必要がありますか?

class_t *search = (students + i)->class_p;//students is a seperate structure where class_p is a pointer to a class_t structure
            while(search != (class_t*)0)
            {
                    fprintf(output,": %s%d %d %d\n", search->name, search->number, search->section, search->credits);
                    search = search->nextClass;
            }

出力のサンプルを次に示します。これを見ると、ファイルから class_t で最後に読み取られたものです。

: CS521 1 4
: CS521 1 4
: CS521 1 4
: CS521 1 4
: CS521 1 4
: CS521 1 4
: CS521 1 4
: CS521 1 4
: CS521 1 4
: CS521 1 4
: CS521 1 4
: CS521 1 4

class_t の作成は次のとおりです。

    class_t newClass;
newClass.number = classid;
newClass.section = section;
newClass.credits = credits;
newClass.nextClass = (class_t*)0;

ノードが追加されると、次のようになります。

void addNode(student_t students[], class_t addClass, int ref)
{
int found = 0;

if((students + ref)->class_p == (class_t*)0)//no classes yet
{
    (students + ref)->class_p = &addClass;
    found = 1;
}
else if((*((students + ref)->class_p)).number > addClass.number && found == 0)//checks first class
{
    class_t *temp = (students + ref)->class_p;
    (students + ref)->class_p = &addClass;
    addClass.nextClass = temp;
    found = 1;
}
else//works way through the class linked list to find where it goes
{
    class_t *beforesearch = (students + ref)->class_p;
    class_t *search = beforesearch->nextClass;
    while(search != (class_t*)0 && found == 0)
    {
        if(search->number < addClass.number)
        {
            beforesearch->nextClass = &addClass;
            addClass.nextClass = search;
            found = 1;
        }
        else
        {
            beforesearch = search;
            search = search->nextClass;
        }
    }

    if(found == 0)
    {
        beforesearch->nextClass = &addClass;
        found = 1;
    }
}

}

typedef を含むヘッダー ファイル:

typedef struct class_t {
char name[3];
int number;
int section;
int credits;
struct class_t *nextClass;
} class_t;

typedef struct student_t {
int id;
class_t *class_p;
} student_t;
4

1 に答える 1

1

これは非常に微妙なエラーです。

void addNode(student_t students[], class_t addClass, int ref)
{
    int found = 0;

    if((students + ref)->class_p == (class_t*)0)//no classes yet
    {
        (students + ref)->class_p = &addClass;

addClass値(つまり、私が推測する構造体のコピー全体)を渡し、そのアドレスを使用してリスト内でリンクします。呼び出しスタックに属する関数パラメーターのアドレスを使用しているため、これは誤りです。

リストループが発生している場合は、呼び出しごとaddNodeに構造体をスタック内の同じアドレスにコピーするケースに遭遇したことを意味します。しかし、これは幸運です。このコードで問題が発生する可能性のあることが非常に多いため、すべてを説明することはできません。

class_t適切な解決策は、ヒープ上にノードを割り当て(つまり、を使用してmalloc())、それらにポインターを渡すことです。または、リンクする前にコピーを割り当てます。

void addNode(student_t students[], class_t addClass_param, int ref)
{
    class_t *addClass = malloc(sizeof(class_t)); /* Check for NULL return skipped */
    memcpy(addClass, &addClass_param, sizeof(class_t));
    /* ... */
于 2012-04-19T23:07:07.880 に答える