问题遇到的现象和发生背景
能在VS和力扣的playground运行,但是提交代码的时候提示
==42==ERROR: AddressSanitizer: alloc-dealloc-mismatch (malloc vs operator delete) on 0x602000000170
#3 0x7fae665680b2 (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)
0x602000000170 is located 0 bytes inside of 16-byte region [0x602000000170,0x602000000180)
allocated by thread T0 here:
#3 0x7fae665680b2 (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)
==42==HINT: if you don't care about these errors you may set ASAN_OPTIONS=alloc_dealloc_mismatch=0
==42==ABORTING
问题相关代码,请勿粘贴截图
/**
- Definition for singly-linked list.
- struct ListNode {
int val;
ListNode *next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode *next) : val(x), next(next) {}
- };
- /
class Solution
{
public:
ListNode* reverseBetween(ListNode* head, int left, int right)
{
ListNode* p;//用于探测链表长度;
ListNode* q;//用于将链表值赋给数组;
ListNode* r;//用于指示原链表片段右端;
ListNode* l;//用于指示原链表片段左端
ListNode* l1;//用于指示新链表片段左端;
ListNode* r1;//用于扩建新链表,用于指示新链表片段右端;
p= head;
q = head;
l = head;
r = head;
int n = 1;
while(p->next != nullptr)
{
n++;
p = p->next;
}
int* array;
array = (int*)malloc(n*sizeof(int));
for (int i = 0; i<=n; i++)
{
if(q->next != nullptr) {
*(array+i) = q->val;
q = q->next;
}
else { * (array + i) = q->val; break; }
}
r1 = (ListNode*)malloc(sizeof(ListNode));
r1->val = *(array+n-1);
r1->next = NULL;
for (int i = n; i > 1; i--)
{
ListNode* q;
q = (ListNode*)malloc(sizeof(ListNode));
q->val =*(array+n-i);
q->next = r1->next;
r1->next = q;
}
l1 = r1;
int right1 = n-left+1;
int left1 = n-right+1;
for (left; left > 2; left--)//原
{
l = l->next;
}
for (right; right > 0; right--)//原
{
r = r->next;
}
for(left1; left1 >1; left1--)//新
{
l1 = l1->next;
}
for (right1; right1 > 1; right1--)//新
{
r1 = r1->next;
}
l->next = l1;
r1->next = r;
return head;
}
};
有没有人能把那个错误忽略了或者进一步改进