Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C++ list/vector problem

Options
  • 29-08-2009 1:13pm
    #1
    Closed Accounts Posts: 259 ✭✭


    I'm trying to learn some C++ and a simple program i've written keeps crashing.. i can't see any bug in the code, but i'd like a second opinion on what the problem is.

    it crashes on exit and after a little tracing in debugger, it looks like a null dereference pointer passed to a string copy function part of the call to free()

    so it's a compiler problem or my code isn't written properly?

    i'm using mingw g++ but it also crashes with intel compiler.

    [PHP]
    #include <algorithm>
    #include <iostream>
    #include <string>
    #include <list>

    using namespace std;

    /*
    mutate case of letters in a word and
    store the results in a list of strings

    example for "cat"

    cat
    Cat
    cAt
    CAt
    caT
    CaT
    cAT
    CAT
    */
    void mutate_case(list<string>& wordlist, string word)
    {
    int *index = new int[word.length()];
    int len;
    string str;
    int perms = 1;

    for(size_t i(0);i < word.length();i++) {
    str.push_back(tolower(word));
    index = 0;
    perms *= 2;
    }

    for(int i(0);i < perms;i++) {
    wordlist.push_back(str);

    if(++index[0] > 1) {
    index[0] = 0;

    for(len = 1;++index[len] > 1;len++)
    index[len] = 0;

    for(int j(1);j <= len;j++)
    str[j] = (index[j] == 0) ? word[j] : toupper(word[j]);
    }
    str[0] = (index[0] == 0) ? word[0] : toupper(word[0]);
    }

    delete []index;
    }

    /*
    create all permutations of a word

    example for "cat"

    act
    atc
    cat
    cta
    tac
    tca
    */
    void permutate(list<string>& wordlist, string word)
    {
    sort(word.begin(),word.end());

    do {
    wordlist.push_back(word);
    } while(next_permutation(word.begin(),word.end()));
    }
    int main(int argc, char *argv[])
    {
    list<string> permutations;
    list<string> wordlist;

    if(argc != 2) {
    cout << "\nUsage:perm <WORD>\n";
    return(0);
    }

    mutate_case(wordlist,argv[1]);
    permutate(permutations,argv[1]);

    system("pause");
    return(0);
    }

    [/PHP]

    thanks in advance for any advice.


Comments

  • Closed Accounts Posts: 1,388 ✭✭✭Señor Juárez


    I might be totally off here, but shouldn't it be

    delete index;

    instead of

    delete []index;

    ?


  • Closed Accounts Posts: 259 ✭✭weiss


    delete index;

    instead of

    delete []index;

    no, i think the second way is "proper" way to remove dynamic array.
    found problem in mutate_case()

    [PHP]
    for(len = 1;++index[len] > 1;len++)
    index[len] = 0;
    [/PHP]

    there's no bounds checking so it overwrites memory outside array.
    changed it to

    [PHP]
    for(len = 1;++index[len] > 1 && len < str.length();len++)
    index[len] = 0;
    [/PHP]

    and also:

    [php]int *index = new int[word.length()];[/php]

    [php]int *index = new int[word.length() + 1];[/php]

    which works..but the whole function needs to be written better.


Advertisement