segmentation fault

I'm trying to covert an array into a dynamically allocated array and I got a segmentation fault I can't figure out.
gdb says that I have a write error somewhere in my loadsongs function but I can't figure out where.
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <iostream>
#include <cstring>
#include <fstream>
#include <string>
using namespace std;

const  int MAX_CHAR=101;

class   Song
{
public:
        Song();
        Song(const char title[]);
        Song(const char title[], float min);
        ~Song();


        void loadsongs(Song song[], int &i, const int ALBUM_SIZE, char buffr[],int &maxsong, int listsize, char *pch);
        void titleget(char Title[]) const;
        void artistget(char Artist[]) const;
        void albumget(char Album[]) const;
        void titleset(const char Title[]);
        void artistset(const char Artist[]);
        void albumset(const char Album[]);
        void minset(float Min);
        void secset(float Sec);
        float minget() const;
        float secget() const;
private:
        char Title[MAX_CHAR];
        char Artist[MAX_CHAR];
        char Album[MAX_CHAR];
        float Min;
        float Sec;
        void init(const char Title[], const char Artist[], const char Album[], float Min, float Sec);
};

int main()
{

        const int ALBUM_SIZE=100;
        int SONGSIZE=0;
        Song* song=new Song[SONGSIZE];
        List list[ALBUM_SIZE];
        char buffer[MAX_CHAR];
        char *pch;
        int i;
        int cmd;
        int maxsong;
        int listsize;

        char Title[MAX_CHAR];

        i=0;
        maxsong=0;

        (*song).loadsongs(song, i, ALBUM_SIZE, buffer, maxsong, listsize, pch);


        list[i].print(song, i, maxsong, listsize);

          while(cmd!=6)
        {
                displayMenu(cmd);
                 executeCommand(song, list, cmd, i, maxsong, listsize);
                cout<<"command is: "<<cmd<<"i is: "<<i<<endl;
        }

        list[i].savelist(song, listsize, i, list);
        delete [] song;
        return 0;
}


void Song::loadsongs(Song song[], int &i, const int ALBUM_SIZE, char buffer[], int &maxsong, int listsize, char *pch)
{
        char            Title[MAX_CHAR];
        char            Artist[MAX_CHAR];
        char            Album[MAX_CHAR];
        float           Min;
        float           Sec;


        ifstream in;
        in.open("songs.txt");

        if(!in)
        {
        exit(1);
        }

        while( in.getline(buffer, MAX_CHAR) )
        {

                char temp[5][MAX_CHAR];
                int counter;

                counter=0;
                pch=strtok (buffer,";");
                while(pch!=nullptr)
                {
                        strcpy(temp[counter], pch);
                        pch = strtok (nullptr, ";");
                        counter++;
                }

                strcpy(Title, temp[0]);
                strcpy(Artist, temp[1]);
                strcpy(Album, temp[2]);
                Min=atoi(temp[3]);
                Sec=atoi(temp[4]);

                cout<<"Sec is: "<<Sec<<endl;
                cout<<"Min is: "<<Min<<endl;

                song->titleset(Title);
                song->artistset(Artist);
                song->albumset(Album);
                song->minset(Min);
                song->secset(Sec);

                i++;
                maxsong++;
        }

        in.close();
}


Song::Song()
{
        init("no title","No artist","No Album", 0, 0);
}

Song::Song(const char Title[])
{
        init(Title,"No artist","No album", 0, 0);
}

Song::Song(const char Title[], float Min)
{
        init(Title,"No artist", "No album", Min, 0);
}

void Song::init(const char Title[],const char Artist[], const char Album[], float Min, float Sec)
{
        strcpy(this->Title, Title);
        strcpy(this->Artist, Artist);
        strcpy(this->Album, Album);
        this->Min = Min;
        this->Sec=Sec;
}

Song::~Song()
{

}
void Song::titleset(const char Title[])
{
        strcpy(this->Title, Title);
}
void Song::artistset(const char Artist[])
{
        strcpy(this->Artist, Artist);
}
void Song::albumset(const char Album[])
{
        strcpy(this->Album, Album);
}
void Song::minset(float Min)
{
        this->Min = Min;
}
void Song::secset(float Sec)
{
        this->Sec=Sec;
}

int SONGSIZE=0; <--- this should have a reasonable value.

var = new thing[0] <--- silent fubar
var[anything] <------- segfault.
Thank you!! I changed SONGSIZE to 100 and its working now. What is silent fubar? I've never heard that term before.
The problem (fubar) is separated from the crash (segfault). The fubar is silent because although it is the source of the problem, it isn't the location of the crash that you do see (which isn't silent).
gdb says that I have a write error somewhere in my loadsongs function but I can't figure out where.

The debugger gdb should be pointing you to exactly where it detects the problem, the actual problem will be either there or somewhere prior to that point.

If you should the complete message, maybe someone can point you in the correct direction.

I have a feeling it may be related to some of the compiler warnings you are probably getting.

Since you haven't posted the complete program I can only guess.



I don't know if the movie invented the term or not, but anyway, fubar means f***ed up beyond all recognition, at least according to Hollywood.

I was saying the compiler accepts (warns if set to, but accepts and is therefore a 'silent' error) allocating zero memory in a new, but its a bad mistake.
and then it crashes later when you try to use the (none) memory that you (didn't) allocate in the (bad) allocation statement.
Last edited on
Topic archived. No new replies allowed.