程序由于未定义的行为而崩溃

Program crashes due to undefined behavior

本文关键字:崩溃 未定义 程序      更新时间:2023-10-16

所以我正在编写一个程序,为CD集合创建一个库并显示它们。每当我将指向文件中歌曲的指针数组写入包含在数组中的结构中时,我的程序就会编译,但会崩溃,如下所示:

//Get song array
        for (int a = 0; a < 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);
            }

我认为这是由于未定义的行为,这是在.中发生的.cpp文件

 #include <iostream>
#include "CDs.h"
#include "CD.h"
#include  <fstream>
#include <string>
#include <cstring>
using namespace std;
//Creates a collection of CDs
CDs* createCDs(const char* file_name)
{
//Declare variables and 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())
        {   
        for (int i = 0; i < max_cds; i++)
            {
            //Get the artist from .txt file
            cd = cd_array[i];
            getline (infile, line);
            sub = line.c_str();
            word = createString(sub);  //Create string from infile line
            cd->artist = word;
            destroyString(word);

            //Get the Title of the album from file
            getline (infile, line);
            sub = line.c_str();
            word = createString(sub);
            cd->title = word;
            destroyString(word);
            //Get the Year of the album from file
            infile >> num;
            cd->year = num;
            //Get the Rating
            infile >> num;
            cd->rating = num;
            //Get number of tracks
            int num_songs;
            infile >> cd->num_tracks;
            //Get song array
            for (int a = 0; a < 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;
}

我不知道该怎么办,如果有人能帮忙,那将是令人惊叹的。

edit-我没有给出.cpp,它包含了使用的一些函数

    #include <iostream>
#include <cstring>
#include "String.h"
using namespace std;
//Function that creates a string
String* createString(const char* char_array)
{
//Allocate memory for a pointer to String struct
//String* string;
String* string = new String;
//Write the char_array to String struct
int length = strlen(char_array);
char array[30];
for (int i = 0; i <= length; i++)
    {
    array[i] = char_array[i];
    string->array[i] = array[i];
    }
return string;
}
//Function that displays the string
void displayString(String* str)
{
for (int i = 0; i < strlen(str->array); i++)
    {
    cout << str->array[i];
    }
cout << endl;
}
//Function that destroys the string
void destroyString(String* str)
{
delete str;
str = NULL;
}
int find(String* str, char delimiter, int start)
{
for (int i = start; i <= strlen(str->array); i++)
    {
    if (str->array[i] == delimiter) 
        {
        return i;
        }
    }
cout << "No occurences of delimiter were found" << endl;
return -1;
}
String* substr(String* str, int start, int end)
{
String* new_str = new String;
int count = 0;
for (int i = start + 1; i < 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 << " is less than " << str2->array << endl;
    }
if (str1 > str2)
    {
    cout << str2->array <<" is less than " << str1->array << endl;
    }
if (str1 == str2)
    {
    cout << "The strings are equal" << endl;
    }
}

您从不为有效的CD分配内存。您只分配一个指向CD的指针数组(CD_array)。这意味着您有一个指针数组,指向未知的内存位置。

确保不可能出现这种错误访问的最佳方法是不动态分配内存。只需使用CD CD_array[max_cds]并使用它即可。(如果需要将其传递给函数,请使用引用调用。)

您需要添加以下行:

    //Get the artist from .txt file 
           cd_array[i] = new CD; 
           cd = cd_array[i];
           .....

你应该在完成使用后取消分配内存

for(...)
 delete cd_array[i];
delete []cd_array;