Avoid memory leakage in return function

Asked

Viewed 191 times

0

There is an exercise in the C++ book that asks you to create a function that returns an object avoiding memory leakage. I did so but I’m not sure if it really works:

class Point1{
    public : int x;
    public : int y;

    Point1(int x,  int y  );
    ~Point1();
};

Point1::Point1( int x1,  int y1    ){
this->x=x1;
y=y1;
std::cout <<"oioi"<<std::endl  ;
}

Point1::~Point1(){
    std::cout <<"destruu"<<std::endl ;

}

Point1 * retorno(){

     Point1* C = new Point1(1,4);

     cout<< endl;
     return C;
}

int main() {


     Point1*C=retorno();

     cout << C ;
     cout<< endl;
     cout<< endl;
     cout<< C->x;

    return 0;
}
  • yes , but in this case I want to create the obj in function and allocate it in heap and return to dstruir with delete

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

1 answer

1

Don’t try to write Java in C++, write C++ and forget how you did it in Java. Writing the right way works and there are no leaks. There’s no reason why this code could produce a leak.

#include <iostream>
using namespace std;

class Point1 {
public:
    int x;
    int y;

    Point1(int x, int y);
    ~Point1();
};

Point1::Point1(int x1, int y1) {
    x = x1;
    y = y1;
    cout << "criou" << endl; //estou deixando só para fins didáticos
}

Point1::~Point1() {
    cout << "destruiu " << x << " " << y << endl; //para fins de debug
}

int main() {
    Point1 C(1, 4);
    cout << C.x << endl;
    cout << C.y << endl;
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

If you want to do what you shouldn’t do in this case, you can do smart pointers, which is the right way to deal with memory management in the heap.

#include <iostream>
#include <memory>
using namespace std;

class Point1 {
public:
    int x;
    int y;

    Point1(int x, int y);
    ~Point1();
};

Point1::Point1(int x1, int y1) {
    x = x1;
    y = y1;
    cout << "criou" << endl; //estou deixando só para fins didáticos
}

Point1::~Point1() {
    cout << "destruiu " << x << " " << y << endl; //para fins de debug
}

unique_ptr<Point1> retorno() {
    unique_ptr<Point1> C(new Point1(1,4));
    return C;
}

int main() {
    auto C = retorno();
    cout << C->x << endl;
    cout << C->y << endl;
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • yes , but in this case I want to create the obj in function and allocate it in heap and return to dstruir with delete -

  • ie wanted to destroy the object allocated with the new Point1 operator* C = new Point1(1,4);

  • so take the turn and passed by pointer "Point1C" the problem is that I do not know if the pointer ''''''Point1C=return();"''' realment could destroy the new point of the function

  • That function wasn’t doing anything useful so I killed her. There’s no reason to create this in heap. But if you really want to create, create and then erase in the hand with delete. Or use smart pointers which is the right way to do it. http://answall.com/q/50165/101 and http://answall.com/a/83894/101. You are thinking as a Java developer, this will not work. I edited to show this option.

  • ok obg by reply I am reading book of c++ basically was followed by an example this look

  • ok obg , I’m trying to seize c++ ,with an lvro you would have some to recommend me

  • I do not like to recommend books because the good ones are rare, and mainly updated for real, and the suitable for each person varies a lot, has some examples in tag: http://answall.com/tags/c%2b%2b/info

  • or some tutorial mainly of pointers I understood the syntax and the way of handling but I do not think that as I came from c++ I do not know well the use of them you have something libro or tutoring for beginner seize

  • Tutorials are usually done medium anyway, I never point out, mainly because who likes tutorials are the people who should use them the least.

  • ok obg by the answer I will try to learn more about pointers that has not yet entered my mind ,thanks for the help

  • If the question solved what you wanted you can accept it. Later on when you have enough reputation you can vote for everything in siote as well. See the [tour].

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.