简体   繁体   English

删除模板化数组时出现分段错误

[英]Segmentation fault when deleting a templated array

-------I forgot to add some stuff so this is a small section for clarity------- -------我忘了添加一些东西,所以为了清楚起见,这是一个小部分-------

  1. I added a hastebin with the full code我添加了一个带有完整代码的hastebin
  2. This is a school assignment这是学校作业
  3. The only std::class im allowed to use is std::string.唯一允许使用的 std::class 是 std::string。 No other external c++ libraries allowed (c is allowed though for some reason)不允许使用其他外部 c++ 库(但出于某种原因允许使用 c)
  4. If you do end up reading the code, stringList is supposed to be the strings on a instrument and not a actual string如果您最终阅读了代码,则 stringList 应该是乐器上的字符串,而不是实际的字符串

I want to make a templated function that quickly reallocated an array that gets passed to it, regardless of type, to a new size.我想制作一个模板化函数,该函数可以快速重新分配传递给它的数组,无论类型如何,都将其设置为新的大小。 I have this code currently:我目前有这个代码:

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;
}

This code works if I don't delete the helper variable, however, if I don't the old memory still exists in the program, which I have verified with memory.如果我不删除 helper 变量,则此代码有效,但是,如果我不删除,旧内存仍然存在于程序中,我已用内存验证过。

If I, however, leave this line in, I get a segmentation fault error from my debugger and this error in memory.但是,如果我保留这一行,我会从我的调试器和内存中得到一个分段错误错误。

    Error #2: UNADDRESSABLE ACCESS: reading 0x0778fdf8-0x0778fdfc 4 byte(s)
# 0 std::__cxx11::basic_string<>::~basic_string               [../../../../../src/gcc-5.1.0/libgcc/libgcc2.c:1169]
# 1 tool_reallocateArray<>                                    [C:/Users/MoustacheSpy/Documents/Programmierung/schulprogramme/Instruments/Instrument.h:67]
# 2 Instrument::setNumberOfMajor                              [C:/Users/MoustacheSpy/Documents/Programmierung/schulprogramme/Instruments/Instrument.cpp:49]
# 3 Instrument::Instrument                                    [C:/Users/MoustacheSpy/Documents/Programmierung/schulprogramme/Instruments/Instrument.cpp:95]
# 4 StringedInstrument::StringedInstrument                    [C:/Users/MoustacheSpy/Documents/Programmierung/schulprogramme/Instruments/StringedInstrument.cpp:61]
# 5 main                                                      [C:/Users/MoustacheSpy/Documents/Programmierung/schulprogramme/Instruments/main.cpp:11]
Note: @0:00:00.562 in thread 9584
Note: instruction: mov    (%ecx) -> %eax

I have no idea what I am doing wrong.我不知道我做错了什么。 The only way I can get this to work is by leaving out the delete.我可以让它工作的唯一方法是省略删除。

----HERE IS MY FULL PROGRAM CODE IF ANYONE IS INTERESTED. ----如果有人感兴趣,这里是我的完整程序代码。 FEEL FREE TO PUT SOME CONSTRUCTIVE CRITISIM BUT REMEMBER THE LIMITATIONS I HAVE, WHICH I POSTED IN THE VERY TOP OF THE POST---- https://hastebin.com/giyucukeya.cpp随意提出一些建设性的批评,但请记住我的局限性,我在帖子的顶部发布了这些内容---- https://hastebin.com/giyucukeya.cpp

Ok forget my other answer.好吧忘记我的另一个答案。 I see where you're coming from now.我知道你现在来自哪里。

I just pulled your code and finally got it running.我刚刚拉了你的代码,终于让它运行了。 The short answer to your problem is that majorList and stringList are never being given a default value so to get your code to work i made the following alterations.对您的问题的简短回答是, majorListstringList从未被赋予默认值,因此为了让您的代码正常工作,我进行了以下更改。 I gave majorList and stringList a default value of nullptr in the respective constructors as shown below.我在各自的构造函数中给 majorList 和 stringList 一个默认值 nullptr ,如下所示。

Even if you are leaving a variable (pointer or not) empty for later use it is always wise to still give that variable a default value usually using the null and nullptr.即使您将变量(指针与否)留空以备后用,仍然通常使用 null 和 nullptr 为该变量提供默认值总是明智的。 Your code is a good example why, Delete has a built-in check for a nullptr however because you never assigned any value to the pointers they were left with random values in them left over from when that chuck of memory was last used so Delete couldn't tell that the pointers were empty and tried to delete a invalid pointer.您的代码是一个很好的例子,为什么 Delete 内置了对 nullptr 的检查,但是因为您从未为它们留下的指针分配任何值't 告诉指针是空的,并试图删除一个无效的指针。

majorList:主要列表:

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);
}

stringList:字符串列表:

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM