2

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.

Hari
  • 5,057
  • 9
  • 41
  • 51
  • 1
    Did you try running valgrind? – PlasmaHH Jan 09 '12 at 10:12
  • 1
    The `c_str` method of `string` can be used to get the `c-style` char array out of it. There is no need to write your own string class just for this. – Naveen Jan 09 '12 at 10:12
  • With what input are you getting a crash? – David Schwartz Jan 09 '12 at 10:14
  • Since this is a submission to a topcoder type online compiler - I do not have any control over the compiler or the inputs. I could try running it on my local machine though. – Hari Jan 09 '12 at 10:16
  • 2
    Possible duplicate of [How to track down a "double free or corruption" error](https://stackoverflow.com/questions/2902064/how-to-track-down-a-double-free-or-corruption-error) – Raedwald Dec 06 '18 at 13:46

3 Answers3

3

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];
hmjd
  • 120,187
  • 20
  • 207
  • 252
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]").

mooware
  • 1,722
  • 2
  • 16
  • 25
1

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

David Schwartz
  • 179,497
  • 17
  • 214
  • 278