What is wrong with my assignment operator??

I am having a serious issue with this. It works and does what it is suppose to do but the object that is created using it doesn't seem to agree with my destructor at the end of the program. Also if I try and make an object created with the default constructor = to some other object i get and error when the program hits the delet [] productDescription. If i use my overloaded >> operatpor or my setters to set the data members of the object created with the default constructor everything runs fine until the very end when my destructor runs.

The error is as follows:
HEAP CORRUPTION DETECTED: after Normal block (#163) at 0x03249D0.
CRT detected that the application wrote to memory after end of heap buffer.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
Product::Product() //default constructor (works)
{
    increaseProductNum++;
    productNumber = increaseProductNum;
    
    sizeOfString = 0;
    productDescription = new char[sizeOfString];
	 for (int i = 0; i < sizeOfString+1; i++)
    {
        productDescription[i] = 0;
    }
    unitCost = 0.00;
}

const Product &Product::operator= (const Product &productToCopy) //assignment operator 
{
    if (&productToCopy != this)
    {
        productNumber = productToCopy.productNumber;
        unitCost = productToCopy.unitCost;
        
       if(sizeOfString != productToCopy.sizeOfString)
       {
           delete [] productDescription;
           sizeOfString = productToCopy.sizeOfString;
           productDescription = new char[sizeOfString];
       }
        for (int i = 0; i < sizeOfString+1; i++)
        {
            productDescription[i] = productToCopy.productDescription[i];
        }
    }
    return *this;
You are accessing productDescription[sizeOfString] which is out of bounds. Change your loop condition or make the array one bigger.
You have the same problem in your default constructor.
Wow. Thanks. Works fine. I have it right in several other places in the class i can't believe i missed that. Thanks again.
Topic archived. No new replies allowed.