runtime error: member access within misaligned address(leetcode第二题)

问题描述 投票:0回答:1

我一直在尝试使用“C”作为编程语言来解决 leetcode 第二个问题“添加两个数字”。问题如下。

给定两个非空链表,代表两个非负整数。数字以相反的顺序存储,并且它们的每个节点都包含一个数字。将两个数字相加并将总和作为链表返回。

您可以假设这两个数字不包含任何前导零,除了数字

0
本身。

如果你不想去 leetcode 检查问题,下面的例子将给出关于问题陈述的想法。`

Input: l1 = [2,4,3], l2 = [5,6,4]
Output: [7,0,8]
Explanation: 342 + 465 = 807.

下面是leet代码给出的struct定义

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

下面是给我们的函数,用于返回具有上述输出的链表。

下面是我的代码

struct ListNode *addTwoNumbers(struct ListNode *l1, struct ListNode *l2) {
    int number1 = 0;
    int reverse1 = 0;
    int rotate = 1;
    int addnumbers;
    struct ListNode *final = (struct ListNode *)malloc(sizeof(struct ListNode));
    struct ListNode *current = final; 
    int split;

    while (l1 != NULL)
    {
        number1 = (l1->val * rotate) + number1; 
        l1 = l1->next;
        rotate = rotate * 10;
    }

    int number2 = 0;
    reverse1 = 0;
    rotate = 1;

    while (l2 != NULL)
    {
        number2 = (l2->val * rotate) + number2; 
        l2 = l2->next;
        rotate = rotate * 10;
    }
    addnumbers = number1 + number2;  

    while (addnumbers != 0)
    {
        split = addnumbers % 10;
        addnumbers = addnumbers / 10;
        if (final != NULL)
        {
            final->val = split; 
            printf("%d \n", final->val);
        }               
        if (addnumbers == 0)
        {
            final->next = NULL;
            break;
        }
        else
        {  
            final->next = malloc(sizeof(struct ListNode));  
        }  
        final = final->next;    
    }
    while (current != NULL)
    {
        printf(" The values are %d \n", current->val);
        current = current->next;
    }
    return 0;
}

在上面的代码中,我还没有返回结构,但我可以确认

8
0
7
在代码执行时正在打印,但第四个地址值也在运行时打印出来以下错误

Line 58: Char 9: runtime error: member access within misaligned address 0xbebebebebebebebe for type 'struct ListNode', which requires 8 byte alignment [solution.c]
0xbebebebebebebebe: note: pointer points here
<memory cannot be printed>

我相信最后一个

while
循环应该只旋转3次但是为什么第四个地址被打印出来了。能否请您指出原因。

c linked-list runtime-error memory-address
1个回答
0
投票

你的代码有两个问题:

  • 你受类型范围的限制
    int
    ,而列表表示可以处理更大的数字。
  • 你修改了
    final
    current
    所以你可能会返回错误的指针,导致程序失败。
  • 如果两个数字都是
    0
    addnumbers
    0
    并且
    while
    循环根本不迭代,使
    final
    指向的节点未初始化,可能导致测试程序中出现未定义的行为。这可以解释运行时错误。

这里是一个可以处理更长列表的修改版本:

struct ListNode *addTwoNumbers(struct ListNode *l1, struct ListNode *l2) {
    struct ListNode *final = malloc(sizeof(*final));
    struct ListNode *current = final;
    int carry = 0;

    for (;;) {
        if (l1) {
            carry += l1->val;
            l1 = l1->next;
        }
        if (l2) {
            carry += l2->val;
            l2 = l2->next;
        }
        current->val = carry % 10;
        carry /= 10;
        if (carry || l1 || l2) {
            current->next = malloc(sizeof(*current));
            current = current->next;
        } else {
            current->next = NULL;
            return final;
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.