C++ 删除模板阵列时出现分段错误

C++ 删除模板阵列时出现分段错误,c++,segmentation-fault,C++,Segmentation Fault,-------我忘了添加一些东西,所以为了清晰起见,这是一个小部分------- 我添加了一个带有完整代码的加速箱 这是学校的作业 唯一允许im使用的std::类是std::string。没有允许其他外部C++库(由于某些原因,C允许) 如果您最终阅读了代码,stringList应该是乐器上的字符串,而不是实际的字符串 我想制作一个模板化函数,它可以快速地将传递给它的数组重新分配到一个新的大小,而不管它是什么类型。我目前有以下代码: template<class t> t* too

-------我忘了添加一些东西,所以为了清晰起见,这是一个小部分-------

  • 我添加了一个带有完整代码的加速箱
  • 这是学校的作业
  • 唯一允许im使用的std::类是std::string。没有允许其他外部C++库(由于某些原因,C允许)
  • 如果您最终阅读了代码,stringList应该是乐器上的字符串,而不是实际的字符串

  • 我想制作一个模板化函数,它可以快速地将传递给它的数组重新分配到一个新的大小,而不管它是什么类型。我目前有以下代码:

    template<class t>
    t* tool_reallocateArray(t array[],int &oldSize,int newSize){
        t* helper = array;
        array = new t[newSize];
        if(array!=0){
            int toCopy=0;
    
            if(oldSize>newSize){
                toCopy = newSize;
                //cout<<"new is smaller";
            }else toCopy = oldSize;
    
            for(int i=0;i<toCopy;i++){
                //helper[i];
                array[i] = helper[i];
            }
            delete[] helper; //this is where the problem is
        }
        oldSize = newSize;
    
        return array;
    }
    
    模板
    t*tool_reallocateArray(t数组[],int&oldSize,int newSize){
    t*helper=数组;
    数组=新的t[newSize];
    如果(数组!=0){
    int-toCopy=0;
    如果(旧尺寸>新闻尺寸){
    toCopy=新闻化;
    
    //好吧,忘了我的另一个答案吧。我知道你现在从哪里来了

    我只是拉了拉你的代码,最后让它运行。你的问题的简单答案是,
    majorList
    stringList
    从来没有被赋予默认值,所以为了让你的代码正常工作,我做了以下修改。我在各自的构造函数中给majorList和stringList一个默认值nullptr,如图所示噢

    即使您留下一个变量(指针与否)空供以后使用通常使用null和nullptr为该变量指定默认值总是明智的。您的代码就是一个很好的例子,为什么Delete有一个内置的nullptr检查,但是因为您从未为指针分配任何值,它们中的随机值是上次使用内存时留下的ed因此Delete无法判断指针是否为空,并尝试删除无效指针

    主要名单:

    Instrument::Instrument(const Instrument& other)
    :name(other.name),majorList(nullptr)
    {
        this->setNumberOfMajor(other.numberOfMajor);
        this->setMajorList(other.majorList);
    }
    
    
    Instrument::Instrument(const string& name, int numberOfMajor, string* majorList)
    :name(name),numberOfMajor(0),majorList(nullptr)
    {
        this->setNumberOfMajor(numberOfMajor);
        this->setMajorList(majorList);
    }
    
    字符串列表:

    StringedInstrument::StringedInstrument(string name, int numberOfMajor, string* majorList, int numberOfStrings, char* stringList)
    :Instrument(name,numberOfMajor,majorList),stringList(nullptr)
    {
    
        cout<<"Constructing stringed instrument"<<endl;
        this->setNumberOfStrings(numberOfStrings);
        this->setStringList(stringList);
    }
    
    StringedInstrument::StringedInstrument(const StringedInstrument &other)
    :Instrument(other.getName(),other.getNumberOfMajor(),other.getMajorList()),stringList(nullptr)
    {
        this->setNumberOfStrings(other.numberOfStrings);
        this->setStringList(other.stringList);
    }
    
    StringedInstrument::StringedInstrument(字符串名称、int-numberOfMajor、string*majorList、int-numberOfStrings、char*stringList)
    :仪器(名称、编号、主列表)、弦列表(空PTR)
    {
    coutsetStringList(其他.stringList);
    }
    
    为什么要尝试
    删除[]
    一个既不属于函数内部也不在函数内部创建的数组?您不知道它是如何创建的(例如,如果它是
    new[]
    ed)。您只会
    删除[]
    使用
    new[]创建的数组
    1)使用
    std::vector
    而不是c样式的数组2)意识到你不需要编写这样的函数,那么
    basic\u string
    在这里涉及到了什么?1.不要发布到代码的链接,试着创建一个2.请解释你传递给构造函数的数组是如何“动态分配”的-我没有看到关键字
    new[]
    在这里我读到了hastebin代码-传递给构造函数的数组不是使用
    new[]
    ->创建的,试图
    delete[]
    任何数组都会调用未定义的行为
    StringedInstrument::StringedInstrument(string name, int numberOfMajor, string* majorList, int numberOfStrings, char* stringList)
    :Instrument(name,numberOfMajor,majorList),stringList(nullptr)
    {
    
        cout<<"Constructing stringed instrument"<<endl;
        this->setNumberOfStrings(numberOfStrings);
        this->setStringList(stringList);
    }
    
    StringedInstrument::StringedInstrument(const StringedInstrument &other)
    :Instrument(other.getName(),other.getNumberOfMajor(),other.getMajorList()),stringList(nullptr)
    {
        this->setNumberOfStrings(other.numberOfStrings);
        this->setStringList(other.stringList);
    }