c++ - Program crashes due to undefined behavior -


so i'm writing program creates library collection of cds , displays them. program compiles crashes whenever write array of pointers songs file structs contained within array shown here:

//get song array         (int = 0; < num_songs; a++)             {              getline (infile, line);             sub = line.c_str();             word = createstring(sub);               length = substr(word, -1, 5);             title = substr(word, 5, strlen(sub));             cd->song_array[a] = createsong(title,length);             destroystring(word);              } 

i think it's due undefined behavior, here's .cpp file happening in.

 #include <iostream> #include "cds.h" #include "cd.h" #include  <fstream> #include <string> #include <cstring> using namespace std;  //creates collection of cds cds* createcds(const char* file_name) { //declare variables , allocate memory int max_cds = 50; cds* collection = new cds; collection->max_cds = max_cds; cd** cd_array = new cd*[max_cds];  int num; int sentinel = 0; string* word; string line; cd* cd; const char* sub; string* length; string* title;  //open .txt file ifstream infile; infile.open(file_name);    if (infile.is_open())     {     while (infile.good())         {             (int = 0; < max_cds; i++)             {              //get artist .txt file             cd = cd_array[i];             getline (infile, line);             sub = line.c_str();             word = createstring(sub);  //create string infile line             cd->artist = word;             destroystring(word);               //get title of album file             getline (infile, line);             sub = line.c_str();             word = createstring(sub);             cd->title = word;             destroystring(word);              //get year of album file             infile >> num;             cd->year = num;              //get rating             infile >> num;             cd->rating = num;              //get number of tracks             int num_songs;             infile >> cd->num_tracks;              //get song array             (int = 0; < num_songs; a++)                 {                  getline (infile, line);                 sub = line.c_str();                 word = createstring(sub);                   cout << "shit" << endl;                 length = substr(word, -1, 5);                 title = substr(word, 5, strlen(sub));                 cd->song_array[a] = createsong(title,length);                 destroystring(word);                  }              cd_array[i] = cd;                sentinel++;               }          }     } else     {     cout << "file did not open";     }  collection->cd_array = cd_array; collection->num_cds = sentinel; collection->max_cds = max_cds; return collection; } 

i have no idea make run, if amazing.

edit - didn't give .cpp included , has of functions used

    #include <iostream> #include <cstring> #include "string.h" using namespace std;  //function creates string string* createstring(const char* char_array) { //allocate memory pointer string struct //string* string; string* string = new string;  //write char_array string struct int length = strlen(char_array);  char array[30]; (int = 0; <= length; i++)     {     array[i] = char_array[i];     string->array[i] = array[i];     } return string; } //function displays string void displaystring(string* str) { (int = 0; < strlen(str->array); i++)     {     cout << str->array[i];     } cout << endl; }  //function destroys string void destroystring(string* str) { delete str; str = null; }  int find(string* str, char delimiter, int start) { (int = start; <= strlen(str->array); i++)     {     if (str->array[i] == delimiter)          {         return i;         }     } cout << "no occurences of delimiter found" << endl; return -1; }  string* substr(string* str, int start, int end) { string* new_str = new string; int count = 0; (int = start + 1; < end - 1; i++)     {     new_str->array[count] = str->array[i];     count++;     }  return new_str; }  void compare(string* str1, string* str2) { if (str1->array < str2->array)     {     cout << str1->array << " less " << str2->array << endl;     } if (str1 > str2)     {     cout << str2->array <<" less " << str1->array << endl;     } if (str1 == str2)     {     cout << "the strings equal" << endl;     } } 

you never allocate memory effective cd. allocate array of pointers cds (cd_array). means have array of pointers, pointing unkown memory locations.

the best way ensure no such bad access possible, not dynamically allocate memory. use cd cd_array[max_cds] , work that. (use call reference, if need pass function.)


Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -