Vector String return length Error

Hi First time poster, so please excuse the messy code. I'm a bit stuck.

The error is unclear but suggests Its received a bad pointer from another heap. It references dbgheap.c line 1322 and assertion failure

I have two string vector functions the first is called from the main function, the second is called from the first.

Their purpose is to receive a string of text and numbers in a semi-specific format, which the main body of the code reads from a text file, and delaminates the data as to return the first variable in the string as the variable name and the second as the variable value. Along the way it filters out a lot of the unwanted whitespace and punctuation.

E.g "{ VariableNameA 123 }" would be returned as "VariableNameA" And "123"

The code works perfectly for most of the lines in the text file but fails on one particular line where the first variable is 25 characters long. Basically it works for anything 22 characters or less. There are never more than 4 elements in the vector and each element is never intended to be longer than 25 characters.

It fails trying to return from the second split function to the first split function.

Is there a limit to the size of each vector element? I'm struggling to find a way round this without having to rewrite the whole thing. Any 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
102
vector<string> split(const string &s, char delim) 
{
    vector<string> elems;
    split(s, delim, elems);
    return elems;
}
vector<string> &split(const string &s, char delim, vector<string> &elems) {
    stringstream ss(s);
    string item;
	int countelems = 0;
	int countchar = 0;
	char * chrStr = new char [countchar];
	char * openBracket;
	char * closeBracket;
	char * decimal;
	char * underScore;
	char * hyphen;
	char * tabb;
	bool bracketClosedOnSameLine = false;
	char * speechMark;
	bool firstCharFound = false;
	bool splitCompleted = false;
	char * comment;
	int spacesRemoved = 0;
	openBracket = "{";
	closeBracket = "}";
	speechMark = """";
	decimal = ".";
	underScore = "_";
	hyphen = "-";
	tabb = "\t";
	comment = "/";

    while (getline(ss, item, delim)) {elems.push_back(item);}
	countelems = elems.size();
	for(int i=0;i<countelems;i++)
	{
		spacesRemoved = 0;
		countchar = elems.at(i).length();
		strcpy(chrStr,elems.at(i).c_str());
		for(int j=0;j<countchar;j++)
		{
			if (isspace(chrStr[countchar-1]) || chrStr[countchar-1] == *tabb)
			{
				strcpy(&chrStr[countchar-1], "");  // Don't remove this otherwise it will never stop removing characters. 
				elems.at(i).erase(elems.at(i).length()-1);
				countchar--;
			}
			if (countchar > 1)
			{
				if ((!isspace(chrStr[j]) || chrStr[j] != *tabb) && firstCharFound == false){firstCharFound = true;}

				if (isspace(chrStr[j]) || chrStr[j] == *tabb)
				{
					if (firstCharFound == false)
					{
						elems.at(i).erase(elems.at(i).begin());
						spacesRemoved++;
					}
					else if (firstCharFound == true)
					{
						int noChar2Copy = 0;
						noChar2Copy = countchar-j;
						string tempChrStr3;
						if(chrStr[countchar-1] == *closeBracket)
						{
							bracketClosedOnSameLine = true;
							noChar2Copy --;
						}

						for (int k=0;k<noChar2Copy;k++)
						{
							if(!isspace(chrStr[k+j])  && (!ispunct(chrStr[k+j]) || chrStr[k+j] == *decimal || chrStr[k+j] == *underScore || chrStr[k+j] == *hyphen))
							{
								tempChrStr3 = tempChrStr3 + chrStr[k+j];
							}
						}
						for (int l=j-spacesRemoved; l<countchar-spacesRemoved; l++)
						{
							elems.at(i).erase(elems.at(i).length()-1);
						}
						elems.push_back(tempChrStr3);
						splitCompleted = true;
						if(bracketClosedOnSameLine){elems.push_back("}");}
					}
				}
				else if (chrStr[j] == *openBracket && (countchar-spacesRemoved) >1)
				{
					elems.at(i).erase(elems.at(i).length()-1);
					elems.push_back("{");
				}
				else if (chrStr[j] == *closeBracket && (countchar-spacesRemoved) >1)
				{
					elems.at(i).erase(elems.at(i).length()-1);
					elems.push_back("}");
				}
				if(splitCompleted){j = countchar;}
			}
		}
	}
	return elems;
}
closed account (Dy7SLyTq)
could you post all of your 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
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
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include <cstring>
#include <cstdlib>
#include <cstddef>
using namespace std;

vector<string> split(const string &s, char delim); 
vector<string> &split(const string &s, char delim, vector<string> &elems);

long countFileLines(string fileName);

vector<string> &split(const string &s, char delim, vector<string> &elems) {
    stringstream ss(s);
    string item;
	int countelems = 0;
	int countchar = 0;
	char * chrStr = new char [countchar];
	char * openBracket;
	char * closeBracket;
	char * decimal;
	char * underScore;
	char * hyphen;
	char * tabb;
	bool bracketClosedOnSameLine = false;
	char * speechMark;
	bool firstCharFound = false;
	bool splitCompleted = false;
	char * comment;
	int spacesRemoved = 0;
	openBracket = "{";
	closeBracket = "}";
	speechMark = """";
	decimal = ".";
	underScore = "_";
	hyphen = "-";
	tabb = "\t";
	comment = "/";

    while (getline(ss, item, delim)) {elems.push_back(item);}
	countelems = elems.size();
	for(int i=0;i<countelems;i++)
	{
		spacesRemoved = 0;
		countchar = elems.at(i).length();
		strcpy(chrStr,elems.at(i).c_str());
		for(int j=0;j<countchar;j++)
		{
			if (isspace(chrStr[countchar-1]) || chrStr[countchar-1] == *tabb)
			{
				strcpy(&chrStr[countchar-1], "");  // Don't remove this otherwise it will never stop removing characters. 
				elems.at(i).erase(elems.at(i).length()-1);
				countchar--;
			}
			if (countchar > 1)
			{
				if ((!isspace(chrStr[j]) || chrStr[j] != *tabb) && firstCharFound == false){firstCharFound = true;}

				if (isspace(chrStr[j]) || chrStr[j] == *tabb)
				{
					if (firstCharFound == false)
					{
						elems.at(i).erase(elems.at(i).begin());
						spacesRemoved++;
					}
					else if (firstCharFound == true)
					{
						int noChar2Copy = 0;
						noChar2Copy = countchar-j;
						string tempChrStr3;
						if(chrStr[countchar-1] == *closeBracket)
						{
							bracketClosedOnSameLine = true;
							noChar2Copy --;
						}

						for (int k=0;k<noChar2Copy;k++)
						{
							if(!isspace(chrStr[k+j])  && (!ispunct(chrStr[k+j]) || chrStr[k+j] == *decimal || chrStr[k+j] == *underScore || chrStr[k+j] == *hyphen))
							{
								tempChrStr3 = tempChrStr3 + chrStr[k+j];
							}
						}
						for (int l=j-spacesRemoved; l<countchar-spacesRemoved; l++)
						{
							elems.at(i).erase(elems.at(i).length()-1);
						}
						elems.push_back(tempChrStr3);
						splitCompleted = true;
						if(bracketClosedOnSameLine){elems.push_back("}");}
					}
				}
				else if (chrStr[j] == *openBracket && (countchar-spacesRemoved) >1)
				{
					elems.at(i).erase(elems.at(i).length()-1);
					elems.push_back("{");
				}
				else if (chrStr[j] == *closeBracket && (countchar-spacesRemoved) >1)
				{
					elems.at(i).erase(elems.at(i).length()-1);
					elems.push_back("}");
				}
				if(splitCompleted){j = countchar;}
			}
		}
	}
	return elems;
}

