0
#include <iostream>
using namespace std;

class A{
    int * p;
    public:
        A(){
            cout << "Default Constructor" << endl;
            p = new int[10]{0};
        }
        A(const A& obj){
            cout << "Copy Constructor" << endl;
            p = new int[10]{0};
            for(int i=0;i<10;i++)
                p[i] = obj.p[i];
        }
        A(A&& obj){
            cout << "Move Constructor" << endl;
            delete [] p;
            p = obj.p;
            obj.p = nullptr;
        }

        A& operator = (const A & obj){
            cout << "Copy Assignment Operator " <<endl;
            if(this == &obj)
                return *this;

            p = new int[10]{0};
            for(int i = 0 ;i < 10;i++)
                p[i] = obj.p[i];

            return *this;    
        }

        A& operator = (A&& obj){
            cout << "Move Assignment Operator" <<endl;
            if(this == &obj)
                return *this;

            delete [] p;
            p = obj.p;
            obj.p = nullptr;

            return *this;    
        }

        ~A(){
            cout << "Destructor " << endl;
            delete [] p;
        }
        void setData(){
            for(int i = 0 ; i < 10 ;i++)
                p[i] = i*i;
        }
        void printData(){
            for(int i=0;i<10;i++)
                cout << p[i] << " ";
            cout << endl;    
        }
};

int main()
{
    A obj ;
    obj.setData();
    obj.printData();

    A soj(std::move(obj));
    soj.printData();

}

I am writing sample code and I am facing Following error , After checking through valgrind --track-origins=yes ./a.out

==1198431== Conditional jump or move depends on uninitialised value(s)
==1198431==    at 0x1093BC: A::A(A&&) (in /home/pankaj/practice/PracticeC++Advanced/a.out)
==1198431==    by 0x109233: main (in /home/alex/practice/PracticeC++Advanced/a.out)
==1198431==  Uninitialised value was created by a stack allocation
==1198431==    at 0x1091DA: main (in /home/alex/practice/PracticeC++Advanced/a.out)

How to Fixed the above error ?

user4581301
  • 33,082
  • 7
  • 33
  • 54
  • 7
    Why do you `delete[] p` in your constructor? Initialize it to `nullptr`. – tkausl Feb 09 '23 at 18:31
  • Side note: Some other tools to run your program through: https://godbolt.org/z/ob96rPPrz I ran this to see if it could give a better diagnostic. No such luck, but it did find some other problems. Which, now that I look at it closer, turn out to be another facet of the same error. – user4581301 Feb 09 '23 at 18:40
  • Not what you are asking about, but why allocate at all when the size is always 10? – john Feb 09 '23 at 18:40
  • Handy reading for when you want to write a quick and simple assignment operator: [What is the copy-and-swap idiom?](https://stackoverflow.com/q/3279543/4581301) Note the commentary pointing out that it's not the most efficient solution, but it's so simple and near-foolproof that it makes an excellent starting point. Profiling will let you know if you care that it's not the peak of performance. – user4581301 Feb 09 '23 at 18:45
  • 1
    @tkausl it is initialized on the line below the deletion. The deletion should simply be removed. – Tommy Andersen Feb 09 '23 at 18:48
  • Why this `delete[] p` in the constructor is a bug: This is a brand-new object and `p` is a member. There are no left-overs like you need to deal with in the assignment operator of a pre-existing instance. In this constructor `p` was not initialized and nothing has been assigned to `p` yet that needs to be cleared. – user4581301 Feb 09 '23 at 18:50
  • yes by removing delete[] p from constructor that problem solve. Thank you for help. – PANKAJ LADE Feb 09 '23 at 18:53
  • Personal opinion: I consider self assignment to almost always be a bug in the program. Rather than testing and ignoring, I'll put in an `assert` so that the mistake will picked off during debugging and save on the time wasted checking in release builds. – user4581301 Feb 09 '23 at 18:54

0 Answers0