-1

I have this code of overloaded operator for vectors class, which is a class of vector of dimension 3:

class Vectors
    {
    //private:
    //    double* vector;
    public:
        double* vector;
        Vectors(double*);
        Vectors();
        Vectors(const Vectors&);
        ~Vectors();
        void print_vector();
    };

Class Methods: Constructors:

Vectors::Vectors(double* value)
{
    this->vector = new double[3];
    for (auto i = 0; i < 3; i++)
    {
        this->vector[i] = value[i];
    }
}

Vectors::Vectors()
{
    this->vector = new double[3];
    for (auto i = 0; i < 3; i++)
    {
        this->vector[i] = 0.0;
    }
}

Copy Constructors:

Vectors::Vectors(const Vectors& copy)
{
    this->vector = new double[3];
    for (auto i = 0; i < 3; i++)
    {
        this->vector[i] = copy.vector[i];
    }
}

Destructor:

Vectors::~Vectors()
{
    //cout << "desctructor called \n";
    delete[] vector;
}

void Vectors::print_vector(){
    for (int i=0; i<3 ;i++){
        cout << this->vector[i];
        cout << '\t';
    }
    cout << endl;
}

Overloaded Operator:

Vectors operator+(const Vectors& obj1,const Vectors& obj2)
{
    Vectors *res = new Vectors;
    for (int i = 0; i < 3; i++){
        //cout << obj1.vector[i] << '\t' << obj2.vector[i] << '\t' << obj1.vector[i] + obj2.vector[i] << endl;
        res->vector[i] = obj1.vector[i] + obj2.vector[i];
        //cout << res->vector[i] << endl;
    }
    //res->print_vector();
    return *res;
}



int main()
{
    double a[3] = {3.5,4.10,5.30};
    double b[3] = {7.0,8.0,9.0};
    Vectors* A = new Vectors(a);
    Vectors* B = new Vectors(b);
    Vectors* C = new Vectors();
    *C = *A+*B; // This does not work
    (*A+*B).print_vector(); // This works
    C->print_vector();

    Vectors D = *A+*B; // This works
    D.print_vector();
}

When creating a pointer to an object Vectors, I get different results when compared to creating direct objects.

Output is: 1st Output: 0 4.68558e-310 14.3 2nd Output: 10.5 12.1 14.3 3rd Output: 10.5 12.1 14.3

  • Show the `Vectors` class. – fabian Aug 28 '22 at 08:26
  • Not reproducible on godbolt btw: https://godbolt.org/z/GnEfKjxdq – fabian Aug 28 '22 at 08:38
  • 2
    Why are you using `new` to create the `Vectors` object? Why not simply declare a `Vectors` and return that instead? And in `main`, why `new Vectors` instead of simply `Vectors A(a), B(b), C(c);`? Your code has memory leaks in multiple places. – PaulMcKenzie Aug 28 '22 at 08:48
  • Almost certainly the problems are caused by a bugged `Vectors` class. I can guess what the problem is but I would rather see the code instead of guessing. But just in case my guess is right you should read about the [rule of three](https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three) Probably the explains that problems you are having. – john Aug 28 '22 at 08:53
  • 2
    `Vectors *res = new Vectors;` leaks memory as the original pointer is thrown away in `operator+`. – Richard Critten Aug 28 '22 at 08:54
  • My crystal ball says your implementation of `Vectors::operator=` is incorrect. – Igor Tandetnik Aug 28 '22 at 16:14
  • @fabian Here is the vectors class: – Abhijit Baishya Aug 29 '22 at 14:14
  • @PaulMcKenzie Simply declaring vectors works but with pointers not working properly, I want to know why? Is it because I have not implemented any overloaded assignment operator? – Abhijit Baishya Aug 30 '22 at 05:11
  • @RichardCritten Can you please explain? I am new to C++ and all these pointers. – Abhijit Baishya Aug 30 '22 at 05:14
  • @AbhijitBaishya *Why not simply declare a Vectors and return that instead?* -- Again, that `new` is inside of `operator +` -- `Vectors *res = new Vectors;`. It shouldn't be there. The basic point is this --- the implementation of the `Vectors` class should be solid -- no games with pointers like this should be going on -- just declare a `Vectors`, fill it in with the information, and return it by value. If the person **using** the `Vectors` class wants to use pointers to create them dynamically, well that's their (ill-advised) choice. – PaulMcKenzie Aug 30 '22 at 07:20
  • Also, you failed to implement an assignment operator: Thus this simple code has issues: `int main() { Vectors v; Vectors v2; v = v2; }` – PaulMcKenzie Aug 30 '22 at 07:23
  • @PaulMcKenzie Thank you very much for your answer. I have implemented an assignment operator and now the results are consistent. – Abhijit Baishya Aug 30 '22 at 08:25
  • @AbhijitBaishya You should always `delete` what you `new` (with a few exceptions). If you don't the program will leak memory. – Richard Critten Aug 30 '22 at 11:43
  • @RichardCritten Thank you, I have given the updated code in the answers. It still leaks memory. – Abhijit Baishya Aug 30 '22 at 13:01

1 Answers1

0

This version works, but I have still got memory leak using valgrind. Can somebody point out where?

#include <iostream>

using namespace std;

class Vectors
    {
    //private:
    //    double* vector;
    public:
        double* vector;
        Vectors(double*);
        Vectors();
        Vectors(const Vectors&);
        ~Vectors();
        void print_vector();
        Vectors operator=(const Vectors&);
    };


Vectors::Vectors(double* value)
{
    this->vector = new double[3];
    for (auto i = 0; i < 3; i++)
    {
        this->vector[i] = value[i];
    }
}

Vectors::Vectors()
{
    this->vector = new double[3];
    for (auto i = 0; i < 3; i++)
    {
        this->vector[i] = 0.0;
    }
}

Vectors::Vectors(const Vectors& copy)
{
    this->vector = new double[3];
    for (auto i = 0; i < 3; i++)
    {
        this->vector[i] = copy.vector[i];
    }
    //this->vector = (copy.vector);
}

Vectors::~Vectors()
{
    //cout << "desctructor called \n";
    delete[] vector;
}

void Vectors::print_vector(){
    for (int i=0; i<3 ;i++){
        cout << this->vector[i];
        cout << '\t';
    }
    cout << endl;
}

Vectors Vectors::operator=(const Vectors& obj1) 
{
    this->vector = new double[3];
    if (this != &obj1)
    {
    for (int i = 0; i < 3; i++) {
        this->vector[i] = obj1.vector[i];
    }
    }
    return *this;
}


Vectors operator+(const Vectors& obj1,const Vectors& obj2) 
{
    Vectors res;
    for (int i = 0; i < 3; i++){
        res.vector[i] = obj1.vector[i] + obj2.vector[i];
    }
    return res;
}


int main()
{
    double a[3] = {3.5,4.10,5.30};
    double b[3] = {7.0,8.0,9.0};

    Vectors* A = new Vectors(a);
    Vectors* B = new Vectors(b);
    Vectors* C = new Vectors();
    *C = *A+*B;
    C->print_vector();
    //(*A+*B).print_vector();
    //cout << C->vector[1]<<endl;
    
    //double a[3] = {3.5,4.10,5.30};
    //double b[3] = {7.0,8.0,9.0};
    //Vectors A(a), B(b);
    Vectors D = *A+*B;
    //Vectors C = A-B;
    //Vectors C = A*B;
    //Vectors C = 5*A;
    //Vectors C = 5.5*A;
    //Vectors C = A/B;
    //Vectors C = 5/A;
    //Vectors C = 5.5/A;
    D.print_vector();
    delete[] A;
    delete[] B;
    delete[] C;
}