簡體   English   中英

總線錯誤:C處理struct指針的10

[英]Bus Error: 10 in C dealing with struct pointers

我正在使用C中的List示例,其中新節點被推送到堆棧的末尾。 當我嘗試將新節點推送到最后時,我不斷收到Bus Error: 10 這是我的推送功能:

void push(struct node *tail, struct node *newNode) {

tail->next = newNode; // gdb says the problem is here
tail = tail->next;

}

我用push(tail, newNode);調用它push(tail, newNode);

如有必要,這里也是我的結構:

struct node
{
    int hour;
    int minute;
    char *name;
    struct node *next;
};

這是顯示導致push()代碼的主要功能

int main()

{
char inputString[50];
int timeHour, timeMin;  
struct node *head;
struct node *tail;

while ((scanf("%d:%d", &timeHour, &timeMin)) != EOF) {
    scanf("%s", inputString);

    if (strcmp(inputString, "enqueue") == 0) {
        if (head == NULL) {
            head = malloc(sizeof(struct node));

            head->hour = timeHour;
            head->minute = timeMin;

            // get name
            scanf("%s", inputString);
            head->name = malloc(strlen(inputString)+1);
            strcpy(head->name, inputString);

            tail = head;

            printEnqueue(head);
        } else {
            struct node *newEntry = malloc(sizeof(struct node));

            newEntry->hour = timeHour;
            newEntry->minute = timeMin;

            // get name
            scanf("%s", inputString);
            newEntry->name = malloc(strlen(inputString)+1);
            strcpy(newEntry->name, inputString);

            push(tail, newEntry);

            printEnqueue(newEntry);
        }
    } else {
        pop(&head, timeHour, timeMin);
    }
}

return 0;
}

我懷疑main函數中的headtail節點沒有正確初始化。

從你的代碼看,如果headNULL ,那么head將被分配一個新節點。 但是,你對head的定義並不能確保它最初為NULLtail也不是)。 所以你可以繞過if (head == NULL)分支(確保它們確實是從gdb執行的,請:))。

Bus error很少見。 所以我用谷歌搜索,從這里 ,可能會發生總線錯誤

使用處理器指令,其地址不滿足其對齊要求。

這可能是因為tail沒有對齊而代碼直接運行到else分支。 所以push(tail, newEntry); 將訪問未對齊的尾部(這也驗證了我的嫌疑人)。

修正#3: while ((scanf("%d:%d", &timeHour, &timeMin)) != EOF)在此循環的主體內,無法保證將timeHourtimeMin分配給兩個整數。 也許你的意思是while ((scanf("%d:%d", &timeHour, &timeMin)) == 2)


修正#2:當您將值傳遞給函數時,您傳遞的是 ,而不是變量。 調用者(您的main )無法看到您在push進行tail的任務。 你需要傳入一個指向該變量的指針(例如。 &head ,這是一個struct node ** ),然后像以前一樣分配給*tail 或者,您可以return newNode; 從你的push和使用返回值作為你的新head


修正案:這甚至看起來都不會編譯。 我們來看看push

void push(struct node **tail, struct node *newNode) {
    (*tail)->next = *newNode; // gdb says the problem is here
    *tail = (*tail)->next;
}

*newNode的類型是什么? struct node 什么是(*tail)->next 這是在這個片段中:

struct node
{
    int hour;
    int minute;
    char *name;
    struct node *next;
};

修復您的不一致性,並確保在發布之前,您的最小,可編譯的測試用例是可編譯的


別忘了檢查scanf的返回值! 在您的情況下,除非發生錯誤,否則應返回1。


        head->name = malloc(strlen(inputString));
        strcpy(head->name, inputString);

這是錯誤的,因為你沒有分配足夠的空間來存儲'\\0'字符。 我認為你的意思是malloc(strlen(inputString) + 1) 您的代碼中存在兩個此錯誤的實例。 我不打算重復自己。


        struct node *newEntry = malloc(sizeof(struct node));
        push(&tail, newEntry);

newEntry的類型是newEntry struct node *

        void push(struct node **tail, struct node **newNode)

newNode的類型是什么? struct node ** 你看到不一致嗎? 您需要傳入struct node ** ,但newEntry是一個struct node *

更改

void push(struct node *tail, struct node *newNode) 
{
  tail->next = newNode; // gdb says the problem is here
  tail = tail->next;
}

void push(struct node **tail, struct node *newNode) 
{
  (*tail)->next = newNode; // gdb says the problem is here
  (*tail) = (*tail)->next;
}

然后改為這樣稱呼它

push(&tail, newEntry);

正如你所擁有的那樣,'tail'永遠不會改變,因為你沒有將變量的地址傳遞給函數,因此你無法改變它所指向的內容。

還要確保初始化所有局部變量(標題,尾部,......), 養成習慣

暫無
暫無

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

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