简体   繁体   English

在运行代码时,我遇到了一个地址清理器错误

[英]While running my code, I encountered an Address Sanitizer error

This is a question from LeetCode where I basically have to add two numbers using linked lists. 这是来自LeetCode的问题,我基本上必须使用链接列表添加两个数字。 I was fairly confident of what I was doing and my code was accepted on their default test case. 我对自己所做的事情非常有信心,并且我的代码被接受了他们的默认测试用例。 However, when I hit submit, it doesn't work on any of their test cases. 但是,当我点击“提交”时,它不适用于他们的任何测试用例。

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */


struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2)
{
 struct ListNode *temp1= l1,*temp2=l2,*temp3=(struct ListNode*)malloc(sizeof(struct ListNode)),*temp4=temp3,*prev;
    temp3->val=0;
 long long int num1=0,num2=0;
 while (temp1!=NULL)
 {
     num1=num1*(long long int )10 + (long long int )temp1->val;
    temp1 = temp1->next;
 }
 while (temp2!=NULL)
 {
     num2=num2*10 + temp2->val;
     temp2 = temp2->next;
 }
 long long int  num3 = num1+num2;
 do
 {

     temp3->val = (long long int )num3%10;
     temp3->next =  (struct ListNode*)malloc(sizeof(struct ListNode));
     prev=temp3;
     temp3 = temp3->next;
     num3/=(long long int )10;
 } while(num3!=0);
  prev->next=NULL;
  return temp4;
}


I applied a brute force approach and just added the two numbers. 我采用了蛮力的方法,只是将两个数字相加。 It gave me the correct value. 它给了我正确的价值。 Then I create a new linked list where I save everu digit and to compensate for the extra element at the end, I save the previous node in each case. 然后,创建一个新的链表,在其中保存Everu位数,并为补偿末尾的多余元素,在每种情况下均保存前一个节点。 In the end, I delete the connection of the last element to the extra one. 最后,我删除了最后一个元素与多余元素的连接。 I run my code and I get the correct output. 我运行我的代码,并获得正确的输出。 I expected [7,0,8] and got [7,0,8] 我期望[7,0,8]并得到[7,0,8]

Here is the traceback: 这是回溯:

AddressSanitizer: SEGV on unknown address 0x0000000c7616 (pc 0x0000004019db bp 0x7ffff1366900 sp 0x7ffff13668e0 T0)

There really isn't much anywhere regarding the error. 关于错误,确实没有什么地方。 Here was the most similar one I could find but I have been using malloc to allocate the memory anyway and using free(prev->next) messes it all up. 这是我能找到的最相似的东西,但是无论如何我一直在使用malloc分配内存,并且使用free(prev-> next)弄乱了所有东西。 link 链接

I also wish to clarify that I am not looking for the ideal answer as I don't want to cheat, just find out what I am doing wrong. 我还想澄清一下,我并不是在寻找理想的答案,因为我不想作弊,只是找出我做错了什么。

Edit 编辑

Adding a do-while looping made me clear 14 additional test cases... out of 1563. A new error has come up 添加do-while循环使我从1563中清除了另外14个测试用例。出现了一个新错误。

Line 17: Char 15: runtime error: signed integer overflow: 399999999 * 10 cannot be represented in type 'int' (solution.c)

Line 17 refers to the num1=num1*10 + temp1->val line; 第17行是num1 = num1 * 10 + temp1-> val行; I decided to replace every int with long long int but it doesn't make a difference except for clearing five additional test cases. 我决定将每个int替换为long long int,但是除了清除五个额外的测试用例外,其他没有什么区别。 ( I casted every value to long long int including the constants) (我将每个值都强制转换为long int,包括常量)

I have made the few changes to not dereference prev pointer when num3 is 0 . num30时,我做了一些更改以不取消引用prev指针。

struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2){
 struct ListNode *temp1= l1,*temp2=l2,*temp3=(struct ListNode*)malloc(sizeof(struct ListNode)),*temp4=temp3;
    temp3->val=0;
    temp3->next=NULL;

 int num1=0,num2=0;
 while (temp1!=NULL)
 {
     num1=num1*10 + temp1->val;
    temp1 = temp1->next;
 }

 while (temp2!=NULL)
 {
     num2=num2*10 + temp2->val;
     temp2 = temp2->next;
 }

 int num3 = num1+num2;
 while(num3!=0)
 {
     temp3->val = num3%10;
     temp3->next =  (struct ListNode*)malloc(sizeof(struct ListNode));
     temp3->next->next = NULL;
     temp3 = temp3->next;
     num3/=10;
 }

  return temp4;
}

Basically I have removed the prev variable instead directly assigning the NULL . 基本上,我删除了prev变量,而不是直接分配NULL Also you have memory leak of size struct ListNode when sum of the numbers is 0 . 当数字的总和为0时, struct ListNode发生大小为struct ListNode内存泄漏。 That I let you to figure out and handle. 我让你弄清楚并处理。

But your solution will not work if there are more digits represented in the lists which will eventually overflow the integers int num3 = num1+num2; 但是,如果列表中有更多数字表示,最终将导致整数int num3 = num1+num2;溢出,则您的解决方案将不起作用int num3 = num1+num2; .

Finally the task to is to add the two list in place not to extract the digits out of them and form the integers. 最后,要做的任务是在适当的位置添加两个列表,而不是从中提取数字并形成整数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM