Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

So I'm working out the problem number 2 of leetcode (basically you got 2 numbers reversed in a list and need to sum them and return the answer in an inverted list too). 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 run it on my machine and it works fine, giving the expected results. My code:

/*Definition of the list:
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;
            if(!answer->next)
                answer->next = malloc(sizeof(struct ListNode));
            answer = answer->next;
            answer->next = NULL;
    return auxVar;

Any thoughts on what might be causing this problem? Thanks for your time.

Edit: here is a verifiable example with the numbers that result the crash:

#include <stdio.h>
#include <stdlib.h>
struct ListNode {
    int val;
    struct ListNode *next;
/*typedef struct InvertedList {
    int val;
    struct InvertedList *next;
    struct InvertedList *previous;
}Lista;*/
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;
            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 = 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;
                You've not provided an MCVE (minimal reproducible example).  You've not provided suitable sample data.  You've not shown how the lists are constructed.  You've not provided us with a printing method.  You need to provide an MCVE and sample data — of the type that crashes your code.  (Crashes on someone else's machine may be OK if you know the data that crashes.)  Have you used Valgrind to analyze what's going wrong?
– Jonathan Leffler
                Jul 23, 2017 at 14:58
                @JonathanLeffler sorry, that wasn't very thoughtful of my part. I've edited the question with your requests.
– Raphael Sauer
                Jul 23, 2017 at 15:46
                I'd recommend using a function to create new nodes. There are definitely places where you're not fully initializing the node, specifically the next pointer, and that is what causes your code to crash for me.
– Retired Ninja
                Jul 23, 2017 at 20:01
                at adicionaLista The last next isn't NULL initialized. There is the same problem throughout.
– BLUEPIXY
                Jul 23, 2017 at 22:11

The main problem is that next in the last element isn't set to NULL.
Secondary problems are...

  • It is expected not to create an empty list, that is, a situation where useless nodes are created.
  • There are many duplications in the code. That makes it difficult to change the code.
  • printf("\n"); should not be included in additional functions of data. It should be used in output functions.
  • fixed and reduced 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 anchor  = { .next = NULL }, *curr = &anchor;
        int carry = 0;
        while(l1 != NULL || l2 != NULL || carry) {
            int val1 = 0, val2 = 0;
            if(l1) {
                val1 = l1->val;
                l1 = l1->next;
            if(l2) {
                val2 = l2->val;
                l2 = l2->next;
            int answer = val1 + val2 + carry;
            carry = answer > 9;//val1 and val2 are one digit.
            curr = curr->next = malloc(sizeof(struct ListNode));
            curr->val = answer % 10;
            curr->next = NULL;
        return anchor.next;
    struct ListNode *makeListFromChars(const char *array, int size) {
        struct ListNode anchor  = { .next = NULL }, *curr = &anchor;
        for(int i = 0; i < size; i++) {
            curr = curr->next = malloc(sizeof(struct ListNode));//Creating lists and adding data is a separate function.
            curr->val = array[i];
            curr->next = NULL;
        return anchor.next;
    void printList(struct ListNode *p) {
        for(; p; p = p->next)
            printf("%d ", p->val);
        printf("\n");
    int main(void){
        char lista1[9] = {4,5,2,2,9,3,8,9,2};
        char lista2[9] = {0,7,6,1,6,5,0,6,7};
        struct ListNode *l1 = makeListFromChars(lista1, 9);
        struct ListNode *l2 = makeListFromChars(lista2, 9);
        printList(l1);
        printList(l2);
        struct ListNode *answer = addTwoNumbers(l1, l2);
        printList(answer);
        //freeList(l1);freeList(l2);freeList(answer);
        return 0;
                    I faced the same problem but with your help found the issue and got ACCEPTED. Here is my working code.
    – Eziz Durdyyev
                    Feb 2, 2018 at 15:34
            

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.