C++ 程序因未定义的行为而崩溃

C++ 程序因未定义的行为而崩溃,c++,C++,所以我正在写一个程序,为一组CD创建一个库并显示它们。当我将指向歌曲的指针数组从文件写入数组中的结构时,我的程序会编译,但会崩溃,如图所示: //Get song array for (int a = 0; a < num_songs; a++) { getline (infile, line); sub = line.c_str(); word = createString(

所以我正在写一个程序,为一组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;
}
我不知道该怎么做才能让这次跑步成功,如果有人能帮上忙,那将是令人惊讶的

编辑-我没有给出包含的.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分配内存。您只需将一个指针数组分配给CDs cd_数组。这意味着您有一个指向未知内存位置的指针数组


确保不会发生此类错误访问的最佳方法是不动态分配内存。只需使用CD_阵列[max_CD]即可。如果需要将其传递给函数,请使用引用调用。

您需要添加以下行:

一旦使用完毕,就应该取消分配内存

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

delete []cd_array;

你考虑过调试它吗?我不知道怎么做。我对编程非常陌生,我该怎么做呢?我使用记事本+,所以没有内置的调试器。你应该从重构代码开始:用std::vector替换CD**CD_数组,用std::String替换字符串,不需要时不要使用字符串`std::String`的指针。@user3330884 notepad++?认真地所有平台上都有免费的IDE。我的建议是不要使用未初始化的变量。你写了很多歌曲;/*某物*/;然后你用num_歌曲。除非您重载>>运算符以神奇地更改该变量,否则此操作将崩溃,内存泄漏随之发生。@nwp,如果未删除分配的对象,内存将泄漏。但这与OP的问题无关,尽管给他一个提示是个好主意。我是新的和删除的邪恶势力之一,建议使用向量、数组或唯一的ptr。顺便说一下,您的第一个代码中有些地方看起来不正确。cd是一种类型还是一个变量?@nwp,我认为每个人在学习基础知识时,首先应该围绕着诸如内存管理之类的讨厌的事情来玩,而不是跳转到STL。我的代码中有一个输入错误,谢谢你指出。他还必须让max_CD常量
for(...)
 delete cd_array[i];

delete []cd_array;