Segmentation Fault

Pages: 12
I am virtually done with this program but there is a segmentation fault and I am not quite sure how to fix it. I am sorting 5 beatles albums alphabetically while sorting the songs also. any help would be appreciated! Here's the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

struct album
{
  string albumN,year,songL[30],song[30];
  int c;
};

void sSort(string ar[],string arL[], int elems)
{
  int swaps = 1,j;
  while(swaps)
    {
      swaps = 0;

      for(j = 0; j < elems-1; j++)
        {
          if(arL[j].compare(arL[j+1]) == 1)
            {
              swap(arL[j],arL[j+1]);
              swap(ar[j],ar[j+1]);
              swaps = 1;
            }
        }
    }

}

int main ()
{
    string line,ar[200];
    struct album beatles[4];
    int len,i = 0,x = 0;

    ifstream beatlesData ("beatles.c"); //opening the file.

    if (beatlesData.is_open()) // if the file is open
    {
       while (!beatlesData.eof()) // while the end of file is NOT reached
          {
             for(x = 0; x <= 4; x++)
             {

             getline(beatlesData,line);
             beatles[x].albumN = line;
             //cout << beatles[x].albumN << endl;
             // get album name
             getline(beatlesData,line);
             beatles[x].year = line;
             beatles[x].c = 0;
             // get album year
             getline(beatlesData,line);
             if(beatles[0].c == 0)beatles[0].c++;
             do
               {
                 len = line.length();
                 beatles[x].song[i] = line;
                 beatles[x].songL[i] = beatles[x].song[i].substr(4,len);
                 getline(beatlesData,line);
                 beatles[x].c++;
                 i++;
               } while(line[i] != '=' && !beatlesData.eof());
             // get album songs
             sSort(beatles[x].song, beatles[x].songL, i); //sort songs
             i = 0; //reset i
             }


          }

          int swaps = 1,j;
          while(swaps)
            {
              swaps = 0;

              for(j = 0; j < 4; j++)
               {
                 if(beatles[j].albumN.compare(beatles[j+1].albumN) == 1)
                   {
                     swap(beatles[j],beatles[j+1]);
                     swaps = 1;
                   }
               }
             }


             for(int y = 0; y <= 4; y++)
               {
                 cout << beatles[y].albumN << endl;
                 cout << beatles[y].year << endl;
                 for(int x = 0; x < beatles[y].c; x++)
                   {
                     cout << beatles[y].song[x] << endl;
                   }
                 cout << "==============================" << endl;
               }

        beatlesData.close(); //closing the file
    }

    else cout << "Unable to open file"; //if the file is not open output
    return 0;
}


Here's the output:

Abbey Road
Year:  1969
 8. Because
15. Carry That Weight
 1. Come Together
