0

我最近从windows上的dev C++切换到osx dev环境,并试图使用崇高的文本3.但是,当我运行我的程序时,出现了分段错误。以下是错误消息:sublime text C++编译会导致seg fault

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

} 

这是怎么回事?

+0

如果您手动运行该命令,会发生什么情况? – Praetorian

+0

'sizeof(flightDirections)'这是什么值?你调试了你的代码吗? – PaulMcKenzie

+0

@Praetorian我该怎么做?谢谢 –

回答

0

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

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

你应该简单地使用:

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

非常感谢你!这是问题所在,我习惯于使用更简单的方法来计算数组大小。 –

2

你的问题是你的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; 
} 
0

sizeof给人以字节为单位的大小,数组的元素数量不限。当你打电话给sizeof(flightDirections)时,你可能会预期16,但是惊慌更大。因此,你的循环太过分了,超出了你的数组的索引,并且你得到了未定义的行为 - 在这种情况下,出现了分段错误。

由于你已经硬编码了数组的大小,你甚至可以硬编码循环。或者,如果你想拥有更灵活的代码,你可以通过将的sizeof(flightDirections)的reuslt由单一元素的大小,类似这样的计算元素的数量:

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

(这招很常见在C语言中练习时,你必须将一个数组传递给一个函数:因为数组会失去它的大小(当它传递给指针时会衰减),你必须显式地传递它作为第二个参数,这是正常的方式)

0

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

How do I find the length of an array?

你可以使用一个std :: vector的呢?这有一个size()方法。

+0

或者'std :: array'。 – Biffen

+0

不幸的是,这是一个类的赋值,我们被标记为使用我们还没有教过的编程技术 –

+0

@ mc-lunar哇,我会找到其他类 – Biffen