What mistake could cause this copy constructor to have read access violation error?

-2

I get an error such as this: Exception thrown at 0x0FE1F6E0 (ucrtbased.dll) in Assignment2.exe: 0xC0000005: Access violation reading location 0xCCCCCCCC.

I tried to trace the code, it blows at a place I cannot comprehend. I would like to simply know what could be possibly wrong with my point such that I would get this error. Both my constructors for Point have an initialized label value. My label is of type char*.

Point::Point() {
    this->x = 0;
    this->y = 0;
    char def[9] = "Default";
    label = new char[9];
    std::copy(def, def + 8, label);
}

Point::Point(int xinit, int yinit, const char* labelinit) {
    x = xinit;
    y = yinit;
    label = new char[strlen(labelinit) + 1]; 
    std::copy(labelinit, labelinit + strlen(labelinit) + 1, label);
}

Point::Point(const Point &copy) {
    x = copy.x;
    y = copy.y;
    if(copy.label)
        label = new char[strlen(copy.label) + 1]; //blows right here, when trying to access copy.label, even though copy.label is not NULL
    std::copy(copy.label, copy.label + strlen(copy.label) + 1, label);
}
c++
asked on Stack Overflow Apr 28, 2019 by Purple_Key

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0