Leet Code运行时错误:代码不能在leet代码上编译,但可以在其他编译器(如netbeans和在线编译器)中编译

4zcjmb1e  于 2023-10-16  发布在  其他
关注(0)|答案(1)|浏览(120)

当我尝试在LeetCode上编译这段代码时,它会抛出这个错误:
第10行错误:Char 19:运行时错误:类型“struct ListNode”的未对齐地址0xbebebebebebebe内的成员访问,需要8字节对齐[solution.c] 0xbebebebebebebe:注意:指针指向此处
我在netbeans上编译了这段代码,如下所示:https://www.onlinegdb.com/online_c_compiler,它工作得很好。我知道LeetCode上的C编译器有点挑剔。但如果你能帮我我可能会想明白为什么会这样.
这是一个简单的链表合并排序的leetcode。第21个问题我不知道还有什么要说的,除了事实上,我需要帮助修复的错误,这是发生在我的推函数行:19.

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

struct ListNode {
    int val;
    struct ListNode *next;
};

void push(int data, struct ListNode* head){
    struct ListNode* newNode = (struct ListNode*)malloc(sizeof(struct ListNode));
    newNode->val = data;
    
    if(head->val == 0){
        head->val = newNode->val;
    }else{
        struct ListNode* curr = head;
        while(curr->next != NULL){
            curr = curr->next;
        }
        curr->next = newNode;
    }
}
 
struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2){
    struct ListNode* mergedList = (struct ListNode*)malloc(sizeof(struct ListNode));
    
    struct ListNode* i = list1;
    struct ListNode* j = list2;
    
    while(i != NULL && j != NULL){
        if(i->val < j->val){
            push(i->val, mergedList);
            i = i->next;
        }else{
            push(j->val, mergedList);
            j = j->next;
        }
    }
    
    while(i != NULL){
        push(i->val, mergedList);
        i = i->next;
    }
    
    while(j != NULL){
        push(j->val, mergedList);
        j = j->next;
    }

    return mergedList;
}

void displayList(struct ListNode *curr){
    while(curr != NULL){
        printf("%d", curr->val);
        curr = curr->next;
    }
}

int main()
{
    struct ListNode* List1 = (struct ListNode*)malloc(sizeof(struct ListNode));
    struct ListNode* List2 = (struct ListNode*)malloc(sizeof(struct ListNode));
    
    struct ListNode* newNode0 = (struct ListNode*)malloc(sizeof(struct ListNode));
    newNode0->val = 1;
    List1 = newNode0;
    
    struct ListNode* newNode1 = (struct ListNode*)malloc(sizeof(struct ListNode));
    newNode1->val = 2;
    List1->next = newNode1;
    
    struct ListNode* newNode2 = (struct ListNode*)malloc(sizeof(struct ListNode));
    newNode2->val = 4;
    List1->next->next = newNode2;
    
    struct ListNode* newNode00 = (struct ListNode*)malloc(sizeof(struct ListNode));
    newNode00->val = 1;
    List2 = newNode00;
    
    struct ListNode* newNode10 = (struct ListNode*)malloc(sizeof(struct ListNode));
    newNode10->val = 3;
    List2->next = newNode10;
    
    struct ListNode* newNode20 = (struct ListNode*)malloc(sizeof(struct ListNode));
    newNode20->val = 4;
    List2->next->next = newNode20;
    
    struct ListNode *mergeL = mergeTwoLists(List1, List2);
    
    displayList(mergeL);

    return 0;
}
b4qexyjb

b4qexyjb1#

您需要将NULL分配给在push中创建的新struct ListNode中的next。此外,您需要struct ListNode **才能更改head。它可以看起来像这样:

void push(int data, struct ListNode **head) {
    // find the pointer pointing at NULL
    while (*head) head = &(*head)->next;
    // allocate memory for the new node:
    *head = malloc(sizeof **head);
    // assign values to both val and next:
    **head = (struct ListNode){data, NULL};
}

然后,您需要相应地调整对push的调用。
Demo
如果push应该能够创建一个排序列表,你可以这样调整它:

void push(int data, struct ListNode **head) {
    // find the insertion point:
    for (;*head; head = &(*head)->next) {
        if ((*head)->val > data) break;
    }
    struct ListNode* next = *head;
    // allocate memory for the new node:
    *head = malloc(sizeof **head);
    // assign values to both val and next:
    **head = (struct ListNode){data, next};
}

Demo

相关问题