Issues with stack overflow and access violation reading location

Hello, everybody,

I am totally a beginner in C++. I tried to write some codes for my study, but, by the results of debugging, encountered issues with stack overflow and access violation reading location. Here is my code.

With this code: I used some functions from smile.h and smillearn.h. These functions are CreateNetwork (), and staticEM (). My goal is to apply these two functions to repeatedly perform some analyses with multiple samples of data.

My data is a 3001x 90 matrix, stored in a .csv file. These data are from 3 samples. Each sample I want to use is a 3001 x 90 matrix. So, my goal is to read this 3001 x 90 .csv file into an two-way array, and then divided the array into three 3001 x 30 two-way arrays representing three data samples. For each data sample, I perform the analyses using CreateNetwork (), and staticEM ().

At the end, I wanted to save the output from all three applications into output.txt (a output file opened and write into in staticEM()).

I have tested those two functions, they worked well with single data sample. I feel the problem should be with reading and manipulating data and building the loop for replications, which really is beyond my ability.

I will appreciate any helps with this.

Sincerely,
Bo


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

#include "stdafx.h"
#define _SECURE_SCL 0
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include "smile.h"
#include "smilearn.h"


using namespace std;

	void CreateNetwork(void) {
		DSL_network LIN_4T;
		int x1 = LIN_4T.AddNode(DSL_CPT, "x1");
		int x2 = LIN_4T.AddNode(DSL_CPT, "x2");
		.......
		int x30 = LIN_4T.AddNode(DSL_CPT, "x30");

		int A = LIN_4T.AddNode(DSL_CPT, "A");
		int B = LIN_4T.AddNode(DSL_CPT, "B");
		int C = LIN_4T.AddNode(DSL_CPT, "C");
		int D = LIN_4T.AddNode(DSL_CPT, "D");

		LIN_4T.AddArc(A, x1);
		LIN_4T.AddArc(B, x2);
		LIN_4T.AddArc(C, x3);
		.......
		LIN_4T.AddArc(D, x29);
		LIN_4T.AddArc(C, x30);
		LIN_4T.AddArc(D, x30);

		LIN_4T.AddArc(B, A);
		LIN_4T.AddArc(C, B);
		LIN_4T.AddArc(D, C);

		LIN_4T.WriteFile("LIN_4T.xdsl");
	}


	void staticEM() {
		DSL_dataset LT;
		
		if (LT.ReadFile("C:\\Users\\Bo\\Desktop\\C++\\LIN_4.txt") != DSL_OKAY) {
			cout << "Cannot read data file...exiting" << endl;
			exit(1);
		}
		DSL_network LIN_4T;
		if (LIN_4T.ReadFile("LIN_4T.xdsl", DSL_XDSL_FORMAT) != DSL_OKAY) {
			cout << "Cannot read network...exiting." << endl;
			exit(1);
		}
		vector<DSL_datasetMatch> matches;
		string err;
		if (LT.MatchNetwork(LIN_4T, matches, err) != DSL_OKAY) {
			cout << "Cannot match network...exiting." << endl;
			exit(1);
		}

		double loglik;
		DSL_em em;
		if (em.Learn(LT, LIN_4T, matches, &loglik) != DSL_OKAY) {
			cout << "Cannot learn parameters...exiting." << endl;
			exit(1);
		}


		LIN_4T.UpdateBeliefs();

		//ATTRIBUTE CPs
		int A = LIN_4T.FindNode("A");
		DSL_sysCoordinates theCoordinates(*LIN_4T.GetNode(A)->Value());
		theCoordinates.GoFirst();
		double P_A_1 = theCoordinates.UncheckedValue();
		theCoordinates.Next();
		double P_A_0 = theCoordinates.UncheckedValue();

		int B = LIN_4T.FindNode("B");
		theCoordinates.LinkTo(*LIN_4T.GetNode(B)->Value());
		theCoordinates.GoFirst();
		double P_B_1_A_1 = theCoordinates.UncheckedValue();
		.......

		int x30 = LIN_4T.FindNode("x30");
		theCoordinates.LinkTo(*LIN_4T.GetNode(x30)->Value());
		theCoordinates.GoFirst();
		double P_x30_1_C_1_D_1 = theCoordinates.UncheckedValue();
		theCoordinates.Next();
		double P_x30_0_C_1_D_1 = theCoordinates.UncheckedValue();
		theCoordinates.Next();
		double P_x30_1_C_1_D_0 = theCoordinates.UncheckedValue();
		theCoordinates.Next();
		double P_x30_0_C_1_D_0 = theCoordinates.UncheckedValue();
		theCoordinates.Next();
		double P_x30_1_C_0_D_1 = theCoordinates.UncheckedValue();
		theCoordinates.Next();
		double P_x30_0_C_0_D_1 = theCoordinates.UncheckedValue();
		theCoordinates.Next();
		double P_x30_1_C_0_D_0 = theCoordinates.UncheckedValue();
		theCoordinates.Next();
		double P_x30_0_C_0_D_0 = theCoordinates.UncheckedValue();
		theCoordinates.GoLast();
		double loglikelihood = loglik;
		double arr[] = {loglikelihood, P_A_1,P_A_0,
			P_B_1_A_1,P_B_0_A_1,P_B_1_A_0,P_B_0_A_0,
			P_C_1_B_1,P_C_0_B_1,P_C_1_B_0,P_C_0_B_0,
			P_D_1_C_1,P_D_0_C_1,P_D_1_C_0,P_D_0_C_0,
			P_x1_1_A_1,P_x1_0_A_1,P_x1_1_A_0,P_x1_0_A_0,
			P_x2_1_B_1,P_x2_0_B_1,P_x2_1_B_0,P_x2_0_B_0,
			P_x3_1_C_1,P_x3_0_C_1,P_x3_1_C_0,P_x3_0_C_0,
			P_x4_1_D_1,P_x4_0_D_1,P_x4_1_D_0,P_x4_0_D_0,
		        ........ P_x30_1_C_0_D_0,P_x30_0_C_0_D_0 };
                        // arr has 207 elements

		std::vector<double> cp_table(arr, arr + 207);
		ofstream output;
		output.open("C:\\Users\\Bo\\Desktop\\C++\\output.txt", std::ofstream::out|std::ofstream::app);
		for (size_t i = 0; i < cp_table.size(); i++) {
			output << cp_table[i] << endl;
		output.close();
		}
	}



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

		double data[3001][90];
		std::ifstream file("C:\\Users\\Bo\\Desktop\\C++\\data_l.csv");

		for (int row = 0; row < 3001; ++row)
		{
			std::string line;
			std::getline(file, line);
			if (!file.good())
				break;

			std::stringstream iss(line);

			for (int col = 0; col < 90; ++col)
			{
				std::string val;
				std::getline(iss, val, ',');
				if (!iss.good())
					break;
				std::stringstream convertor(val);
				convertor >> data[row][col];
			}
		}
	
		
			for (int i = 0; i< 3001; ++i) {
				for (int k = 0; k < 3; ++k) {
					for (int j = (k - 1) * 30; j < (k - 1) * 30 + 30; ++j) {
					double dat[3001][30];
					dat[i][j] = data[i][j];
					ofstream LIN_4("C:\\Users\\Bo\\Desktop\\C++\\LIN_4.txt");
					LIN_4 << dat[i][j];
					staticEM();
				}
			}
		}
	}

Last edited on
LIne 302 creates a very big array on the stack. The stack is usually relatively small. Try changing this to static double data[3001][90];. That will put it in initialized data where there is more room.
Topic archived. No new replies allowed.