【问题标题】:RUN FAILED (exit value -1.073.740.940 )运行失败(退出值 -1.073.740.940 )
【发布时间】:2016-11-07 21:35:43
【问题描述】:

我对 c++ 和 netbeans 很陌生,在这里遇到了很大的问题,我无法确定错误可能是什么。 构建总是成功的,但是在运行我的程序时出现 RUN FAILED (exit value -1.073.740.940, total time: 2s) 错误。我的代码:

Arraylist.hpp:

#include <iostream>

namespace hfu {

class Arraylist {
private:
    double* members;
    int size;
public:
    Arraylist(int);
    ~Arraylist();
    Arraylist(const Arraylist&);
    double get(int) const;
    void set(int, double);
    Arraylist& operator=(const Arraylist&);
    //float operator[](int);
    friend std::ostream& operator<<(std::ostream&, const Arraylist&);
};
}

Arraylist.cpp:

#include "Arraylist.hpp"
#include <exception>


namespace hfu {

Arraylist::Arraylist(int i) : size(i), members(new double[size]) {
    for (int i = 0; i < size; i++) {
        set(i, 0);
    }
}

Arraylist::~Arraylist() {
    delete members;
}

Arraylist::Arraylist(const Arraylist& other) : size(other.size), members(new double[other.size]) {
    for (int i = 0; i < 5; i++) {
        set(i, other.get(i));
    }

}

double Arraylist::get(int i) const {
    if (i < 0 || i >= size) {
        throw (std::exception());
    } else {
        return members[i];
    }
}

void Arraylist::set(int i, double value) {
    if (i < 0 || i >= size) {
        throw (std::exception());
    } else {
        members[i] = value;
    }
}

Arraylist& Arraylist::operator=(const Arraylist& other) {
    size = other.size;
    for (int i = 0; i < size; i++) {
        set(i, other.get(i));
    }
    return *this;
}

//    float Arraylist::operator [](int index) {
//        return members[index];
//   }

std::ostream& operator<<(std::ostream& os, const Arraylist& list) {

    for (int i = 0; i < list.size; i++) {
        os << "[" << list.get(i) << "]";
    }
    return os;
}

}

main.cpp:

#include "Arraylist.hpp"

int main() {
try {
    auto a1 = hfu::Arraylist(10);
    std::cout << a1 << std::endl;
    auto a2 = hfu::Arraylist(10);
    std::cout << a2 << std::endl;
    auto a3 = hfu::Arraylist(10);
    std::cout << a3 << std::endl;

}
catch (std::exception& e) {
    std::cout << e.what() << std::endl;
}

}  

我可以创建 2 个 Arraylist 对象....但不能更多,它会打印前两个然后失败....但是当我创建较短的列表时,比如说 3 个只有一个大小...它会起作用...所以我认为这可能与记忆有关...但我不知所措。想法?

非常感谢!

顺便说一句:我正在使用 netbeans 8.2 和 mingw g++ 6.1.0

【问题讨论】:

  • 您在使用调试器单步执行代码时观察到了什么?
  • 退出值 -1.073.740.940 更好地理解为退出值 0xc0000374,它是一个 Windows 核心错误代码 (NTSTATUS),记录为“a heap has been corrupted”。换句话说:你有一个内存损坏错误。申请valgrind。如果您不能使用valgrind,请购买一台真正的计算机。
  • ... 浏览您的代码,您的复制构造函数中有一个明显的错误:for (int i = 0; i &lt; 5; i++) { 应该是i &lt; size,不是吗?我不确定这是否会导致您描述的问题,但我没有看到任何其他明显错误。
  • 哦,那只是为了测试.. 抱歉,实际上是 i
  • @JaninePolster,试试这个private: int size; double* members;

标签: c++ netbeans


【解决方案1】:

成员变量的初始化顺序由它们在类中的定义顺序定义,而不是它们在初始化列表中的顺序。事实上,你的编译器应该给你 "hfu::Arraylist::size will be initialized after" 警告。

所以在你的情况下,members 在size 之前被初始化,因此当size 仍然是垃圾时调用new double[size]。在我的情况下,它只会导致 std::bad_array_new_length 被抛出。在你的情况下,事情变得更糟,你的应用程序崩溃了。

所以解决办法就是改变

class Arraylist {
private:
    double* members;
    int size;

到

class Arraylist {
private:
    int size;
    double* members;

【讨论】:

    猜你喜欢
    • 2018-10-20
    • 1970-01-01
    • 2018-09-14
    • 1970-01-01
    • 2021-06-11
    • 1970-01-01
    • 2021-10-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多