内存泄漏删除节点C++; 我试图删除C++中链表中的一个节点,但它总是说删除事务后,不丢失nulpPTR。valgrind还说我有一个内存泄漏,我无法找到它 bool BankData::void_transaction(const size_t& customer_id, const size_t& timestamp) { bool var8 = false; if (transaction_exists(customer_id, timestamp) == true) { int blah3 = customerVector.size(); for (int i = 0; i < blah3; i++) { if (customerVector.at(i)._customer_id == customer_id) { if (customerVector.at(i)._pending_txs != nullptr) { CSE250::dTransactionNode *temp = customerVector.at(i)._pending_txs; while (temp->_tx._timestamp != timestamp) { temp = temp->_next; if ((temp->_next == nullptr) && (temp->_tx._timestamp != timestamp)) { var8 = false; } } if (temp->_tx._timestamp == timestamp) { if ((temp->_prev == nullptr) && (temp->_next == nullptr)) //head and only node { delete customerVector.at(i)._pending_txs; customerVector.at(i)._pending_txs = nullptr; //after i delete the head and only node i reset it to a nullptr var8 = true; } else if ((temp->_prev == nullptr) && (temp->_next != nullptr)) //head { temp = customerVector.at(i)._pending_txs->_next; delete customerVector.at(i)._pending_txs; customerVector.at(i)._pending_txs = temp; customerVector.at(i)._pending_txs->_prev = nullptr; var8 = true; } else if ((temp->_prev != nullptr) && (temp->_next == nullptr)) //tail { temp = customerVector.at(i)._pending_txs->_prev; delete customerVector.at(i)._pending_txs; customerVector.at(i)._pending_txs = temp; customerVector.at(i)._pending_txs->_next = nullptr; var8 = true; } else //middle node { temp = customerVector.at(i)._pending_txs->_next; customerVector.at(i)._pending_txs->_next->_prev = customerVector.at(i)._pending_txs->_prev; delete customerVector.at(i)._pending_txs; customerVector.at(i)._pending_txs = temp; //temp->_prev->_next = temp->_next; //temp->_next->_prev = temp->_prev; //temp = nullptr; //delete temp; var8 = true; } } } } } } return var8;

内存泄漏删除节点C++; 我试图删除C++中链表中的一个节点,但它总是说删除事务后,不丢失nulpPTR。valgrind还说我有一个内存泄漏,我无法找到它 bool BankData::void_transaction(const size_t& customer_id, const size_t& timestamp) { bool var8 = false; if (transaction_exists(customer_id, timestamp) == true) { int blah3 = customerVector.size(); for (int i = 0; i < blah3; i++) { if (customerVector.at(i)._customer_id == customer_id) { if (customerVector.at(i)._pending_txs != nullptr) { CSE250::dTransactionNode *temp = customerVector.at(i)._pending_txs; while (temp->_tx._timestamp != timestamp) { temp = temp->_next; if ((temp->_next == nullptr) && (temp->_tx._timestamp != timestamp)) { var8 = false; } } if (temp->_tx._timestamp == timestamp) { if ((temp->_prev == nullptr) && (temp->_next == nullptr)) //head and only node { delete customerVector.at(i)._pending_txs; customerVector.at(i)._pending_txs = nullptr; //after i delete the head and only node i reset it to a nullptr var8 = true; } else if ((temp->_prev == nullptr) && (temp->_next != nullptr)) //head { temp = customerVector.at(i)._pending_txs->_next; delete customerVector.at(i)._pending_txs; customerVector.at(i)._pending_txs = temp; customerVector.at(i)._pending_txs->_prev = nullptr; var8 = true; } else if ((temp->_prev != nullptr) && (temp->_next == nullptr)) //tail { temp = customerVector.at(i)._pending_txs->_prev; delete customerVector.at(i)._pending_txs; customerVector.at(i)._pending_txs = temp; customerVector.at(i)._pending_txs->_next = nullptr; var8 = true; } else //middle node { temp = customerVector.at(i)._pending_txs->_next; customerVector.at(i)._pending_txs->_next->_prev = customerVector.at(i)._pending_txs->_prev; delete customerVector.at(i)._pending_txs; customerVector.at(i)._pending_txs = temp; //temp->_prev->_next = temp->_next; //temp->_next->_prev = temp->_prev; //temp = nullptr; //delete temp; var8 = true; } } } } } } return var8;,c++,data-structures,memory-leaks,linked-list,nodes,C++,Data Structures,Memory Leaks,Linked List,Nodes,我也不明白为什么当我试图删除它时,它只会删除时间戳,而不会删除时间戳和金额。因此,当我运行我的transaction exists函数时,它仍然表示部分事务存在 当您删除某个内容时,指针不会自动设置为nullptr。这是程序员的责任。阅读更多 当您将指针设置为null时,它不会删除内存。如果这样做,在调用delete之前,如果不使用另一个指向内存的指针删除内存,则将创建内存泄漏 可以使用智能指针,这会为您带来好处。delete不会将任何内容设置为空指针。请使用适当的智能指针。您会说temp=c

我也不明白为什么当我试图删除它时,它只会删除时间戳,而不会删除时间戳和金额。因此,当我运行我的transaction exists函数时,它仍然表示部分事务存在


当您
删除某个内容时,指针不会自动设置为
nullptr
。这是程序员的责任。阅读更多

当您将指针设置为null时,它不会删除内存。如果这样做,在调用
delete
之前,如果不使用另一个指向内存的指针删除内存,则将创建内存泄漏


可以使用智能指针,这会为您带来好处。

delete不会将任何内容设置为空指针。请使用适当的智能指针。您会说temp=customerVector.at(i)。\u pending\u txs;customerVector.at(i)。_pending_txs=nullptr;删除温度:?不@MatthewKirshy,这会导致内存泄漏,请参阅我的答案。好的,第二行不处理这个问题吗?删除customerVector.at(i)。_pending_txs;customerVector.at(i)。_pending_txs=nullptr//删除头部和唯一节点后,我将其重置为nullptr var8=true;是@MatthewKirshy。我就是这么做的,而且我仍然有内存泄漏,这意味着@MatthewKirshy,您没有正确删除节点。请记住,您应该删除与使用
new
创建的节点数量相同的节点。
namespace CSE250 {
struct dTransactionNode;}

struct CSE250::dTransactionNode {
Transaction _tx;

dTransactionNode* _prev;

dTransactionNode* _next;

dTransactionNode(size_t time, double amount) : _tx(time, amount), _prev(nullptr), _next(nullptr) { }};