【发布时间】:2015-06-19 23:27:57
【问题描述】:
我无法理解我的问题。我有文件:
/* main.C */
#include <iostream>
#include "point.h"
using namespace std;
int main()
{
Point p_default;
p_default.print();
Point p_equal(2.5);
p_equal.print();
Point p_full(1.23, 2.4, 0.18);
p_full.print();
return 0;
}
/* point.h */
#include <iostream>
using namespace std;
class Point {
double x, y, z;
double* arr;
public:
// constructors
Point (); // default
Point (double); // equal arguments
Point (double _x, double _y, double _z); // standard
// destructor
~Point ();
// print function
void print () const;
};
/* point.C */
#include <iostream>
#include "point.h"
using namespace std;
// constructors
Point::Point () : Point(0.0) {}; // default - zero initialised
Point::Point (double _c) : Point(_c, _c, _c) {}; // equal arguments
// standard constructor
Point::Point (double _x, double _y, double _z = 0.0)
: x(_x), y(_y), z(_z) {
double* arr = nullptr;
arr = new double[3];
*arr = x;
*(arr + 1) = y;
*(arr + 2) = z;
};
// destructor
Point::~Point () {
delete[] arr;
};
// print function
void Point::print () const {
cout << "Point(" << x << ", " << y << ", " << z << ")" << endl;
};
我使用以下命令编译我的项目:g++ -Wall -std=c++11 main.C point.C -o main。它编译时没有任何错误或警告,但是当我使用./main 运行它时,它会正确打印所有内容,最后给我Segmentation fault:
Point(0, 0, 0)
Point(2.5, 2.5, 2.5)
Point(1.23, 2.4, 0.18)
Segmentation fault (core dumped)
我认为它与我的析构函数有关,但无法理解问题出在哪里。
【问题讨论】:
-
你为什么需要
arr?即使是这样,也应该是std::array<double,3> arr; -
@πάνταῥεῖ 因为
Point是一个数组。 -
使用所有警告和调试信息 (
g++ -Wall -Wextra -std=c++11 -g main.C point.C -o main) 进行编译,然后使用调试器 (gdb) 和valgrind(如果有)。对于最近的 GCC,还可以考虑将-fsanitize=address传递给g++ -
您忘记了
Point需要的复制构造函数和赋值运算符。 -
@Alexandr 如果您坚持自己管理内存(恕我直言,这是一个有点愚蠢的决定),您还应该在默认构造函数中将
arr初始化为nullptr,其中包括正确处理复制构造和任务。
标签: c++ constructor segmentation-fault destructor