vector<string> split(const string &s, char delim) 
{
    vector<string> elems;
    split(s, delim, elems);
    return elems;
}
long countFileLines(string fileName)
{
	FILE* fp;
    int countLines = 1, i=0;
	const char* fileName1 = fileName.c_str();
    fp = fopen(fileName1, "r");
    if (fp == NULL)
    {  
        countLines = -1;
    }
    else
    {
        while((i=fgetc(fp))!=EOF)
        {
			if (i == '\n'){countLines++;}
        }
        printf("Number of lines: %d\n",countLines);
    }
	fclose(fp);
    return countLines;
}

int main(void)
{
    string fileName = "testFile.txt";
	
	ifstream iFile(fileName);
    string line;
	long noofLines = 0;
	int y =0;
	
	noofLines = countFileLines(fileName);
	if (noofLines < 0){printf("The File: %s not found!\n",fileName);}
    else if (noofLines ==0){printf("The File: %s is Empty!\n",fileName);}
	else
	{
		bool closeCmt = true;
		size_t fndStart;
		size_t fndEnd;
		/* While there is still a line. */
		while(getline(iFile, line)) 
		{
			int length = line.length();
			size_t commentLine = line.rfind("//");
			size_t commentOpen = line.rfind("/*");
			size_t commentClose = line.rfind("*/");
			
			if (commentLine < length)
			{
				fndStart = commentLine;
				fndEnd = length;
			}
			else
			{
				fndStart = length;
				fndEnd = length;
			}
			if(closeCmt == true)
			{
				if (commentOpen < length)
				{
					closeCmt = false;
					fndStart = commentOpen;
				}
				else{fndEnd = length;}
			}
			if(closeCmt == false)
			{
				if(fndStart != commentOpen){fndStart =0;}
				if (commentClose < length)
				{
					closeCmt = true;
					fndEnd = commentClose+2;
				}
				else{fndEnd = length;}
			}
			line.erase(fndStart,fndEnd);
			int Clength =0;
			for(auto m = line.cbegin(); m != line.cend(); m++)
			{
				if(isalnum(*m) || ispunct(*m)){Clength++;}
			}
			if( length > 0 && Clength>0 )
			{
				
				vector<string> x = split(line, ' ');
				for(int i=0;i<x.size();i++)
				{
					y=x.at(i).length();
					for(int j=0;j<y;j++){cout << x.at(i).at(j);}
					cout <<  endl;
				}
			}
		}  
	}
	iFile.close();
	system("PAUSE");
	return 0;
}
Your code has undefined behaviour. You defined an array with no elements.

1
2
	int countchar = 0;
	char * chrStr = new char [countchar];


and when you are trying to write to the memory that was not allocated.

1
2
3
4
5
	for(int i=0;i<countelems;i++)
	{
		spacesRemoved = 0;
		countchar = elems.at(i).length();
		strcpy(chrStr,elems.at(i).c_str());

Ahhh! Thank you that's fixed it.

Not sure why it even worked at all before.
I appreciate the help.
Topic archived. No new replies allowed.