【问题标题】:Unhandled exception at 0x5E10F1C0 (ucrtbased.dll) in HW 5 Rational pt2.exe: 0xC0000005: Access violation reading location 0xCCCCCCCC. occurredHW 5 Rational pt2.exe 中 0x5E10F1C0 (ucrtbased.dll) 处的未处理异常:0xC0000005:访问冲突读取位置 0xCCCCCCCC。发生了
【发布时间】:2017-10-17 04:30:00
【问题描述】:

我很困惑为什么会收到此错误。我环顾四周,发现有人有类似的错误,他们的问题是没有为 NULL 分配足够的新内存。我不认为这是我的问题,因为我添加了 strlen + 1?我迷路了……

//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);
    }
}

【问题讨论】:

  • 0xCC 表示您已阅读uninitialized memory
  • 读取错误,而不是写入错误...other.m_name 不是 NULL,但它实际上是一个正确终止的字符串吗?

标签: c++ pointers strcpy


【解决方案1】:

0xCCCCCCCC 表示某些编译器的未初始化内存。几乎唯一可能在该行失败的是strlen(other.m_name),其中other.m_name 可能是前面提到的指针。 other 是如何从崩溃的地方构建的?

【讨论】:

  • Rational& Rational::operator + (const Rational& x) { this->add(x);返回*这个; }
  • 这是添加函数: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();返回 r; }
  • 对,但是m_name是怎么初始化的?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-05
  • 2016-11-12
相关资源
最近更新 更多