Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用SmartPointer在列表中执行错误访问_C++_Macos_Qt_Memory Leaks_Smart Pointers - Fatal编程技术网

C++ 使用SmartPointer在列表中执行错误访问

C++ 使用SmartPointer在列表中执行错误访问,c++,macos,qt,memory-leaks,smart-pointers,C++,Macos,Qt,Memory Leaks,Smart Pointers,当我从列表中删除一个元素时,我面临这个问题 这是我的清单 class AwesomeList { friend class Iteratore; private: class Nodo; class SmartPointer { public: Nodo* punt; SmartPointer(Nodo* n = 0): punt(n) {} SmartPointer(const SmartPointer& ptr): punt(ptr.punt) {}

当我从列表中删除一个元素时,我面临这个问题

这是我的清单

class AwesomeList {
friend class Iteratore;
private:
class Nodo;

class SmartPointer {
public:
    Nodo* punt;

    SmartPointer(Nodo* n = 0): punt(n) {}
    SmartPointer(const SmartPointer& ptr): punt(ptr.punt) {}
    ~SmartPointer() {
        delete punt;
    }

    SmartPointer& operator=(const SmartPointer& ptr) {
        if (this != &ptr) {
            delete punt;
            punt = ptr.punt;
        }
        return *this;
    }
    bool operator==(const SmartPointer& ptr) const {
        return ptr.punt == punt;
    }
    bool operator!=(const SmartPointer& ptr) const {
        return ptr.punt != punt;
    }
    Nodo* operator->() const {
        return punt;
    }
    Nodo& operator*() const {
        return *punt;
    }
};

class Nodo {
public:
    T* value;
    SmartPointer next;

    Nodo(T* t = T(), const SmartPointer& ptr = SmartPointer()): value(t), next(ptr) {}
};

SmartPointer head;
SmartPointer tail;

public:
class Iteratore{
    friend class AwesomeList;
private:
    AwesomeList::SmartPointer punt;
public:
    bool operator==(const Iteratore& it) const {
        return it.punt == punt;
    }
    bool operator!=(const Iteratore& it) const {
        return it.punt != punt;
    }
    Iteratore& operator++() {
        if(punt != 0) punt = punt->next;
        return *this;
    }
    Iteratore& operator++(int) {
        if(punt != 0) punt = punt->next;
        return *this;
    }
    T* operator*() const {
        if (punt != 0) return punt->value;
    }
};

AwesomeList(const SmartPointer& ptr = 0): head(ptr), tail(0) {
    if (head != 0) {
        SmartPointer p = head;
        while (p != 0)
            p = p->next;
        tail = p;
    }
}
AwesomeList(const AwesomeList& list): head(list.head), tail(list.tail) {}

AwesomeList& operator=(const AwesomeList& list) {
    head = list.head;
    tail = list.tail;
}

int getSize() const {
    int count = 0;
    SmartPointer p = head;
    while (p != 0) {
        p = p->next;
        count++;
    }
    return count;
}
bool isEmpty() const {
    return getSize() == 0;
}
T* at(int pos) const {
    if (pos > -1 && pos < getSize()) {
        SmartPointer p = head;
        while (pos--) {
            p = p->next;
        }
        return p->value;
    } else return 0;
}
void add(const T& t) {
    if (head == 0) {
        head = SmartPointer(new Nodo(&(const_cast<T&>(t))));
        tail = head;
    } else {
        tail->next = SmartPointer(new Nodo(&(const_cast<T&>(t))));
        tail = tail->next;
    }
}
void remove(int pos) {
    if (pos > -1 && pos < getSize()) {
        SmartPointer newHead = head;
        SmartPointer p = newHead;
        head = 0;
        while (pos--) {
            add(*p->value);
            p = p->next;
        }
        p = p->next;
        while (p != 0) {
            add(*p->value);
            p = p->next;
        }
    }
}
void replace(int pos, T* t) {
    if (pos > -1 && pos < getSize()) {
        SmartPointer p = head;
        while (pos--)
            p = p->next;
        p->value = t;
    }
}
void replace(int pos, const T& t) {
    if (pos > -1 && pos < getSize()) {
        SmartPointer p = head;
        while (pos--)
            p = p->next;
        T& t_obj = const_cast<T&>(t);
        p->value = &t_obj;
    }
}

Iteratore begin() const {
    Iteratore it;
    it.punt = head;
    return it;
}
Iteratore end() const {
    Iteratore it;
    it.punt = 0;
    return it;
}
T* operator[](const Iteratore& it) const {
    return it.punt->value;
}
};

对不起,我不明白你的
SmartPointer
类下面的比率

它携带一个指向
Nodo
的指针,并使用构造函数将其删除。好

但是,如果我没有错的话

(1) 使用复制构造函数创建
SmartPointer
时,从已复制的
SmartPointer
复制指针,因此有两个具有相同值的
punt
的对象;销毁两个对象时,在同一指针上调用
delete
两次;这会使程序崩溃

(2) 调用
操作符=
时,复制构造函数也有同样的问题,但是,您没有删除旧的指向值

通过示例,查看
add()

在这种情况下,
tail->next
(并计算
take
指向已删除区域的数量)从删除该区域的临时对象接收指针。因此,您在已删除区域中写入一个指针,该指针将立即被删除

我希望大家清楚这一切有多危险

建议:重新设计
SmartPointer
class


p、 s:对不起,我的英语不好。

你的错误列表实际上没有列出错误是什么……错误是EXC\u bad\u ACCESS。我的朋友,我想你是想愚弄我们<代码>智能指针头SmartPointer的正向声明来编译code>。要实例化
智能指针
,您需要
智能指针
的完整定义。Nodo显然应该是一个模板,但没有声明为模板。我想你在
类AwesomeList
@user4581301之前删除了
模板
,程序可以编译,别担心。我请求有关错误EXC\u BAD\u访问的帮助。模板已存在。我没有包括在消息中,因为它产生了问题。你已经非常清楚了。谢谢在您看来,如果我使用一个私有字段SmartPointer*head,然后我写head=newsmartpointer(…),可以解决我的问题吗?请思考一下逻辑。您希望创建指向智能指针的原始指针。这就破坏了智能指针的作用。马克斯是对的。您需要重新实现
智能指针
,使之成为智能指针,然后执行。另一种选择是代替
SmartPointer
。是的,你说得对。有什么建议吗?PS:我已经用一个正确的操作符更新了我的代码=。@Daniele-是的,检查你是否正在复制一个对象本身是好的。但主要问题仍然是:C++可以有多个实例,代码<> SmartPointer >代码>与代码< Punt 相同,因此,您可能会删除两个或更多的同一指针的时间。(否则,我的建议是:使用
std::list
,如果可以使用C++11,
std::shared_ptr
),所以我建议(只是为了玩C++):在
Nodo
中添加计数器并将其初始化为零;每次将
Nodo
指针复制到
SmartPointer
时,增加所指向节点中的计数器;每次从
SmartPointe
释放指针时(赋值运算符中的析构函数和旧值)减去节点计数器并验证值;只有计数器(后减量)为0时,才调用<代码>删除>代码>。只需玩C++。
AwesomeList<int> list = AwesomeList<int>();
list.add(1);
list.add(2);
list.add(3);
for (int i = 0; i < list.getSize(); i++)
    qDebug() <<*(list.at(i)) <<" ";

list.remove(-1);
for (int i = 0; i < list.getSize(); i++)
    qDebug() <<*(list.at(i)) <<" ";

list.remove(2);
for (int i = 0; i < list.getSize(); i++)
    qDebug() <<*(list.at(i)) <<" ";

list.replace(0, 5);
qDebug() <<"Replace in posizione 0";
auto cit = list.begin();
for (; cit != list.end(); cit++)
    qDebug() <<*(*cit) <<" ";

qDebug() <<"Size";
qDebug() <<list.getSize() <<endl;
SmartPointer(Nodo* n = 0): punt(n) {
        if (punt) punt->references++;
    }
    SmartPointer(const SmartPointer& ptr): punt(ptr.punt) {
        if (punt) punt->references++;
    }
    ~SmartPointer() {
        if (punt) {
            punt->references--;
            if (punt->references == 0) delete punt;
        }
    }

    SmartPointer& operator=(const SmartPointer& ptr) {
        if (this != &ptr) {
            Nodo* n = punt;
            punt = ptr.punt;
            if (punt) punt->references++;
            if (n) {
                n->references--;
                if (n->references == 0) delete n;
            }
        }
        return *this;
    }
    bool operator==(const SmartPointer& ptr) const {
        return ptr.punt == punt;
    }
    bool operator!=(const SmartPointer& ptr) const {
        return ptr.punt != punt;
    }
    Nodo* operator->() const {
        return punt;
    }
    Nodo& operator*() const {
        return *punt;
    }
};

class Nodo {
public:
    T* value;
    SmartPointer next;
    int references;

    Nodo(T* t = T(), const SmartPointer& ptr = SmartPointer()): value(t), next(ptr), references(0) {}
};
    head = SmartPointer(new Nodo(&(const_cast<T&>(t))));
    tail = head;
    tail->next = SmartPointer(new Nodo(&(const_cast<T&>(t))));
    tail = tail->next;