Appending a space to a string is causing an error

So, I'm still working on a program that inserts every character in a dictionary into a self-balancing tree. Since I am using a heap, I use spaces for empty spots that do not have a char. To add this space to my string I have a padString function that adds a single space to the tree in question up to an index. The strange thing is that this function used to work perfectly fine. All of a sudden it is giving me random segmentation fault at a certain word when i attempt to add more spaces. I'm doing thousands of these operations before it decides to give me an error. Any thoughts? Help would be appreciated!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using namespace std;
#include <iostream>
#include <string.h>
#include <vector>
#include <fstream>
#include <algorithm>


string lastWord;

int getLeftChildIndex(int index)
{

    return (2*index)+1;
}

int getRightChildIndex(int index)
{
    return (2*index)+2;
}

int getParentIndex(int index)
{
    return (index-1)/2;
}
void padString(string &Tree, int index)
{
Tree += std::string((Tree.size() - index)+1, ' ');
}

unsigned char insertOrderedPoint(string &Tree, char CharToInsert, int index)
{
    //calculate and add needed space to string
    padString(Tree, index);
    unsigned char category = 0;
    if(Tree[index] == ' ')
    {
        Tree[index] = CharToInsert;
    }
    else if (CharToInsert < Tree[index])
    {
        insertOrderedPoint(Tree, CharToInsert, getLeftChildIndex(index));
    }
    else
    {
        insertOrderedPoint(Tree, CharToInsert, getRightChildIndex(index));
    }
    // category = balance(Tree, index);
    return category;
}
    unsigned char insertToTree(string &Tree, char CharToInsert)
    {
        unsigned char category = 0;
        if(Tree.empty())
        {
            Tree += CharToInsert;
        }
        else
        {
            category = insertOrderedPoint(Tree, CharToInsert, 0);
        }
        return category;
    }

//To insert each character in the string one at a time into the heap
    unsigned char BuildTree(string word)
    {
        int category = 0;
        string Tree, charList;
        for(int i = 0; i < word.length(); i++)
        {
            if(charList.find(word[i])==string::npos)
            {
                word[i] = tolower(word[i]);
                charList += word[i];
            }
        }
        if(lastWord==charList)
        {
            return category;
        }
        for(int i = 0; i < charList.length(); i++)
        {
            category = insertToTree(Tree, charList[i]);
        }
        lastWord = charList;
        return category;
    }



int main()
{
    //reading in each word
    ifstream dictionary;
    string word = "abced";
    unsigned char category = BuildTree("abced");
    return 0;
}




Last edited on
Please edit your post to include code tags (highlight your code and click the '<>' button that appears on the right) and include a link to your dictionary?
http://pastebin.com/ , perhaps?

This makes testing significantly easier.
Last edited on
@mbozzi Is that better?
i guess no-one can figure it out?
> #include "TimeFunctions.h"
> balanceNbrCheck = 0;
> dictionary.open("american");
your testcase is incomplete, and so, useless.
There, i fixed it. You shouldn't call it useless though, I never said it was a testcase, but I changed it so it should run by itself now.
Unless I'm missing something, insertOrderedPoint will never return since:

"Since I am using a heap, I use spaces for empty spots that do not have a char. " is not true.

To add this space to my string I have a padString function that adds a single space to the tree in question up to an index.

That may be what you intended padString to do, but that isn't what it does. '\0' is not a space.

oop sorry, yes it needs to have spaces, when it does, it compiles but it prints a bunch of endlines and if you use an entire dictionary it eventually throws a bad allocation error. I will fix the code right now
Thanks for the help, I'm new to asking for help on the interwebz
Topic archived. No new replies allowed.