0

I have recently switched from dev c++ on windows to an osx dev environment and am trying to use sublime text 3. However, when I run my program, I get a segmentation fault. Here is the error message:

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

Here is the full code:

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

}

What is going on?

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
mc-lunar
  • 293
  • 2
  • 4
  • 14

4 Answers4

2

You issue is with your for loop.

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

Is going to run off the end of the array as sizeof(flightDirections) is grater than the size of the array. sizeof(flightDirections) is sizeof(std::string) * number_of_elements_in_the_array. In order to get the correct size of the array you need to use

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

Or better yet use a ranged based for loop as

int i = 0;
for (const auto & e : flightDirections) {
    cout << ++i << e;
}
NathanOliver
  • 171,901
  • 28
  • 288
  • 402
0

You are using sizeof erroneously. The sizeof returns the number of bytes the entity contains, not the number of entries (for the case of an array). Thus, this is incorrect:

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

You should be simply using:

for (int i = 0; i < sizeof(flightDirections)/sizeof(flightDirections[0]); i++) {
PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45
  • Thank you so much! this was the problem. I'm used to languages that have easier ways to figure out array sizes. – mc-lunar Nov 05 '15 at 19:31
0

sizeof gives the size in bytes, not the number of elements of the array. When you call sizeof(flightDirections) you probably expect 16, but the risult is much more. Therefore your loop goes too far, beyond the indices of your array, and you get undefined behaviour - in this case, a segmentation fault.

Since you have hard-coded the size of the array, you can even hard-code the loop. Or, if you want to have more flexible code, you can calculate the number of elements by dividing the reuslt of sizeof(flightDirections) by the size of a single element, like this:

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

(This trick is common practice in C, when you have to pass an array to a function: since the array would lose its size (when passed it decays to a pointer), you have to pass it explicitly, as a second argument, and this is the normal way of doing it.)

0

If you want to use sizeof() to find the length of an array, you need to divide it by what is held in the array. See this question:

How do I find the length of an array?

Can you use a std::vector instead? That has a size() method.

Community
  • 1
  • 1
Andy M
  • 596
  • 3
  • 7
  • unfortunately this is a class assignment and we are marked down for using programming techniques we haven't been taught yet – mc-lunar Nov 05 '15 at 19:15
  • @mc-lunar Wow. I'd find an other class. – Biffen Nov 05 '15 at 19:15
  • 2
    @mc-lunar *We are marked down for using programming techniques we haven't been taught yet* -- Instead of being praised for doing independent research, you're penalized. Get another teacher. When I was teaching, if a student used something that wasn't taught, I would always have him/her explain what they used. If they give a good enough explanation, that was enough. – PaulMcKenzie Nov 05 '15 at 19:19
  • @PaulMcKenzie I wish I had you as an instructor. Even if I knew a better way I could only use what was taught up to that point in class. – NathanOliver Nov 05 '15 at 19:22
  • @PaulMcKenzie I think it is a way to prevent students to get too much help, or too easily, from others. Maybe from SO members :-) But anyway getting another teacher is probably not an option. – Fabio says Reinstate Monica Nov 05 '15 at 19:22
  • 1
    @FabioTurati That's why I would have the student explain their work. If they indeed did independent research, then they should explain. If they can't explain, then suspicions would be raised. So if you wanted to cheat, you're only going to be embarrassed when it's time to hand in the homework. – PaulMcKenzie Nov 05 '15 at 19:24
  • @PaulMcKenzie sadly this is a mandatory programming course for my degree. my understanding is that after first year it gets better! – mc-lunar Nov 13 '15 at 02:41