I am coding a solution to the LeetCode problem "Odd Even Linked List" which can be read here.
My code is failing the test case with the error
================================================================
==31==ERROR: AddressSanitizer: heap-use-after-free on address 0x6020000000d8 at pc 0x0000003d7f1d bp 0x7fff37cf9640 sp 0x7fff37cf9638
READ of size 8 at 0x6020000000d8 thread T0
However, when I run the code in Visual Studio to diagnose the error, everything works. The solution for LeetCode is here:
class Solution {
public:
ListNode* oddEvenList(ListNode* head) {
if (!head){
return head;
}
ListNode* odd_head = head;
ListNode* even_head = odd_head->next;
if (!even_head){
return head;
}
ListNode* last_odd = odd_head;
ListNode* last_even = even_head;
ListNode* next_node = even_head->next;
bool flag = true;
while(true){
if (!next_node){
break;
}
if (flag){
last_odd -> next = next_node;
last_odd = next_node;
} else {
last_even -> next = next_node;
last_even = next_node;
}
flag = !flag;
next_node = (next_node->next);
}
last_odd->next = even_head;
return odd_head;
}
};
The code I am using to test the above is here:
#include "oddevenlinkedlist.h"
#include <iostream>
int main() {
ListNode* l1 = new ListNode(1);
ListNode* l2 = new ListNode(2);
l1->next = l2;
ListNode* l3 = new ListNode(3);
l2->next = l3;
ListNode* l4 = new ListNode(4);
l3->next = l4;
ListNode* l5 = new ListNode(5);
l4->next = l5;
Solution solution{};
ListNode* result = solution.oddEvenList(l1);
ListNode* next_node = result;
for (int i = 0; i < 5; ++i) {
std::cout << next_node->val << " ";
next_node = next_node->next;
}
delete l1;
delete l2;
delete l3;
delete l4;
delete l5;
return 0;
}
If you wish to test this, you will want the definition of a ListNode which is here:
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) {}
};
Because the code works on my compiler, I am having trouble diagnosing the error. While of course I hope someone can identify the error, my question is this: why is MSVS not catching the "heap-use-after-free" error?
