Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

C++ list/vector problem

  • 29-08-2009 01: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