Member access within misaligned address


  • 0
    S

    So I'm working out the problem number 2 of leetcode, however I keep getting this annoying error: "Runtime error: member access within misaligned address 0x000000000031 for type 'struct ListNode', which requires 8 byte alignment"
    I did a bit of researching and it appears that this error is about pointers that are not defined after being declared (or something like it), but I couldn't found any of it in my code. And if I use the same variables in a custom test case I get the right answer. Code here:

    struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
        struct ListNode* answer = malloc(sizeof(struct ListNode));
        answer->next = NULL;
        answer->val = 0;
        int auxInt = 0;
        struct ListNode* auxVar = answer;
    
        while(l1 != NULL || l2 != NULL) {
            if(!l1) {
                auxInt = answer->val;
                answer->val = (auxInt + l2->val) % 10;
                if(((l2->val + auxInt) / 10) != 0) {
                    answer->next = malloc(sizeof(struct ListNode));
                    answer->next->val = (l2->val + auxInt) /10;
                }
                l2 = l2->next;
            }
            else if(!l2) {
                auxInt = answer->val;
                answer->val = (auxInt + l1->val) % 10;
                if(((l1->val + auxInt) / 10) != 0) {
                    answer->next = malloc(sizeof(struct ListNode));
                    answer->next->val = (l1->val + auxInt) /10;
                }
                l1 = l1->next;
            } else {
                auxInt = answer->val;
                answer->val = (l1->val + l2->val + auxInt) % 10;
                if(((l1->val + l2->val + auxInt) / 10) != 0) {
                    answer->next = malloc(sizeof(struct ListNode));
                    answer->next->val = (l1->val + l2->val + auxInt) /10;
                }
                l1 = l1->next;
                l2 = l2->next;
            }
            if(l1 == NULL && l2 == NULL)
                break;
            else
            {
                if(!answer->next)
                    answer->next = malloc(sizeof(struct ListNode));
                answer = answer->next;
                answer->next = NULL;
            }
        }
        return auxVar;
    }
    

    Any thoughts of why this is happening? Thanks for your time.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.