我最近从windows上的dev c ++切换到osx dev环境,并尝试使用sublime文本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;
}
发生了什么事?
答案 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;
}
答案 1 :(得分:0)
您错误地使用了sizeof
。 sizeof
返回实体包含的 bytes 的数量,而不是条目的数量(对于数组的情况)。因此,这是不正确的:
for (int i = 0; i < sizeof(flightDirections); i++) {
你应该只是使用:
for (int i = 0; i < sizeof(flightDirections)/sizeof(flightDirections[0]); i++) {
答案 2 :(得分:0)
sizeof
给出大小(以字节为单位),而不是数组的元素数。当你打电话给sizeof(flightDirections)
时,你可能会期待16,但恐惧更多。因此,你的循环走得太远,超出了数组的索引,并且你得到了未定义的行为 - 在这种情况下,是一个分段错误。
由于您已经对数组的大小进行了硬编码,因此您甚至可以对循环进行硬编码。或者,如果您想拥有更灵活的代码,您可以通过将sizeof(flightDirections)的重新除以单个元素的大小来计算元素的数量,如下所示:
for (int i = 0; i < sizeof(flightDirections)/sizeof(flightDirections[0]); i++) {
(这个技巧在C中是常见的做法,当你必须将一个数组传递给一个函数时:由于数组会失去它的大小(当它传递给它时衰减到一个指针),你必须明确地传递它,作为第二个参数,这是正常的做法。)
答案 3 :(得分:0)
如果要使用sizeof()来查找数组的长度,则需要将其除以数组中保存的内容。看到这个问题:
How do I find the length of an array?
你可以使用std :: vector吗?这有一个size()方法。