Looked at the lecture here. Describes how to create a class for an array. In particular, we are talking about redefining the assignment operator and its work for the case a = a; (Timing 1: 2: 40).
Array& Array::operator=(MyArray &a){ mySize = a.mySize; delete [] myData; myData = new int[mySize]; for(int i=0;i<mySize;i++){ myData[i] = a.myData[i]; } return *this; }
Well, somewhere goes:
Array a(10); a.set(0,1000);//a[0]==1000; a=a; //a[0]==?
And it is declared that in this case, after freeing memory through delete, the old pointer may already contain garbage and will continue to copy this garbage.
Question: where will a.myData
refer a.myData
after myData = new int[mySize]
?
I have a feeling that the new area, not the old one, and garbage (in the form of zeros, for example) will always be there, and not so that after deletion, "another part of our program can start using this area" and therefore there will be garbage which we copy later.