簡體   English   中英

如何在Ubuntu中使用Code :: Blocks解決C中的Segmentation Fault(core dumped)錯誤?

[英]How to solve Segmentation fault(core dumped) error in C using Code::Blocks in Ubuntu?

我正在使用Code :: Blocks IDE在C中運行鏈接的隊列數據結構。 下面是我的代碼:

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

typedef struct LinkedQueue
{
    int data;
    struct LinkedQueue *next;
}Q;
Q *front , *rear;

int QEmpty( Q * );
void QInsert();
Q *QDelete();
void QDisplay( Q* );


int main()
{
    int ans, choice;
    front = NULL;
    rear = NULL;
    do
    {
        printf("\n::Linked Queue Menu::");
        printf("\n  1. Insert ");
        printf("\n  2. Delete ");
        printf("\n  3. Display ");
        printf("\n  4. Exit ");
        printf("\n Enter your choice:");
        scanf("%d", &choice);
        switch( choice )
        {
            case 1: QInsert();
                    break;

            case 2: front = QDelete();
                    break;

            case 3: QDisplay(front);
                    break;

            case 4: exit(0);

        }
        printf("\nDo you want to go to main menu? (1/0) :");
        scanf("%d", &ans);
    }while ( ans == 1 );
    return 0;
}



void QInsert()
{
    Q *temp;
    temp = (Q*)malloc(sizeof(Q));
    temp->next = NULL;
    if( temp == NULL)
    {
        printf("\nMemory cannot be allocated");
        return;
    }
    printf("\nEnter the data:");
    scanf("%d", &temp->data);
    if( QEmpty( front ) )
    {
        front = temp;
        rear = temp;
    }
    else
    {
        rear->next = temp;
        rear = rear->next;
    }
    printf("\nInsertion Successful");

}

int QEmpty( Q *front )
{
    if( front == NULL )
        return 1;
    else
        return 0;
}

Q *QDelete()
{
    Q *temp;
    temp = front;
    if( QEmpty( front ) )
    {
        printf("\nQueue Underflow!!");
        printf("\nReturning to main menu . . ");
        return NULL;
    }
    else
    {
        printf("\nThe deleted element is %d.", temp->data);
        front = front->next;
        temp->next = NULL;
        free( temp );
    }
    return front;
}

void QDisplay( Q *front )
{
    if( QEmpty( front ) )
    {
        printf("\nThe Queue is empty");
    }
    printf("\nThe display of Queue is:\n");
    printf("!!!->");
    do
    {
        printf("%d", front->data);
        front = front->next;
    }while( front != rear );
}

當我運行該程序時,插入和刪除功能可以正常工作。 但是,當我調用顯示函數時,它導致了分段錯誤(核心已轉儲)。

請幫助我如何解決此錯誤。 提前致謝。

在函數QDisplay ,您檢查指針是否為null,但如果該條件為true,則不退出,而是繼續取消對指針的引用,這將導致段錯誤。 如果隊列為空,則需要修改返回方法。 例如

void QDisplay( Q *front )
{
    if( QEmpty( front ) )
    {
        printf("\nThe Queue is empty");
        // Two lines below added by me
        printf("\nReturning to main menu . . ");
        return;
    }
 ...

從@evolvedmicrobe答案顯示的錯誤QDisplay的情況下,您的列表是空的(即當front為NULL)。

這個答案將集中在QDisplay另一個錯誤上。

考慮當列表僅包含1個元素時將發生什么。 在這種情況下frontrear是相同的。 進一步的front->next為NULL。

因此,按順序1),2),3)和4)看我的評論:

void QDisplay( Q *front )
{
    if( QEmpty( front ) )
    {
        printf("\nThe Queue is empty");
    }
    printf("\nThe display of Queue is:\n");
    printf("!!!->");
    do
    {
        printf("%d", front->data);  // 1) In first loop this will print the data from the first element (fine)
                                    // 4) In the second loop you deference front which is NULL => seg fault

        front = front->next;        // 2) Now front gets the value NULL

    }while( front != rear );        // 3) Front and rear differs so the loop continuees
}

(注意:除了恰好有1個元素時會引起錯誤外,您的方法對於“超過1個元素”也是不利的,因為您永遠不會打印最后一個元素)

更好的方法是:

void QDisplay( Q *front )
{
    if( QEmpty( front ) )
    {
        printf("\nThe Queue is empty");
        return;
    }
    printf("\nThe display of Queue is:\n");
    printf("!!!->");
    do
    {
        printf("%d", front->data);
        front = front->next;
    }while( front != NULL );   // or just } while(front);
}

附加注意事項:

您的代碼使用全局變量frontrear 通常,我建議您不要使用全局變量。 這令人困惑並且容易出錯。 相反,您可以在main定義它們,並將指向它們的指針傳遞給需要更改它們的函數。 或者-也許更好-將它們放置在結構中,以便您可以將指針傳遞給該結構的變量(在main定義)。

如果確實確實需要全局變量,則強烈建議您不要對局部變量和函數參數使用相同的名稱。 換句話說: QDisplay當前有一個名為front的參數-就像全局變量front 這是一種混亂...當你將東西會發生什么front里面QDisplay 全球front改變嗎? 答案是否定的,但要避免造成混淆,最好為函數參數使用另一個名稱。 喜歡:

void QDisplay( Q *p )
{
    if( QEmpty( p ) )
    {
        printf("\nThe Queue is empty");
        return;
    }
    printf("\nThe display of Queue is:\n");
    printf("!!!->");
    do
    {
        printf("%d", p->data);
        p = p->next;
    }while( p != NULL );   // or just } while(p);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM