为什么这段看似简单的 C++ 代码会产生段错误?
Why does this seemingly simple C++ code generate a segmentation fault?
我正在尝试编写增强算法(人工智能的一项功能)。速度是重中之重,所以我已经从使用我的原生 Python 切换到 C++。我写出了整个程序,但我得到了一个错误,我将其缩小为我在基础 class 中犯的一个错误:一个非常简单的启发式算法,称为 "H." 文件 h.h,h.cpp,我目前的测试函数main.cpp是:
//h.h
#ifndef __H_H_INCLUDED__
#define __H_H_INCLUDED__
#include <iostream>
#include <vector>
class H
{
public:
H(int, double, bool);
//The first parameter is the axis
//The second parameter is the cutoff
//The third parameter is the direction
bool evaluate(std::vector<double>&);
//This evaluates the heuristic at a given point.
private:
int axis;
double cutoff;
bool direction;
};
#endif
//h.cpp
#include "h.h"
H::H(int ax, double cut, bool d)
{
axis = ax;
cutoff = cut;
direction = d;
}
bool H::evaluate(std::vector<double>& point)
{
if (direction)
{
return point[axis] > cutoff;
}
else
{
return point[axis] <= cutoff;
}
}
//main.cpp
#include <iostream>
#include <vector>
#include "h.h"
int main()
{
H h(0, 2.0, true);
for (double x = 0; x < 4; x = x + 1)
{
for (double y = 0; y < 4; y = y + 1)
{
std::vector<double> point(x, y);
std::vector<double>& point_ref = point;
std::cout << "Before computation" << std::endl;
bool value = h.evaluate(point_ref);
std::cout << "After computation" << std::endl;
std::cout << "heuristic(x = " << x << ", y = " << y << ") = " << value << std::endl;
}
}
return 0;
}
(我将 "Before computation" 和 "After computation" 放入以查明错误发生在哪一行。)与我期望的输出完全相反,我得到:
Before computation
Segmentation fault (core dumped)
我做错了什么?该错误消息到底是什么意思?
谢谢!
编辑:我正在使用 C++11,仅供那些好奇的人使用。
这一行:
std::vector<double> point(x, y);
用 y
的 x
个副本制作 vector
。是 constructor #2 here。因此,当 x
为 0 时,point
为空 vector
- 这意味着您访问索引 0 处的元素是未定义的行为,在本例中表现为分段错误。
您可能打算做的是制作一个包含两个值 x
和 y
的向量,即:
std::vector<double> point{x, y}; // in c++11, note the braces
std::vector<double> point(2); // pre-c++11
point[0] = x;
point[1] = y;
我正在尝试编写增强算法(人工智能的一项功能)。速度是重中之重,所以我已经从使用我的原生 Python 切换到 C++。我写出了整个程序,但我得到了一个错误,我将其缩小为我在基础 class 中犯的一个错误:一个非常简单的启发式算法,称为 "H." 文件 h.h,h.cpp,我目前的测试函数main.cpp是:
//h.h
#ifndef __H_H_INCLUDED__
#define __H_H_INCLUDED__
#include <iostream>
#include <vector>
class H
{
public:
H(int, double, bool);
//The first parameter is the axis
//The second parameter is the cutoff
//The third parameter is the direction
bool evaluate(std::vector<double>&);
//This evaluates the heuristic at a given point.
private:
int axis;
double cutoff;
bool direction;
};
#endif
//h.cpp
#include "h.h"
H::H(int ax, double cut, bool d)
{
axis = ax;
cutoff = cut;
direction = d;
}
bool H::evaluate(std::vector<double>& point)
{
if (direction)
{
return point[axis] > cutoff;
}
else
{
return point[axis] <= cutoff;
}
}
//main.cpp
#include <iostream>
#include <vector>
#include "h.h"
int main()
{
H h(0, 2.0, true);
for (double x = 0; x < 4; x = x + 1)
{
for (double y = 0; y < 4; y = y + 1)
{
std::vector<double> point(x, y);
std::vector<double>& point_ref = point;
std::cout << "Before computation" << std::endl;
bool value = h.evaluate(point_ref);
std::cout << "After computation" << std::endl;
std::cout << "heuristic(x = " << x << ", y = " << y << ") = " << value << std::endl;
}
}
return 0;
}
(我将 "Before computation" 和 "After computation" 放入以查明错误发生在哪一行。)与我期望的输出完全相反,我得到:
Before computation
Segmentation fault (core dumped)
我做错了什么?该错误消息到底是什么意思?
谢谢!
编辑:我正在使用 C++11,仅供那些好奇的人使用。
这一行:
std::vector<double> point(x, y);
用 y
的 x
个副本制作 vector
。是 constructor #2 here。因此,当 x
为 0 时,point
为空 vector
- 这意味着您访问索引 0 处的元素是未定义的行为,在本例中表现为分段错误。
您可能打算做的是制作一个包含两个值 x
和 y
的向量,即:
std::vector<double> point{x, y}; // in c++11, note the braces
std::vector<double> point(2); // pre-c++11
point[0] = x;
point[1] = y;