Segmentation fault with cout<<endl

For some reason when i put cout<<endl i'm getting segmentation faults in my Parser code. It's really weird some inputs of strings using cout is already faulting. However sometimes printf doesn't fault, but sometimes it will, it seems really unstable.

The court can basically be anywhere in the Parse(string) function, i have a cout at line 254 which faults.

Here is the code:

Parser.cpp
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/* 
 * File:   Parser.cpp
 * Author: Daniel Rigby
 * 
 * Created on December 23, 2014, 10:23 AM
 */

#include "Parser.h"
#include <stdio.h>
#include <iostream>

using namespace std;

Parser::Parser()
{
    data = "";
    parseTree = 0;
    lineCount = 0;
}

Parser::Parser(string newData)
{
    data = newData;
    parseTree = 0;
    lineCount = 0;
}

Parser::~Parser()
{
    data = '\0';
    delete parseTree;
    delete lineCount;
}
void Parser::loadData(string newData)
{
    data = newData;
}
//Parses the local string data and returns as a Parse Tree
//Probably faster to do with linked list, for future implementations
//Also can create a functions or possibly new class to handle repeititve code
//for each check
ParseTree* Parser::parse()
{
    parseTree = new ParseTree();
    lineCount = new ParseTree();
    ParseNode* node, *newNode, *curCountNode, *countNode;
    int parseCount = 0;
    string* curString = new string("");
    //Count Number of lines, number of parse elements
    for(int i = 0; i < data.length(); i++)
    {
        char cur = data.at(i);
        //check for delimiter and start string again if found
        for(int j = 0; j < DELIMITERS_LENGTH; j++)
        {
            if(cur == DELIMITERS[j])
            {
                if(curString->length() > 0)
                    parseCount+= 2;
                else
                    parseCount++;
                *curString = "";
                continue;
            }     
        }
        //check for operator
        for(int j = 0; j < OPERATORS_LENGTH; j++)
        {
            if(cur == OPERATORS[j])
            {
                if(curString->length() > 0)
                    parseCount+= 2;
                else
                    parseCount++;
                *curString = "";
                continue;
            }     
        }
            //check for operator
        for(int j = 0; j < SYMBOLS_LENGTH; j++)
        {
            if(cur == SYMBOLS[j])
            {
                if(curString->length() > 0)
                    parseCount+= 2;
                else
                    parseCount++;
                *curString =  "";
                continue;
            }     
        }
        if(cur == ' ')
        {
            if(node->getSymbol() != '\"')
            {
                if(curString->length() > 0)
                    parseCount+= 2;
                else
                    parseCount++;
                *curString =  "";
                continue;
            }
        }
        //encounter new line just prevent code from appending the newline
        if(cur == '\n')
            continue;
        //continue appending characters to string as no checks were hit
        char appendString[1] = {cur};
        *curString += appendString;
        
    }
    
    //Parse string data into parse tree
    for(int i = 0, count = 1; i < data.length(); i++)
    {
        bool doContinue = false;
        char cur = data.at(i);
        //check for delimiter and start string again if found
        for(int j = 0; j < DELIMITERS_LENGTH; j++)
        {
            if(cur == DELIMITERS[j])
            {
                if(!curString->empty())
                {
                    countNode = new ParseNode(count);
                    newNode = new ParseNode(*curString);
                    parseTree->insertLeft(node, newNode);
                    this->lineCount->insertLeft(curCountNode, countNode);
                    node = newNode;
                    curCountNode = countNode;
                }
                
                countNode = new ParseNode(count);
                newNode = new ParseNode(cur);
                parseTree->insertLeft(node, newNode);
                this->lineCount->insertLeft(curCountNode, countNode);
                node = newNode;
                curCountNode = countNode;
                *curString = "";
                doContinue = true;
                printf("Inserting character: %c\n", cur);
                break;
            }     
        }
        if(doContinue)
        {
            doContinue = false;
            continue;
        }
        //check for operator
        for(int j = 0; j < OPERATORS_LENGTH; j++)
        {
            if(cur == OPERATORS[j])
            {
                if(!curString->empty())
                {
                    countNode = new ParseNode(count);
                    newNode = new ParseNode(*curString);
                    parseTree->insertLeft(node, newNode);
                    this->lineCount->insertLeft(curCountNode, countNode);
                    node = newNode;
                    curCountNode = countNode;
                }
                countNode = new ParseNode(count);
                newNode = new ParseNode(OPERATORS[j]);
                parseTree->insertLeft(node, newNode);
                this->lineCount->insertLeft(curCountNode, countNode);
                node = newNode;
                curCountNode = countNode;
                *curString = "";
                doContinue = true;
                break;
            }     
        }
        if(doContinue)
        {
            doContinue = false;
            continue;
        }
            //check for symbols
        for(int j = 0; j < SYMBOLS_LENGTH; j++)
        {
            if(cur == SYMBOLS[j])
            {
                
                if(!curString->empty())
                {
                    countNode = new ParseNode(count);
                    newNode = new ParseNode(*curString);
                    parseTree->insertLeft(node, newNode);
                    this->lineCount->insertLeft(curCountNode, countNode);
                    node = newNode;
                    curCountNode = countNode;
                }
                countNode = new ParseNode(count);
                newNode = new ParseNode(SYMBOLS[j]);
                parseTree->insertRight(node, newNode);
                this->lineCount->insertRight(curCountNode, countNode);
                node = newNode;
                curCountNode = countNode;
                *curString = "";
                doContinue = true;
                printf("Inserting character: %c\n", cur);
                break;
            }     
        }
        if(doContinue)
        {
            doContinue = false;
            continue;
        }
        if(cur == ' ')
        {
            if(node->getSymbol() == '\"')
            {
                if(curString->length() > 0)
                {
                    countNode = new ParseNode(count);
                    newNode = new ParseNode(*curString);
                    parseTree->insertLeft(node, newNode);
                    this->lineCount->insertLeft(curCountNode, countNode);
                    node = newNode;
                    curCountNode = countNode;
                }
                newNode = new ParseNode(' ');
                countNode = new ParseNode(count);
                parseTree->insertLeft(node, newNode);
                this->lineCount->insertLeft(curCountNode, countNode);
                node = newNode;
                curCountNode = countNode;
                *curString = "";
                doContinue = true;
                break;
            }
        }
        if(doContinue)
        {
            doContinue = false;
            continue;
        }
        //encounter new line just prevent code from appending the newline
        if(cur == '\n')
        {
            count++;
            continue;
        }
        //continue appending characters to string as no checks were hit
        char appendString[1] = {cur};
        printf("Current char: %s\n", appendString);
        *curString += appendString;
        cout<<appendString<<"\n";

    }
    cout<<"WHY SEGMENTATION FAULT"<<endl;
    return parseTree;
}
ParseTree* Parser::parse(string newData)
{
    ParseTree* parseTree = new ParseTree();
    
    return parseTree;
}
Last edited on
Hei Neber

Would you mind posting the original error message? :)

The most common Segmentation fault I get is when dereferencing a NULL pointer...

Segmentation fault is a specific kind of error caused by accessing memory that “does not belong to you.” It’s a helper mechanism that keeps you from corrupting the memory and introducing hard-to-debug memory bugs. Whenever you get a segfault you know you are doing something wrong with memory – accessing variable that has already been freed, writing to a read-only portion of the memory, etc.
The error message was: Segmentation Fault 11. That's all i can remember since now i've fixed the code but don't know what exactly fixed it except having clean code. Even though it's fixed i'd still like to know what caused the fault in the above code though.

I've now moved the repetitous code into a new ParseBlock class and this has cleaned up the code and removed the instability in the program so i can now cout and what not wherever i want in that code.
1
2
3
4
        char appendString[1] = {cur};
        printf("Current char: %s\n", appendString);
        *curString += appendString;
        cout<<appendString<<"\n";
This alone is enought to trigger undefined behavior (which can lead to anything)
You have a c-string without terminating 0.
Both printf and cout statements will output values of consequitive memory cells until it hits 0.
If there is 0 before uninitialized memory, everything is fine. else you have a crash.

Also you have many leaks here.
Topic archived. No new replies allowed.