简体   繁体   中英

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
  2. This is a school assignment
  3. The only std::class im allowed to use is std::string. No other external c++ libraries allowed (c is allowed though for some reason)
  4. If you do end up reading the code, stringList is supposed to be the strings on a instrument and not a actual string

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.

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

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. I gave majorList and stringList a default value of nullptr in the respective constructors as shown below.

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. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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