sublime text C++ 编译导致段错误

sublime text C++ compilation causes seg fault

我最近从 windows 上的开发 c++ 切换到 osx 开发环境,并尝试使用 sublime text 3。但是,当我 运行 我的程序时,我得到分段错误。这是错误消息:

/bin/bash: line 1: 20506 Segmentation fault: 11  "/Users/jimi/Documents/University/lab0603"
[Finished in 1.2s with exit code 139]
[shell_cmd: g++ "/Users/jimi/Documents/University/lab0603.cpp" -o "/Users/jimi/Documents/University/lab0603" && "/Users/jimi/Documents/University/lab0603"]
[dir: /Users/jimi/Documents/University]
[path: /usr/bin:/bin:/usr/sbin:/sbin]

完整代码如下:

#include <iostream>
#include <cmath>

using namespace std;


string flightDirections[16] = {"ENE", "NE", "NNE", "N", "NNW", "NW", "WNW", "W", "WSW", "SW", "SWS", "S", "SSE", "SE", "ESE", "E"};
double cruisingSpeed = 0;
double windSpeed = 0;
int windDirection = 0;
double flightDistance = 0;

string numberToDirection(int direction) {

    return (direction) ? "The wind is blowing from the East." : " The wind is blowing from the West.";

    //this way we only have one return statement :)

}

void getInput () {
    cout << "Hello! \n"
         << "Thank you for choosing to use this really great program!! \n"
         << "This program will compute the necessary heading adjustment for your flight,"
         << " and provide the estimated flight time. \n";
    cout << "Enter the aircraft cruising speed in still air (in km/h): ";
    cin >> cruisingSpeed;
    cout << " \n \t cruising speed = " << cruisingSpeed << "\n Enter the wind speed in km/h: ";
    cin >>  windSpeed;
    cout << " \n \t wind speed = " << windSpeed << "\n Enter 1 if the wind is blowing from the West and -1 if wind is blowing from the East:";
    cin >> windDirection;
    cout << "\n\t" << numberToDirection(windDirection) << "\n Enter the distance between the originating and destination cities, in km:";
    cin >> flightDistance;
    cout << "\n\t flight distance = " << flightDistance << "\n Enter the compass direction of the destination city, relative to the originating cities, using the following values:";
    for (int i = 0; i < sizeof(flightDirections); i++) {
        cout << i + 1;
        cout << flightDirections[i];
    }
    cin.ignore();


}

int main() {


    getInput();
    return 0;

}

这是怎么回事?

您错误地使用了 sizeofsizeof returns 实体包含的 字节数 ,而不是条目数(对于数组的情况)。因此,这是不正确的:

 for (int i = 0; i < sizeof(flightDirections); i++) {

你应该简单地使用:

for (int i = 0; i < sizeof(flightDirections)/sizeof(flightDirections[0]); i++) {

您的问题出在您的 for 循环上。

for (int i = 0; i < sizeof(flightDirections); i++) 

将 运行 关闭数组的末尾,因为 sizeof(flightDirections) 大于数组的大小。 sizeof(flightDirections)sizeof(std::string) * number_of_elements_in_the_array。为了获得正确的数组大小,您需要使用

sizeof(flightDirections) / sizeof(flightDirections[0])

或者更好的是使用 ranged based for loop 作为

int i = 0;
for (const auto & e : flightDirections) {
    cout << ++i << e;
}

sizeof 给出以字节为单位的大小,而不是数组元素的数量。当您调用 sizeof(flightDirections) 时,您可能期望 16,但结果要多得多。因此,您的循环走得太远,超出了数组的索引,并且您会得到未定义的行为 - 在本例中为分段错误。

由于您已经对数组的大小进行了硬编码,因此您甚至可以对循环进行硬编码。或者,如果你想有更灵活的代码,你可以通过将 sizeof(flightDirections) 的结果除以单个元素的大小来计算元素的数量,像这样:

for (int i = 0; i < sizeof(flightDirections)/sizeof(flightDirections[0]); i++) {

(这个技巧在 C 中很常见,当你必须将一个数组传递给一个函数时:因为数组会失去它的大小(当传递它时它会衰减为一个指针),你必须显式地传递它,因为第二个参数,这是正常的做法。)

如果你想使用sizeof() 来计算数组的长度,你需要将它除以数组中的内容。看到这个问题:

How do I find the length of an array?

可以改用 std::vector 吗?有一个 size() 方法。