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