-1

I'm working on LeetCode problem 2. This is my code so far

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2)
{
    int firstNumber = 0;
    int secondNumber = 0;
    struct ListNode* finalNode = (struct ListNode *)malloc(sizeof(struct ListNode));
    struct ListNode* tmp = finalNode;
    int x;
    for(x = 0; l1 != NULL || l2 != NULL; x++)
    {
        if(l1 != NULL)
        {
        firstNumber += l1->val * pow(10, x);
        l1 = l1->next;
        }
        if(l2 != NULL)
        {
        secondNumber += l2->val * pow(10, x);
        l2 = l2->next;
        }
    }
    char* finalValString = (char*)calloc(x + 1, sizeof(char));
    sprintf(finalValString, "%d", firstNumber + secondNumber);
    for(int a = x; a >= 0; a--)
    {
        tmp->val = finalValString[a] - 48;
        tmp = tmp->next;
    }
    free(tmp);

    return finalNode;
}

But when I run this code I always get the error:

Line 32: Char 21: runtime error: member access within misaligned address 0xbebebebebebebebe for type 'struct ListNode', which requires 8 byte alignment [solution.c] 0xbebebebebebebebe: note: pointer points here

I'm not sure how to fix this.

Rezno
  • 1
  • 3
  • Questions seeking debugging help should generally provide a [mre] of the problem, which includes a function `main` and all `#include` directives, as well as the exact input required to reproduce the problem. This allows other people to easily test your program, by simply using copy&paste. If the function `main` and the input is being provided by the third-party site that you are using, please try to reproduce the problem by providing your own function `main` as well as your own input. It would be ideal if you could hard-code the input into the function `main`. – Andreas Wenzel Apr 15 '23 at 05:32
  • _"But when I run this code I always get the error..."_ - Run the program on _your_ computer and use a debugger. The verdict you get from these competitive programming sites is usually useless. – Ted Lyngmo Apr 15 '23 at 05:43
  • Have you tried running your code line-by-line in a debugger while monitoring the control flow and the values of all variables, in order to determine in which line your program stops behaving as intended? If you did not try this, then you may want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) You may also want to read this: [How to debug small programs?](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Andreas Wenzel Apr 15 '23 at 05:44

1 Answers1

1

Here's the issue:

You initalize finalNode and tmp as follows:

struct ListNode* finalNode = (struct ListNode *)malloc(sizeof(struct ListNode));
struct ListNode* tmp = finalNode;

Hence, tmp and finalNode point to the same thing. Also, finalNode->next (and tmp->next) is uninitialized and points to an undefined address.

And then you do this:

for(int a = x; a >= 0; a--)
{
    tmp->val = finalValString[a] - 48;
    tmp = tmp->next;
}

So wait.... What is tmp->next on the first iteration of the loop? It's definitely not pointing to properly allocated memory. But even if it was NULL, then would guarantee that the next iteration of the loop will crash on tmp->val = ...

That's not your only bug. Even if you fix the bug above, your solution will fail again once a linked list for l1 or l2 is passed in that exceeds 10 digits because int can't be bigger than 2,147,483,648 (assuming 32-bit int).

The proper solution is to allocate a node as you go and keep track of the "carry" value (0 or 1) as you iterate through l1 and l2. Don't keep a running int firstNumber or int secondNumber.

Also, there's no need to convert using -48. I'm guessing you think you need to do an ascii conversion. You don't. You're expected to return an entire link list of integers from 0-9 in each node.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
selbie
  • 100,020
  • 15
  • 103
  • 173