-1

I'm confused why I am getting this error. I've looked around and have found someone with a similar error, and their issue was not allocating enough new memory for NULL. I didn't think that was the problem with mine, because I added strlen + 1? I'm lost...

//Copy Constructor
Rational::Rational(const Rational& other) :
    m_numerator(other.m_numerator), m_denominator(other.m_denominator), 
m_name(NULL)
{
    m_name = NULL;
    if (other.m_name != NULL)
    {
        this->m_name = new char[strlen(other.m_name) + 1]; //ErrorMarkHere
        strcpy(this->m_name, other.m_name);
    }
}
Hanna369
  • 61
  • 5
  • 3
    0xCC means you've read [uninitialized memory](https://stackoverflow.com/q/370195/995714) – phuclv Oct 17 '17 at 04:32
  • Error is on read, not write... `other.m_name` is non-NULL, but is it actually a properly terminated string? – hobbs Oct 17 '17 at 04:34

1 Answers1

1

0xCCCCCCCC means uninitialized memory with some compilers. Pretty much the only thing that could fail on that line would be strlen(other.m_name), where other.m_name probably is the pointer mentioned before. How is other constructed from where this crashed?

N00byEdge
  • 1,106
  • 7
  • 18
  • Rational& Rational::operator + (const Rational& x) { this->add(x); return *this; } – Hanna369 Oct 17 '17 at 18:03
  • This is the add function: Rational Rational::add(const Rational& input) { Rational r; r.m_denominator = m_denominator * input.m_denominator; r.m_numerator = (m_numerator * input.m_denominator) + (input.m_numerator * m_denominator); r.reduce(); return r; } – Hanna369 Oct 17 '17 at 18:04
  • Right, but how is `m_name` initialized? – N00byEdge Oct 19 '17 at 20:35