Song loop is crashing

My program keeps crashing, ive narrowed it down to a certain loop, but i cant figure out why its not working

heres my loop
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
for(int i = 0; i < num_tracks; i++)
{
	int start = 0;
	char delimiter = ',';
	
	string song_name;
	getline(cdstxt, song_name);
	const char* song= song_name.c_str();
	String* str = createString(song);
	
	int end= find(str, delimiter, start);
	String* length = substr(str, start, end);
	
	start = end + 1;
	end = find(str, delimiter, start);
	String* title = substr(str, start, end);
	
	addSong(cd, title, length);
}


and the other functions im calling
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
String* createString(const char* char_array)
{
   int sz = strlen(char_array);
   char* text = new char[sz+1];
   for (int i = 0; i < sz; i++)
   {
      text[i] = char_array[i];
   }
   text[sz] = 0;  //null terminator

   String* str = new String;
   str->text = text;
   str->sz = sz;
   return str;
}

int find(String* str, char delimiter, int start)
{
   int sz = str->sz;
   const char* text = str->text;
   if (start >= sz || start < 0) return -1;

   int loc = -1;
   for (int i = start; i < sz; i++)
   {
      if (text[i] == delimiter)
      {
         loc = i;
         break;
      }
   }

   return loc;
}

String* substr(String* str, int start, int end)
{
   if (start > end || start < 0) return NULL;

   int sz = str->sz;
   if (start > sz || end > sz) return NULL;

   String* sub = new String;
   const char* text = str->text;
   int sub_len = end - start + 1;
   char* sub_text = new char[sub_len + 1];

   int count = 0;
   for (int i = start; i <= end; i++)
   {
      sub_text[count] = text[i];
      count++;
   }
   sub_text[count] = 0;

   sub->text = sub_text;
   sub->sz = sub_len;
   return sub;
}

void addSong(CD* cd, String* title, String* length)
{
	Song** song_array = new Song*[strlen(title->text)];
	int num_tracks = cd->num_tracks;
	for(int i = 1; i <= num_tracks; i++)
	{
		Song* song = createSong(title, length);
		song_array[i] = song;
	}
	cd->song_array = song_array;
}

Song* createSong(String* title, String* length)
{
	Song* song = new Song;
	song->title = createString(title->text);
	song->length = createString(length->text);
	
	return song;
}


Any help would be greatly appriciated
things like this:
for(int i = 1; i <= num_tracks; i++)

are you making sure that your num_tracks variable isn't bigger than your max allowed i. for example:

song_array[i] = song;

if you're number of tracks is 50 say, but you're song_array length is 10, you're gonna have problems.
Line 63: Song** song_array = new Song*[strlen(title->text)]; // I don't think you want strlen()?

-> Song** song_array = new Song*[cd->num_tracks];

Line 65: An array starts with 0 -> for(int i = 0; i < num_tracks; i++)

Oh and, why do you impersonate std::string (= reinvent the wheel)?
I'm pretty sure you have a massive memory leak problem (and perhaps other problems like pointer copy)
Last edited on
The String file is what my professor gave us, we had to write one ourselves but he gave us one that he did and we are supposed to use it for this assignment.

Im thinking the fact that I have yet another for loop in addSong you be messing it up seeing how im only taking in one song at a time. Ive tried to fox it but it doesnt like that im trying to convert Song* to Song**
any ideas on how to fix that?

Thanks for your help ive made some changes and i know my code is a big mess right now

1
2
3
4
5
6
7
8
9
10
Song** song_array = new Song*[cd->num_tracks];
	int num_tracks = cd->num_tracks;
	Song* song = createSong(title, length);
	song_array = song;
	/*for(int i = 0; i <= num_tracks; i++)
	{
		Song* song = createSong(title, length);
		song_array[i] = song;
	}*/
	cd->song_array = song_array;
Topic archived. No new replies allowed.