在循环(C++)中用新指针填充映射
Populating map with new pointers in a loop (C++)
所以我正在处理leetcode中的带有随机指针的复制列表问题,遇到了一些问题。
for (; iterateOG->next != nullptr;
iterateOG = iterateOG->next) {
Node* tmp = new Node;
hashTable.insert(std::pair<Node*,Node*>(iterateOG,tmp));
}
因此,我想为原始列表中的每个现有节点创建一个新的Node对象。运行测试用例后,我收到一条错误消息,比如在下面包含HINT
'hashTable' <== Memory access at offset 912 overflows this variable
HINT: this may be a false positive if your program uses some custom stack unwind mechanism or swapcontext
(longjmp and C++ exceptions *are* supported)
有人知道是什么导致了这个问题吗?我知道原因可能是因为我创建了一个新对象,但没有在范围内对其进行任何处理,但我看不到解决这个问题的方法。如有任何帮助,我们将不胜感激。非常感谢。
完整代码和下方的错误消息
/*
// Definition for a Node.
class Node {
public:
int val;
Node* next;
Node* random;
Node() {}
Node(int _val, Node* _next, Node* _random) {
val = _val;
next = _next;
random = _random;
}
};
*/
class Solution {
public:
Node* copyRandomList(Node* head) {
if (head == nullptr)
return nullptr;
Node* iterateOG = head;
std::map<Node*, Node*> hashTable;
for (; iterateOG->next != nullptr;
iterateOG = iterateOG->next) {
Node* tmp = new Node;
hashTable.insert(std::pair<Node*,Node*>(iterateOG,tmp));
}
std::cout << endl;
iterateOG = head;
for (; iterateOG->next != nullptr;
iterateOG = iterateOG->next) {
hashTable.find(iterateOG)->second->val = iterateOG->val;
hashTable.find(iterateOG)->second->next = hashTable.find(iterateOG->next)->second;
hashTable.find(iterateOG)->second->random = hashTable.find(iterateOG->random)->second;
std::cout << hashTable.find(iterateOG)->second->val << ',';
}
std::cout << endl;
return hashTable.find(head)->second;
}
};
错误消息
=================================================================
==29==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7ffc4dbbbb70 at pc 0x00000041d7eb bp 0x7ffc4dbbb7b0 sp 0x7ffc4dbbb7a8
READ of size 8 at 0x7ffc4dbbbb70 thread T0
#2 0x7fa042ca92e0 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x202e0)
Address 0x7ffc4dbbbb70 is located in stack of thread T0 at offset 912 in frame
This frame has 14 object(s):
[32, 33) '__c'
[96, 97) '__c'
[160, 168) 'iterateOG'
[224, 232) 'tmp'
[288, 296) '<unknown>'
[352, 360) '<unknown>'
[416, 424) '<unknown>'
[480, 488) '<unknown>'
[544, 552) '<unknown>'
[608, 616) '<unknown>'
[672, 680) '<unknown>'
[736, 744) 'head'
[800, 816) '<unknown>'
[864, 912) 'hashTable' <== Memory access at offset 912 overflows this variable
HINT: this may be a false positive if your program uses some custom stack unwind mechanism or swapcontext
(longjmp and C++ exceptions *are* supported)
Shadow bytes around the buggy address:
0x100009b6f710: 00 f2 f2 f2 f2 f2 f2 f2 f8 f2 f2 f2 f2 f2 f2 f2
0x100009b6f720: f8 f2 f2 f2 f2 f2 f2 f2 00 f2 f2 f2 f2 f2 f2 f2
0x100009b6f730: 00 f2 f2 f2 f2 f2 f2 f2 00 f2 f2 f2 f2 f2 f2 f2
0x100009b6f740: 00 f2 f2 f2 f2 f2 f2 f2 00 f2 f2 f2 f2 f2 f2 f2
0x100009b6f750: 00 f2 f2 f2 f2 f2 f2 f2 00 f2 f2 f2 f2 f2 f2 f2
=>0x100009b6f760: f8 f8 f2 f2 f2 f2 f2 f2 00 00 00 00 00 00[f2]f2
0x100009b6f770: f3 f3 f3 f3 00 00 00 00 00 00 00 00 00 00 00 00
0x100009b6f780: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x100009b6f790: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x100009b6f7a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x100009b6f7b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
Shadow byte legend (one shadow byte represents 8 application bytes):
Addressable: 00
Partially addressable: 01 02 03 04 05 06 07
Heap left redzone: fa
Freed heap region: fd
Stack left redzone: f1
Stack mid redzone: f2
Stack right redzone: f3
Stack after return: f5
Stack use after scope: f8
Global redzone: f9
Global init order: f6
Poisoned by user: f7
Container overflow: fc
Array cookie: ac
Intra object redzone: bb
ASan internal: fe
Left alloca redzone: ca
Right alloca redzone: cb
==29==ABORTING
错误在这一行:
hashTable.find(iterateOG)->second->next = hashTable.find(iterateOG->next)->second;
对于最后一个节点,iterateOG->next
具有不在hashTable
中的值。
相关文章:
- 使用结构成员指针在C++中填充结构
- 如何使用数据成员填充派生类的对象到基类的指针数组中
- 在循环(C++)中用新指针填充映射
- 用基于范围的for循环填充指针向量
- 内存集不会填充整个指针数组 c++
- 如何填充包含指针数组的结构数组
- 使用指向数组的指针填充动态数组
- 操作员的地址可以启动指针,但填充值是垃圾
- 正确填充炭指针
- C++传递指向未知大小的数组的指针,函数大小并填充它
- 在使用结构体和用函数填充其变量(使用指针)时遇到问题
- 使用来自函数的指针填充结构数组
- 如何在文本文件中逐行读取并填充指向对象数组的指针
- 如何从文本文件中填充指针数组
- 通过指针填充自定义的类 - 与构造函数的麻烦
- 如何使用类实例成员函数指针填充函数指针成员
- 将指针填充到向量的向量会产生奇怪的结果
- 使用指针填充向量时出现分段错误
- 通过返回的指针填充私有字段
- 用指针填充列表 - c++