Stack around the variable was corrupted

Hi guys.

I'm working on a program that needs to parse a configuration file and it seems to work when I test printing out the values again, but after printing, I get the error in the title. This is 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
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <map>
#include <vector>

using namespace std;

struct atom {
	string atom_type;
	int atom_number, nbonds, IDa, IDb, IDc, IDd;
	double x1, y1, z1, Xa, Ya, Za, Xb, Yb, Zb, Xc, Yc, Zc, Xd, Yd, Zd;
};

int main(int argc, char* argv[]) {

	if (2 != argc) {
		cerr << "USAGE: bam file.input";
		return 1;
	}

	string configLine; // string buffer for reading in the config file
	bool eOutput, errOutput, mOutput, bOutput, vOutput, nnOutput; // Options for choosing which output files to generate
	bool switchCode, relaxVolume;
	string atomTypeOne, atomTypeTwo, atomFixed, atomToSwitchOne, atomToSwitchTwo; // strings for atoms, could be 2 char arrays
	int atomQuantityOne, atomQuantityTwo, numberSwitches, relaxTime;
	double bondSwitchProbOne, bondSwitchProbTwo, atomSwitchProb, temp, latticeConsts[3];
	string basename; // name of output files
	ifstream configFile(argv[1]);

	string key, value, buffer;
	map<string, string> configMap;
	vector<atom> configAtoms;
	configAtoms.reserve(100);
	while (getline(configFile, buffer)) {
		if ('#' == buffer[0])
			continue;
		else if (!buffer.find('=')) {
			atom* n = new atom;
			configFile >> n->atom_type >> n->atom_number >> n->x1 >> n->y1 >> n->z1 >> n->nbonds >> n->IDa >> n->Xa >> n->Ya >> \
n->Za >> n->IDb >> n->Xb >> n->Yb >> n->Zb >> n->IDc >> n->Xc >> n->Yc >> n->Zc >> n->IDd >> n->Xd >> n->Yd >> n->Zd;
			configAtoms.push_back(*n);
		}
		else {
			key = buffer.substr(0, buffer.find('='));
			value = buffer.erase(0, buffer.find('=') + 1);
			configMap[key] = value;
		}
	}

	istringstream(configMap["output_file_prefix"]) >> basename;
	istringstream(configMap["energy"]) >> boolalpha >> eOutput;
	istringstream(configMap["error"]) >> boolalpha >> errOutput;
	istringstream(configMap["output"]) >> boolalpha >> mOutput;
	istringstream(configMap["bond"]) >> boolalpha >> bOutput;
	istringstream(configMap["vasp"]) >> boolalpha >> vOutput;
	istringstream(configMap["nn"]) >> boolalpha >> nnOutput;
	istringstream(configMap["atom_type_1"]) >> atomTypeOne;
	istringstream(configMap["atom_type_2"]) >> atomTypeTwo;
	istringstream(configMap["atom_number_1"]) >> atomQuantityOne;
	istringstream(configMap["atom_number_2"]) >> atomQuantityTwo;
	istringstream(configMap["bond_switch_prob1"]) >> bondSwitchProbOne;
	istringstream(configMap["bond_switch_prob2"]) >> bondSwitchProbTwo;
	istringstream(configMap["atom_switch_code"]) >> boolalpha >> switchCode;
	istringstream(configMap["atoms_to_switch"]) >> atomToSwitchOne >> atomToSwitchTwo;
	istringstream(configMap["atoms_switch_prob"]) >> atomSwitchProb;
	istringstream(configMap["temperature"]) >> temp;
	istringstream(configMap["number_switches"]) >> numberSwitches;
	istringstream(configMap["lattice_constants"]) >> latticeConsts[1] >> latticeConsts[2] >> latticeConsts[3];
	istringstream(configMap["relax_volume"]) >> boolalpha >> relaxVolume;
	istringstream(configMap["volume_relax_time"]) >> relaxTime;
	istringstream(configMap["atoms_fixed"]) >> atomFixed;


	cout << "basename is " << basename << endl;
	cout << "eOutput is " << eOutput << endl;
	cout << "errOutput is " << errOutput << endl;
	cout << "mOutput is " << mOutput << endl;
	cout << "bOutput is " << bOutput << endl;
	cout << "vOutput is " << vOutput << endl;
	cout << "nnOutput is " << nnOutput << endl;
	cout << "atomTypeOne is " << atomTypeOne << endl;
	cout << "atomTypeTwo is " << atomTypeTwo << endl;
	cout << "atomQuantityOne is " << atomQuantityOne << endl;
	cout << "atomQuantityTwo is " << atomQuantityTwo << endl;
	cout << "bondSwitchProbOne is " << bondSwitchProbOne << endl;
	cout << "bondSwitchProbTwo is " << bondSwitchProbTwo << endl;
	cout << "switchCode is " << switchCode << endl;
	cout << "atomToSwitchOne and Two are " << atomToSwitchOne << " " << atomToSwitchTwo << endl;
	cout << "atomSwitchProb is " << atomSwitchProb << endl;
	cout << "temp is " << temp << endl;
	cout << "numberSwitches is " << numberSwitches << endl;
	cout << "latticeConsts are " << latticeConsts[1] << " " << latticeConsts[2] << " " << latticeConsts[3] << endl;
	cout << "relaxVolume is " << relaxVolume << endl;
	cout << "relaxTime is " << relaxTime << endl;
	cout << "atomFixed is " << atomFixed << endl;

	getchar();

	return 0;
}


After some googling, I found out that I am assigning too much data to a variable or buffer somewhere. This is visible in the output, where some of the numbers are being rounded:

input:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
output_file_prefix= outputfiles
energy=         true
error=          false
output=         true
bond=               true
vasp=               true
nn=             true
atom_type_1=        Si
atom_type_2=        Au
atom_number_1=      1
atom_number_2=      3
bond_switch_prob1=  2.00145
bond_switch_prob2=  97.1
atom_switch_code=       true
atoms_to_switch=        113 202
atoms_switch_prob=  55.69856
temperature=        270.888
number_switches=    402
lattice_constants=  3921.231 1238.456 5454.444
relax_volume=       false
volume_relax_time=  200
atoms_fixed=        Si


output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
basename is outputfiles
eOutput is 1
errOutput is 0
mOutput is 1
bOutput is 1
vOutput is 1
nnOutput is 1
atomTypeOne is Si
atomTypeTwo is Au
atomQuantityOne is 1
atomQuantityTwo is 3
bondSwitchProbOne is 2.00145
bondSwitchProbTwo is 97.1
switchCode is 1
atomToSwitchOne and Two are 113 202
atomSwitchProb is 55.6986
temp is 270.888
numberSwitches is 402
latticeConsts are 3921.23 1238.46 5454.44
relaxVolume is 0
relaxTime is 200
atomFixed is Si


Is this a limitation of istringstream, the >> operator, or the map? Is there a way to allocate space for a bigger buffer?

Thanks.
Last edited on
Array indices start from zero so latticeConsts[0], latticeConsts[1] and latticeConsts[2] are valid but latticeConsts[3] is out of bounds.
Peter87 wrote:
Array indices start from zero so latticeConsts[0], latticeConsts[1] and latticeConsts[2] are valid but latticeConsts[3] is out of bounds.


This was it! I feel silly for having made such an obvious mistake.

Thanks very much.
Last edited on
Topic archived. No new replies allowed.