写入对象时运行时引发异常错误

Exception thrown error at runtime when writing to object

本文关键字:异常 错误 运行时 对象      更新时间:2023-10-16

我已经坐在这里几个小时了,试图弄清楚这一点。我无法找到类似的问题(尽管我确定已经完成了)。所以关于我的问题。当我编译这个时,它很好。
我应该补充一点,unsortedList 是 Book*,它是一个结构体。

string tit = tempBook->title;
string act = tempBook->action;
string aut = tempBook->author;
string sub = tempBook->subject;
char* tm = tempBook->time;
unsortedList[unsortedArrayLength].title;
unsortedList[unsortedArrayLength].action;
unsortedList[unsortedArrayLength].author;
unsortedList[unsortedArrayLength].subject;
unsortedList[unsortedArrayLength].time;  

但是,当我编译它时,我收到一个错误:
在分配.exe:0xC0000005:访问冲突写入位置0x00000000中0x5AC6516F (vcruntime140d.dll) 引发异常。

string tit = tempBook->title;
string act = tempBook->action;
string aut = tempBook->author;
string sub = tempBook->subject;
char* tm = tempBook->time;
unsortedList[unsortedArrayLength].title = tit;
unsortedList[unsortedArrayLength].action = act;
unsortedList[unsortedArrayLength].author = aut;
unsortedList[unsortedArrayLength].subject = sub;
unsortedList[unsortedArrayLength].time = tm;  

然后它会弹出窗口memcpy.asm,光标位于此位置:

CopyUpByteLoop:
    mov     al, byte ptr [esi]
    mov     byte ptr [edi], al
    inc     esi
    inc     edi
    dec     ecx
    jne     CopyUpByteLoop  

结构书的定义按要求:

    struct Book
{
    std::string title;
    std::string author;
    std::string subject;
    char* time;
    std::string action;
};

这是完整的功能:

    void DB::insertBook(Book* tempBook)
{
    using namespace std;
    unsortedArrayLength++;
    string tit = tempBook->title;
    string act = tempBook->action;
    string aut = tempBook->author;
    string sub = tempBook->subject;
    char* tm = tempBook->time;
    unsortedList[unsortedArrayLength].title = tit;
    unsortedList[unsortedArrayLength].action = act;
    unsortedList[unsortedArrayLength].author = aut;
    unsortedList[unsortedArrayLength].subject = sub;
    unsortedList[unsortedArrayLength].time = tm;
    system("cls");
    cout << "You have " << unsortedList[unsortedArrayLength].action <<":n" << endl <<
    unsortedList[unsortedArrayLength].title << endl <<
    unsortedList[unsortedArrayLength].author << endl <<
    unsortedList[unsortedArrayLength].subject << endl <<
    "on " << unsortedList[unsortedArrayLength].time << endl << endl;
    printToLog(tempBook);
}

请帮助欧比旺。你是我唯一的希望...

好吧,我想通了。我使数组的实例化更大。看来我正在达到阵列的上限。数组用 2 初始化,虽然这就足够了,但事实并非如此。 :(感谢大家把他们的知识借给我。