"Member access Within misaligned address" with a chained list

Asked

Viewed 72 times

0

I’m solving a problem with leetcode (add two numbers that are inverted, each digit in a node of a chained list and return a chained list as a response). When giving Submit in the reply I get the following error: "Runtime error: Member access Within misaligned address 0x000000000031 for type 'struct Listnode', which requires 8 byte Alignment". Here is the code:

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

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

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;
        }
    }
    return auxVar;
}

void adicionaLista(struct ListNode* ptrLista, char *array, int size) {
    struct ListNode *auxNode = ptrLista;
    for(int i = 0; i < size; i++) {
        ptrLista->val = array[i];
        if(i + 1 != size) {
            ptrLista->next = malloc(sizeof(struct ListNode));
            ptrLista = ptrLista->next;
        }
    }
    ptrLista->next = NULL;
    ptrLista = auxNode;
    printf("\n");
}
void printAnswer(struct ListNode *ptrAnswer) {
    for(; ptrAnswer != NULL; ptrAnswer = ptrAnswer->next)
        printf("%d ", ptrAnswer->val);
}

int main()
{
    struct ListNode *l1 = malloc(sizeof(struct ListNode));
    struct ListNode *l2 = malloc(sizeof(struct ListNode));
    char lista[9] = {4,5,2,2,9,3,8,9,2};
    char lista2[9] = {0,7,6,1,6,5,0,6,7};
    adicionaLista(l1, lista, 9);
    adicionaLista(l2, lista2, 9);
    struct ListNode *answer = addTwoNumbers(l1, l2);
    printAnswer(answer);
    return 0;
}

Any idea what might be causing such an error?

Note: on my machine the code runs smoothly.

  • 1

    This is memory alignment error. It means that the processor only accepts read/write aligned to memory depending on the types. Which line gives the error?

  • 1

    It may not be useful to solve your question but it can contribute to your knowledge of Structs knowing about Padding and how to avoid it. https://answall.com/questions/50614/como-funciona-o-padding-do-c

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.