我正在使用链表将信息存储在二进制位上。度表示将整数2幂次幂。
我的set_bit方法中出现问题。一旦找到“目标”节点指针,就应该删除节点指针“ currentSet”。但是,不是将“ currentSet”设置为空/释放内存,而是给currentSet一个不同的较低的内存地址。为什么是这样?我究竟做错了什么?该程序在我将currentSet明确出售给nullptr时有效,但是不应该删除currentSet吗?
这是标头的实现
//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/