繁体   English   中英

分配的内存被回收

[英]allocated memory gets reclaimed

嗨,我想编码自己的列表类,实际上它的工作原理类似于std :: vector类。 问题是当我使用new为下一个列表分配内存时,它工作正常。 但是当我尝试为目标(数据)分配内存时,当程序到达push_back()范围的末尾时,它会被回收,我不明白为什么这两种方式不一样,以及如何使用分配的内存我的数据没有被破坏?

代码在这里

#include <iostream>
#include <cstdlib>

using namespace std;

struct pos{
    int x;
    int y;

    pos()
    {
        x = y = 0;
    }

    pos(int x, int y)
    {
        this->x = x;
        this->y = y;
    }

    pos& operator=(pos rhs)
    {
        x = rhs.x;
        y = rhs.y;
        return *this;
    }

    bool operator==(const pos& rhs)
    {
        if(x == rhs.x && y == rhs.y)
            return true;
        else
            return false;
    }
    ~pos()
    {
        cout << "x =" << x << ", y =" << y << "got distorted!" << endl;
    }
};


class list {
    private:
        pos *target;
        list* next;
        int index;
    public :
        list();
        list(pos target);
        void push_back (int first , int second);
        void push_back (const pos target);
        pos pop_back();
        pos* search(int first , int second);
        pos* search(pos target);
        int erase(int index);
        pos get(int index);
        void change(const pos target,int index);
        void change(int first,int second,int index);
        ~list();
};

void print(list lst);
// function declarations

list::~list()
{
    cout << "list is destroyed!" << endl;
    if(target != NULL)
        delete target;
    if(next != NULL)
        delete next;
}

list::list()
{
    target = NULL;
    next = NULL;
    index = 0;
}
list::list(pos target)
{
    this->target = new pos(target);
    index = 0;
    next = NULL;
}

void list::push_back(const pos target)
{
    cout << "push_back() begin" << endl;
    list* it = this;
    while(it->next != NULL)
    {
        it = it->next;
    }
    if(it->target == NULL)
    {
        it->target = new pos(target);
    }
    else
    {
        it->next = new list;
        it->next->index = it->index+1;
        //option one
        it->next->target = new pos(target);
        //option two
        it->next->target = (pos*)malloc(sizeof(pos));
        (*it->next->target) = target;
        //it->next->next is already NULL
    }
    cout << "push_back() end" << endl;
}

void list::push_back(int first , int second)
{
    push_back(pos(first,second));
}

pos list::pop_back()
{
    print(*this);
    list* it = this;
    cout << "address of x is" << this << endl;
    cout << "this->target is" << this->target << endl;
    cout << (*target).x << endl;



    if(it->target == NULL)
        return *(new pos); // an error is occurred there is not any data to return! must find another solution maybe throw an exception

    if(it->next == NULL)
    {
        pos return_data = *(it->target);
        delete it->target;
        it->target = NULL;
        return return_data;
    }

    while(it->next->next != NULL)
    {
        cout << "it->target is" << it->target << endl;
        it = it->next;
    }
    pos return_data = *(it->next->target);

    delete it->next;
    it->next = NULL;

    return return_data;
}

pos* list::search(pos target)
{
    list* it = this;
    do
    {
        if(target == *(it->target))
            return it->target;
        if(it->next != NULL)
            it = it->next;
        else
            return NULL;
    }while(1);
}
pos* list::search(int first , int second){
    return search(pos(first,second));
}

int list::erase(int index){
    if(index < 0)
        return 0;


    list *it = this , *it_next = this->next;
    if(index == 0)
    {
        if(it->next == NULL)
        {
            delete it->target;
            return 1;
        }
        while(it_next->next != NULL)
        {
            it->target = it_next->target;
            it = it_next;
            it_next = it_next->next;
        }//needs to be completed

    }


    do
    {
        if(it_next->index == index)
        {
            it->next = it_next->next;
            delete it_next;
            return 1;
        }
        if(it_next->next != NULL)
        {
            it = it_next;
            it_next = it_next->next;
        }
        else
            return 0;

    }while(1);


    return 1;
}

pos list::get(int index)
{
    if(index < 0)
        return *(new pos);//error
    list* it = this;
    do
    {
        if(it->index == index)
        {
            return *(it->target);
        }
        if(it->next != NULL)
            it = it->next;
        else
           return *(new pos);//error , index is bigger than [list size] - 1

    }while(1);
}

void list::change(const pos target,int index)
{
    if(index < 0)
        return ;//error
    list* it = this;
    do
    {
        if(it->index == index)
        {
            *(it->target) = target;
        }
        if(it->next != NULL)
            it = it->next;
        else
           return;//error , index is bigger than [list size] - 1

    }while(1);
}
void list::change(const int first,const int second,int index)
{
    change(pos(first,second),index);
}

void print(list lst)
{
    int idx = 0;
    while(!(lst.get(idx)==pos(0,0)))
    {
        cout << "index " << idx << " : x = " << lst.get(idx).x << ", y = " << lst.get(idx).y << endl;
        idx++;
    }
}


int main(int argc, char const *argv[])
{
    list x;
    cout << "address of x is" << &x << endl;
    x.push_back(1,1);
    x.push_back(2,2);
    x.push_back(3,3);
    x.push_back(4,4);
    x.push_back(5,5);
    print(x);
    cout << "--------------------------" << endl;
    x.pop_back();
    print(x);
    cout << "--------------------------" << endl;
    cout << x.get(2).x << endl;
    x.erase(2);
    print(x);
    cout << "--------------------------" << endl;
    return 0;
}

换句话说,为什么push_back返回时它-> next-> target和/或it-> target被破坏?

正在销毁(或调用析构函数)的是在下一行作为输入参数创建的临时对象:

push_back(pos(first,second));

void list::push_back(const pos target)

target是按值传递的,因此push_backtarget是一个临时副本。 函数结束时,副本将超出范围并被销毁。 这就是您所看到的。 烦人,但不是您真正的问题。

list违反三规则 这意味着在复制list时,不会正确复制它。 指针将被复制,而不是指向的项目。 每次复制list ,原件和复制点都位于同一位置。 当副本超出范围并被销毁时,它将随其携带原始数据。

碰巧的是,您只按值传递,因此有很多复制和销毁操作。 例如, print功能将错误地复制并在返回时清除提供的list

解决方案:在list中添加一个复制构造函数和一个赋值运算符,以遍历列表的方式进行工作,并复制所有链接,并通过引用进行读取。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM