gpt4 book ai didi

c++ - 为 LinkedList 调用 delete 时,变量被分配不同的内存

转载 作者:行者123 更新时间:2023-11-28 06:40:27 27 4
gpt4 key购买 nike

我正在使用链表来存储二进制位的信息。度数表示整数 2 的幂次方。

问题出现在我的 set_bit 方法中。一旦找到“目标”节点指针,就应该删除节点指针“currentSet”。然而,不是将“currentSet”设置为空/释放内存,而是为 currentSet 提供了一个不同的、较低的内存地址。为什么是这样?我究竟做错了什么?当我明确地将 currentSet 出售给 nullptr 但不应该删除 currentSet 时,该程序可以正常工作吗?

这里是header的实现

//file is "binary.cpp"
#include <iostream>
#include "binary.h"

//using std::cout;

Binary::Binary(int x) {
firstTerm = nullptr;

while (x > 0) {
unsigned int degree = (unsigned int) (log(float(x)) / log(float(2)));
set_bit(1, degree);
x -= (int) pow(float(2), float(degree));
}
}

Binary::~Binary() {
BinaryNode *temp;

while (firstTerm != nullptr) {
temp = firstTerm->next;
delete firstTerm;
firstTerm = temp;
}
}

void Binary::set_bit(int bit, int degree) {
BinaryNode *currentSet = firstTerm;
BinaryNode *target;
std::cout << "get_bit returns " << get_bit(degree) << std::endl;

if (bit == 0 && get_bit(degree)) {
std::cout << "bit = 0 " << std::endl;
std::cout << "currentSet->degree " << currentSet->degree << std::endl;
if (currentSet->degree == degree) {
std::cout << "in if" << std::endl;
firstTerm = currentSet->next;
delete currentSet;
std::cout << "end if" << std::endl;
}
else {
target = currentSet;
std::cout << "in else" << std::endl;
while (currentSet != nullptr) {
if (currentSet->degree == degree) {
std::cout << "in nested if" << std::endl;
//std::cout << "currentSet->degree = " << currentSet->degree << std::endl;
target->next = currentSet->next;
std::cout << "currentSet before delete " << currentSet << std::endl;
delete currentSet;
std::cout << "currentSet " << currentSet << "\nend nested if" << std::endl;
}
else {
std::cout << "in nested else" << std::endl;
std::cout << "firstTerm->next = " << firstTerm->next << std::endl;
std::cout << "currentSet->next = " << currentSet->next << std::endl;
target = currentSet;
currentSet = currentSet->next;
std::cout << "currentSet->next = " << currentSet->next << std::endl;
std::cout << "end nested else\n";
}
}
}
}

else if (bit == 1 && get_bit(degree) == 0) {
std::cout << "bit = 1" << std::endl;
firstTerm = new BinaryNode(degree, firstTerm);
/*if (firstTerm->next nullptr) {
currentSet = firstTerm;
}*/
std::cout << "firstTerm = " << firstTerm << std::endl;
std::cout << "firstTerm->degree = " << firstTerm->degree << std::endl;
std::cout << "firstTerm->next = " << firstTerm->next << std::endl;
}
}

int Binary::get_bit(int degree) const {
BinaryNode *currentGet = firstTerm;
while (currentGet != nullptr) {
//std::cout << "currentGet != nullptr";
if (currentGet->degree == degree) {
return 1;
}
currentGet = currentGet->next;
}
return 0;
}

这是标题

//file is "binary.h"
#ifndef _BINARY_H_
#define _BINARY_H_
#include <iostream>

class Binary {
private:
struct BinaryNode {
int degree;
BinaryNode* next;
BinaryNode(int d, BinaryNode* n): degree(d),next(n) {}
};
BinaryNode *firstTerm;

public:
// default constructor
Binary() {
firstTerm = nullptr;
}

void set_bit(int b, int d);

int get_bit(int d) const;
#endif

这是测试文件

//file is "binary_main.cpp
#include <iostream>
#include "binary.h"

using namespace std;

int main (void)
{
cout << "\nTESTING GET AND SET METHODS" << endl;
b1.set_bit(1, 2);
b1.set_bit(1, 5);
b1.set_bit(1, 0);
b1.set_bit(0, 2);
}

已编辑

最佳答案

显式设置 currentSet 为 nullptr,使用来自 n.m. 的信息找到了答案。

关于c++ - 为 LinkedList 调用 delete 时,变量被分配不同的内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26074824/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com