glibc detected - double free or corruption

2019-07-21 21:50发布

问题:

I get the following error messages when I submit the code (pasted below) to an online gcc compiler.

* glibc detected /run-1326102706-2046832693/solution: double free or corruption (!prev): 0x091901a8 ** =======

The code is as follows:

# include <iostream>
# include <string>
# include <list>
# include <cstring>

using namespace std;

int main()
{
    int test_cases, i, score, str_len;
    string str;
    char first_char, current_char;
    list <int> strlist;
    list <int> :: iterator it; 

    cin>>test_cases;

    char *cstr[test_cases]; //Creating an array of cstr pointers (test_cases number of pointers)

    while(test_cases > 0)
    {
        cin>>str;
        first_char = str.at(0);
        str_len = str.length();
        score = str_len;
        strlist.clear();

        cstr[test_cases-1] = new char[str_len];
        strcpy(cstr[test_cases-1],str.c_str()); //copying the input str into cstr. This is done to minimize the complexity of std::string's at function.

        for(i=1;i<str_len; i++)
        {
            current_char = *(cstr[test_cases-1]+i);
            if (current_char == first_char)
            {
                score++; strlist.push_front(1);
                it = strlist.begin();
                if (it != strlist.end())
                    it++;
            }

            while (!strlist.empty() && it != strlist.end())
            {
                if (current_char == *(cstr[test_cases-1] + *(it)))
                {
                    (*it)++;it++;score++;
                }
                else
                    it = strlist.erase(it);
            }
            if (!strlist.empty())
                it = strlist.begin();

        }
        cout<<score<<endl;
        delete(cstr[test_cases-1]);
        test_cases--;

    }

    return 0;
}

As mentioned in the code itself, I initially used std::string, but found that the std::string.at function was quite slow (esepcially since this problem has really large input strings). So I decided to store the string input in a character array, so that direct indexing to a particular position would be possible.

Appreciate any help.

回答1:

There are two problems that I can see:

cstr[test_cases-1] = new char[str_len]; // Not allocating space for terminating NULL.

delete(cstr[test_cases-1]); // Incorrect delete, should be delete[]
                            // As already pointed out by mooware

Change these two lines to:

cstr[test_cases-1] = new char[str_len + 1];

delete[] cstr[test_cases-1];


回答2:

You are using array-new ("new char[str_len]") to allocate the strings, but scalar-delete ("delete(cstr[test_cases-1])") to delete them. You should always match the new- and delete-operators, so when you use array-new, also use array-delete ("delete[] cstr[test_cases-1]").



回答3:

You have two bugs. One is here:

    cstr[test_cases-1] = new char[str_len];
    strcpy(cstr[test_cases-1],str.c_str());

You allocate one byte too few. That should be new char[str_len+1] since strcpy copies the terminator.

The other is here:

    delete(cstr[test_cases-1]);

You cannot allocate with new[] and deallocate with delete. If you allocate with new[], you must deallocate with delete[].



标签: c++ free glibc