14. Golden Slumbers
17. Her Majesty
 7. Here Comes the Sun
 6. I Want You (She's So Heavy)
 3. Maxwell's Silver Hammer
11. Mean Mr. Mustard
 5. Octopus's Garden
 4. Oh! Darling
12. Polythene Pam
13. She Came in Through the Bathroom Window
 2. Something
10. Sun King
16. The End
 9. You Never Give Me Your Money
==============================
Hey Jude
Year:  1970
 1. Can't Buy Me Love
 9. Don't Let Me Down
 7. Hey Jude
 2. I Should Have Known Better
 5. Lady Madonna
 8. Old Brown Shoe
 3. Paperback Writer
 4. Rain
 6. Revolution
10. The Ballad of John and Yoko
==============================
Magical Mystery Tour
Year:  1967
11. All You Need Is Love
10. Baby You're a Rich Man
 4. Blue Jay Way
 3. Flying
 7. Hello Goodbye
 6. I Am the Walrus
 1. Magical Mystery Tour
 9. Penny Lane
 8. Strawberry Fields Forever
 2. The Fool on the Hill
 5. Your Mother Should Know
==============================
Sgt. Pepper's Lonely Hearts Club Band
Year:  1967
13. A Day in the Life
 7. Being for the Benefit of Mr. Kite!
 5. Fixing a Hole
 4. Getting Better
11. Good Morning Good Morning
10. Lovely Rita
 3. Lucy in the Sky With Diamonds
 1. Sgt. Pepper's Lonely Hearts Club Band
12. Sgt. Pepper's Lonely Hearts Club Band (Reprise)
 6. She's Leaving Home
 9. When I'm Sixty-Four
 2. With a Little Help from My Friends
 8. Within You Without You
==============================
The Beatles White Album
Year:  1968
 1. Back in the U.S.S.R.
18. Birthday
11. Blackbird
28. Cry Baby Cry
 2. Dear Prudence
14. Don't Pass Me By
21. Everybody's Got Something to Hide Except Me and My Monkey
 3. Glass Onion
30. Good Night
 8. Happiness Is a Warm Gun
23. Helter Skelter
26. Honey Pie
16. I Will
10. I'm So Tired
17. Julia
24. Long, Long, Long
 9. Martha My Dear
20. Mother Nature's Son
 4. Ob-La-Di, Ob-La-Da
12. Piggies
25. Revolution 1
29. Revolution 9
13. Rocky Raccoon
27. Savoy Truffle
22. Sexy Sadie
 6. The Continuing Story of Bungalow Bill
 7. While My Guitar Gently Weeps
15. Why Don't We Do It in the Road?
 5. Wild Honey Pie
19. Yer Blues
Segmentation fault
Last edited on
What's wrong with what he posted?

Could you provide "beatles.c"?
it is giving me a segfault and I can't find where it is I've been looking but maybe I've just been staring at it. anyway yes here it is:


The Beatles White Album
Year:  1968
 1. Back in the U.S.S.R.
 2. Dear Prudence
 3. Glass Onion
 4. Ob-La-Di, Ob-La-Da
 5. Wild Honey Pie
 6. The Continuing Story of Bungalow Bill
 7. While My Guitar Gently Weeps
 8. Happiness Is a Warm Gun
 9. Martha My Dear
10. I'm So Tired
11. Blackbird
12. Piggies
13. Rocky Raccoon
14. Don't Pass Me By
15. Why Don't We Do It in the Road?
16. I Will
17. Julia
18. Birthday
19. Yer Blues
20. Mother Nature's Son
21. Everybody's Got Something to Hide Except Me and My Monkey
22. Sexy Sadie
23. Helter Skelter
24. Long, Long, Long
25. Revolution 1
26. Honey Pie
27. Savoy Truffle
28. Cry Baby Cry
29. Revolution 9
30. Good Night
===============================
Abbey Road
Year:  1969
 1. Come Together
 2. Something
 3. Maxwell's Silver Hammer
 4. Oh! Darling
 5. Octopus's Garden
 6. I Want You (She's So Heavy)
 7. Here Comes the Sun
 8. Because
 9. You Never Give Me Your Money
10. Sun King
11. Mean Mr. Mustard
12. Polythene Pam
13. She Came in Through the Bathroom Window
14. Golden Slumbers
15. Carry That Weight
16. The End
17. Her Majesty
===============================
Magical Mystery Tour
Year:  1967
 1. Magical Mystery Tour
 2. The Fool on the Hill
 3. Flying
 4. Blue Jay Way
 5. Your Mother Should Know
 6. I Am the Walrus
 7. Hello Goodbye
 8. Strawberry Fields Forever
 9. Penny Lane
10. Baby You're a Rich Man
11. All You Need Is Love
===============================
Sgt. Pepper's Lonely Hearts Club Band
Year:  1967
 1. Sgt. Pepper's Lonely Hearts Club Band
 2. With a Little Help from My Friends
 3. Lucy in the Sky With Diamonds
 4. Getting Better
 5. Fixing a Hole
 6. She's Leaving Home
 7. Being for the Benefit of Mr. Kite!
 8. Within You Without You
 9. When I'm Sixty-Four
10. Lovely Rita
11. Good Morning Good Morning
12. Sgt. Pepper's Lonely Hearts Club Band (Reprise)
13. A Day in the Life
===============================
Hey Jude
Year:  1970
 1. Can't Buy Me Love
 2. I Should Have Known Better
 3. Paperback Writer
 4. Rain
 5. Lady Madonna
 6. Revolution
 7. Hey Jude
 8. Old Brown Shoe
 9. Don't Let Me Down
10. The Ballad of John and Yoko
I think it has something to do with lines 44 and 67 but I can't put my finger on it..
My debugger says its on line 102... Checking now.

EDIT 1: First thing I see, on line 92, you have y<=4 with y starting as 0, but that means that 5 elements are accessed, and beatles[] is declared with 4 elements.

After changing that, though, it still has a SEGFAULT error, so I'll check whatever's next.

EDIT 2: I think beatlesData.close() is producing the error, because I cout "DEBUG" just before it, and it works. After pressing ENTER, it produces a SEGFAULT, with close() being the last used function.

EDIT 3: I moved my debugging macro to just before return, and it produced a SEGFAULT. I think something has a faulty destructor.

EDIT 4: Ok, debugger says that the SEGFAULT is in basic_string.h on line 217. My best guess is that you have too much memory allocated, but I'm too tired/bored to dig deeper.
Last edited on
closed account (z05DSL3A)
Line 46 has the same error as Line 92 (as pointed out by PiMaster above).

Line 81-88 have a similar error, if(beatles[j].albumN.compare(beatles[j+1].albumN) == 1) j+1 will take you out of bounds on beatles[]

Line 67, } while(line[i] != '=' && !beatlesData.eof());, looks wrong. I think that line[i] should probably by line[0], as i increases you risk going out of bounds of line

Can't deside if it is a problem (but it should be avoided), the x in line 96 hides the x in line 38.

There may be some other things but generaly it looks like you keep going out of bounds on things.
it was line 58. all I did was delete it and it worked. the rest was correct. thanks anyway
closed account (z05DSL3A)
the rest was correct.
no, it is not correct. It may not have crashed, yet, but it is not correct.
it is right.. i/o is correct. sorting is correct. logic is now correct. I even asked my CS professor who has a PhD in CS. try it. prove us both wrong. then I'll bring it to his attention.
it may not have crashed yet


it either works or it doesn't. no "crash yet". the code either works all of the time or it doesn't work. period.
closed account (z05DSL3A)
If you are happy that the code has no flaws, then fine. If your CS professor, who has a PhD in CS, is happy that it has no flaws then...well I feel sorry for you.

Have fun.
AND the 0 <= 4 iterates through 5 times. 0,1,2,3,4. that's 5 times. count them. that is the basics of an array. so, lines 46 and 92 ARE correct.
We are both happy that it has no flaws.. What's your point? I'm not going to argue with you over the internet. SO, prove it wrong or get off of this thread.
struct album beatles[4];

And beatles is declared with 4 elements, count them, 4. If you haven't changed it, you should. Can't really say what it's overwriting...
Last edited on
an array starts at 0. 0,1,2,3,4. 5. where did you learn arrays?
closed account (z05DSL3A)
If you want to be a prick about it, that is fine.
AND the 0 <= 4 iterates through 5 times. 0,1,2,3,4. that's 5 times. count them. that is the basics of an array. so, lines 46 and 92 ARE correct.

Yes, it is five but your array is four, therefore you are running the risk of indexing your array outside of it bounds.
The array "beatles" of type "album" is being declared with 4 elements. 4. What could possibly lead you to believe that you can access 5 elements?

For any given array, this will go out of the array's bounds:

1
2
type name[len];
name[len]=myvar;


No exception.
no it is not four. it is five. it will be five. cout << beatles[x].albumN << endl; yields five values each of them stored in 0-4 of the array. and you were the prick first. deal with it. it's correct.
just because it says 4 doesn't mean the count is 4. it goes through 5 times.
Pages: 12