c++) Problems that are automatically deleted and not returned when returning an object in the operator function

Asked 1 years ago, Updated 1 years ago, 80 views

I'm writing a linked list.

I wrote +=, = operator and checked that it works properly.

After that, write the + operator.

unvlist unvlist::operator+(const unvlist &ul){
    unvlist *list = new unvlist();
    *list = *this;
    *list += ul;
    return *list;
}

I wrote it like this, so I don't know the reason for the leak (12byte) Detected memory leaks! Dumping objects -> {216} normal block at 0x0122B298, 12 bytes long. Data: < 8 " " > 05 00 00 00 38 B3 22 01 D0 B4 22 01 Object dump complete. It happens, but I think it's because there's no process of generating and deleting *list in the function.

unvlist unnvlist::operator+(constunvlist &ul) {
    unvlist list = unvlist();
    list = *this;
    list += ul;
    return list;
}

I wrote it like this.

unvlist=unvlist(), ll=unvlist();
l = *l1;
l += *l2;
ll = *l1 + *l2;

If you run l, it works fine, but operator= fails the moment you run l = *l1 + *l2

The reason is that *l1 + *l2 goes in size normally, but none of the remaining nodes go in. (NULL)

As I checked within the operator+ function, it works fine until the return.

Below is my destructor function

unvlist::~unvlist(){
    clear();
    //  //  free(this);
}
void unvlist::clear(){
    NodeBase* T = Tail;
    NodeBase* tmp = Head;
    NodeBase* ttmp;
    while (tmp != NULL){
        ttmp = tmp->nextNode;
        delete tmp;
        tmp = ttmp;
    }
    Tail = NULL;
    Size = 0;
}

The unvlist class is as follows:

class unvlist {
private:
    int Size;
    NodeBase* Head;
    NodeBase* Tail;
public:
    unvlist operator+(const unvlist &ul);
    unvlist& operator=(const unvlist &ul);
    unvlist& operator+=(const unvlist &ul);
    bool operator==(const unvlist &ul);
    bool operator!=(const unvlist &ul);
    ....
}

That is, to summarize,

The value of *l1+*l2 is automatically deleted before it is returned (from operator+), so it is not returned normally.

What's the cause?

https://kldp.org/node/132271

I think it's a similar case to me, so I looked into it, but I don't know why there's an error even though I just set the return type as a type, so I'm asking you a question.

c++ return operator

2022-09-22 16:45

1 Answers

One of the things that C++ makes a lot of mistakes is the copy maker.

The copy generator is the constructor called when forwarding in the form call by value.

This means that the copy generator is called when it is handed over to a function factor other than a pointer (*) or reference (&) type, or when it is taken over by return.

The Copy Creator's role determines how to copy instances (objects), and by default, it performs a shallow copy. However, if you create memory with new or malloc in the constructor and release memory from the destructor, you must create a copy generator to perform a deep copy.

I suspect this is not the problem.

Note

Light copy If the data member has a pointer, copy only the address of the point. In this case, the original and the copy point to the same pointer, and if either of them releases the pointer's memory, the pointer is also released on the other.

Deep Copy Replicate all the pointers on the data members so that no data is shared between the copy and the source.


2022-09-22 16:45

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.