将项pushing_back到矢量中包含的矢量时未处理的异常

Unhandled exception when pushing_back an item to a vector contained in a vector

本文关键字:pushing 未处理 异常 包含 将项 back      更新时间:2023-10-16

当我尝试迭代包含另一个向量的向量时,我发现一些困难。我有一个头文件,它定义了这样的结构: typedef struct Set{ 查尔·瓦纳姆[32]; 无效 * 值; } 设置;

typedef struct Setup {
    float instant;
    std::vector<Set *> SetVar;
} Setup;
std::vector<Setup *> TestCase;

在 cpp 中,我有一个函数,它只是读取一个 xml 文件并将 readen 值解析为 TestCase 向量,如下所示:

while(SetupHandle.ToElement()){
    Setup * newSetup = (Setup *)malloc(sizeof(Setup));
    str = SetupHandle.ToElement()->Attribute("instant");
    newSetup->instant = atof(str);
    TiXmlHandle SetHandle = SetupHandle.FirstChild("Set");
    i=0;
    while(SetHandle.ToElement()){
        Set * newSet = (Set *)malloc(sizeof(Set));
        str = SetHandle.ToElement()->Attribute("Variable");
        strcpy(newSet->varname, str);
        const char * newstr = SetHandle.ToElement()->Attribute("Value");
        newSet->value = (void *) newstr;
        newSetup->SetVar.push_back(newSet);
        i++;
        SetHandle = SetupHandle.Child("Set", i);
    }
    TestCase.push_back(newSetup);
    index++;
    SetupHandle = TC_top.Child("Setup", index);
}

该库可以编译,但是当我使用它时,程序在显示"newSetup->SetVar.push_back(newSet);"的行中崩溃。它抛出一个未经处理的异常,指出:"ISORC2014_CaseStudy_d.exe:0xC0000005:访问冲突读取位置0xcdcdcdc1中0x0430d7ba (tcmodule_psttm_isorc2014_d.pyd) 处未处理的异常。

有人知道我做错了什么吗?

您似乎试图通过使用malloc()来智胜系统。请放心,系统会回复您的!如果你不使用new这些物体就不会自动构建,你会遭受各种悲伤。在C++,基本上没有malloc()的位置!C 内存函数有时用作内存分配工具的一部分,也可能用作连接遗留代码时,但在任何情况下都不足以malloc() C++对象。