为LinkedList调用delete时,会为变量分配不同的内存

时间:2014-09-27 12:46:04

标签: c++ linked-list

我使用链表来存储二进制位的信息。 Degree告诉哪个幂可以将整数2提高到。

我的set_bit方法出现问题。一旦找到“目标”节点指针,就应该删除节点指针“currentSet”。但是,不是将“currentSet”设置为null /释放内存,而是为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);
}

编辑

1 个答案:

答案 0 :(得分:1)

将currentSet显式设置为nullptr, 使用来自n.m。

的信息找到答案