Program crashes as pointer tries to access struct

Hi, this is my solution to UVa123: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=59
I am not sure exactly why the code crashes at line 104 when I try to access a struct with a pointer that was allocated memory

My Input:
is
::
Descent of Man
END

Output:
did END
Working on title #0
descent of man
titleLength 14
In string, searching for words
Word Beg, Word Length, curCursor: 0, 7, 7
descent
loop: 0
Word detected as not ignored
finished capitalisation
DESCENT
finished replacing
DESCENT of man
created new storeByKeyWord
set head to curr
0x687210
DESCENT

Then it crashes

My Code:

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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include <iostream>
#include <sstream>
#include <string>
#include <stdio.h>
#include <locale>
#include <stdlib.h>


using namespace std;

bool isIgnored(string wordCheck, string ignoredWords[], int ignoreCount) {
    for (int loop=0; loop<ignoreCount; loop++) {
        cout << "loop: " << loop << endl;
        if(wordCheck==ignoredWords[loop]) {
            return true;
        }
    }
    return false;
}

struct storeByKeyWord {
string keyword;
string entireTitle;
storeByKeyWord* next;
};

string word, newCapWord, newCapPhrase;
int ignoreCount=0;
int numTitles=0;
bool spaceBefore;
int titleLength;
bool wordUnique;
int wordBegCur, wordLeng, curCursor;


int main()
{
    storeByKeyWord *head=NULL, *curr=NULL, *tail=NULL, *findPlace=NULL, *findPlaceLag=NULL;
    string* titles=new string[200];
    string* wordsIgnore=new string[50];
    while(1) {
        cin >> word;
        if (word=="::") {
            break;
        }
        wordsIgnore[ignoreCount]=word;
        ignoreCount++;
    }
    while (getline(cin, word, '\n')) {
        if (!cin||word=="END") {
            break;
        }
        if (word=="") continue;
        titles[numTitles]=word;
        titleLength=word.length();
        for (int looptoLower=0; looptoLower<titleLength; looptoLower++) {
            //cout << "looptoLower, char: " << looptoLower << ", " <<
            //word[looptoLower]<< endl;
            word[looptoLower]=char(tolower(word[looptoLower]));
            //cout << "char after" << word[looptoLower] << endl;
        }
        titles[numTitles]=word;
        numTitles++;
    }
    cout << "did END" << endl;
    for (int loopFindKeywords=0; loopFindKeywords<numTitles; loopFindKeywords++) {
        cout << "Working on title #" << loopFindKeywords << endl;
        cout << titles[loopFindKeywords] << endl;
        wordBegCur=0; wordLeng=0; curCursor=0;
        titleLength=(titles[loopFindKeywords]).length();
        newCapPhrase=titles[loopFindKeywords];
        cout << "titleLength " << titleLength << endl;
        while (curCursor<titleLength-1) {
            curCursor=wordBegCur;
            wordLeng=0;
        cout << "In string, searching for words" << endl;
            while ((newCapPhrase[curCursor]!=' ')&&(curCursor<=titleLength-1)) {
                wordLeng++;
                curCursor++;
            }
        cout << "Word Beg, Word Length, curCursor: " << wordBegCur << ", " <<
        wordLeng << ", " << curCursor << endl;
        newCapWord=newCapPhrase.substr(wordBegCur,wordBegCur+wordLeng);
        cout << newCapWord << endl;
        if (!isIgnored(newCapWord, wordsIgnore, ignoreCount)) {
            cout << "Word detected as not ignored" << endl;
            for (int loopCap=0; loopCap<wordLeng; loopCap++) {
                newCapWord[loopCap]=char(toupper(newCapWord[loopCap]));
            }
            cout << "finished capitalisation" << endl;
            cout << newCapWord << endl;
            newCapPhrase.replace(wordBegCur, wordBegCur+wordLeng, newCapWord);
            cout << "finished replacing" << endl;
            cout << newCapPhrase << endl;
            curr=(storeByKeyWord*)malloc(sizeof(storeByKeyWord));
            cout << "created new storeByKeyWord" << endl;
            if (head==NULL) {
                head=curr;
                cout << "set head to curr" << endl;
            }
            cout << curr << endl;
            cout << newCapWord << endl;
            (curr->keyword)=newCapWord;
            cout << "curr->keyword: " << (curr->keyword) << endl;
            (curr->entireTitle)=newCapPhrase;
            cout << "curr->entireTitle: " << (curr->entireTitle) << endl;
            findPlace=head; findPlaceLag=head;
            while(1) {
                if ((findPlace->keyword)>(curr->keyword)) {
                    if (findPlace==head) {
                        (curr->next)=head;
                        (head->next)=NULL;
                        head=curr;
                        break;
                    }
                    else {
                        while (1) {
                            if ((findPlaceLag->next)==findPlace) {
                                (findPlaceLag->next)=curr;
                                (curr->next)=findPlace;
                                break;
                            }
                            findPlaceLag=(findPlaceLag->next);
                        }
                    }
                    break;
                }
                else {
                    if ((findPlace->next)!=NULL) {
                        findPlace=(findPlace->next);
                        continue;
                    }
                    else {
                        (curr->next)=(findPlace->next);
                        (findPlace->next)=curr;
                        break;
                    }
                }
            }
        }
        wordBegCur=curCursor+1;
        }
        break;
    }
    spaceBefore=false;
    while (1) {
        if (spaceBefore) cout << endl;
        cout << (head->entireTitle);
        if (head->next==NULL) {
            break;
        }
        spaceBefore=true;
        head=(head->next);
    }


    return 0;
}

Last edited on
> string* titles=new string[200];
unnecessary dynamic allocation

> if (!cin||word=="END") {
¿where does it say that the input ends with "END"?

> for (int looptoLower=0; looptoLower<titleLength; looptoLower++) {
not a good name for an index, in fact it's quite bad.

> cout << "did END" << endl;
annoying logs messages.
use the debugger to make a step-by-step run

> curr=(storeByKeyWord*)malloc(sizeof(storeByKeyWord));
storeByKeyWord contains std::string, which is not POD.
std::string has a constructor, which is bypassed by malloc(), then undefined behaviour.
so instead of malloc() it should be new, however, ¿why are you rolling your own list? (and a linked list for more injury)

here you are
1
2
std::map< std::string, std::vector<std::string> > dictionary;
dictionary[keyword].push_back(title);

Thanks, I didn't know I could use structs like lists.
I also tried using new, it worked and stopped crashing when I wrote

 
TYPE* pName= (TYPE*) new TYPE[sizeof(TYPE)]();
Last edited on
Topic archived. No new replies allowed.