Debugging windoze error dialogue box

I am in the process of adding another function (getY()) to a previously working program (#02).
The ultimate purpose of this function is to select values from the vector chromosome and add them to an array (Y), but it currently just adds a dummy value.
I have produced a dummy program (getY.cpp) which processed the array with these dummy values with no problem, and I have updated #02 accordingly to produce #03.
I have listed the code for getY.cpp and #03.cpp at the end of this post.

The changes I made to #02 to produce the listed program (#03) are:
in 'class definition':
update parameters in declaration of init_PM()
add declaration of getY()
in 'function definitions':
add definition for getY()
in 'init function':
amend parameters in heading
add call to getY(), with associated cout<< instruction
in main():
amend parameters in init_PM() declaration
add declaration of array Y
amend parameters in call of init_PM()
block to print out Y[] once the rest of the program has completed

It compiles with no errors and no warnings.

When run when m is 4 or less, the program completes perfectly.
When m=5, the program completes (including the final printout of Y[]), but then I get a windoze "#03.exe has stopped working -> close the program" dialogue box.
When m>5, the program completes the first 5 calls of init_PM properly and then produces this dialogue box during the line chromosome.resize(chr, vector<int> (2)) - after cout<<"before ..."; but before cout<<"...and after";.
This seems totally unaffected by any other variable, but m is not involved in the resize line. However, it is the index used during the original definition of the array, which is the bit I am adding. So there is some logic in the idea that the problem lies within the array bit, but then I am left wondering why that bit worked perfectly (with very high values of m>1000) in getY.cpp.

My problem is that I have no idea of where to go from here, or how to debug a windoze error dialogue box.

I am therefore seeking any advice on the next steps I should take to debug my program.

Thanks

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
//#03.cpp
#include <iostream>
#include <vector>
using namespace std;

/////////////////////// class definition

class indiv {

vector<vector<int> > chromosome;	// declares 2D vector
char sex;

public:
	indiv(){};	//constructor
	~indiv(){};	//destructor
	
	//actions
	int init_PM(indiv &PM, int m, int chr, int z, int Y[], int x);
	void expand(int chr);			// expands the vector to chr * 2 cells
	int set_values(int chr, int z);	// fills vector cells with consecutive numbers
	void print_out(int chr);		// prints out contents of vector to check it has done what is expected
	void set_male();
	int getY(int Y[], int m, int x, int z);
};

///////////////// function definitions

int indiv::set_values(int chr, int z) {	// enters incremented values to cells of vector chromosome; chr & z are passed from main()
{
	cout<<"before ...";
chromosome.resize(chr, vector<int> (2));	// give vector chr rows and 2 columns
	cout<<"and after ...\n";
}
for (int x=0; x<chr; x++) {				// for rows 0 to chr-1 [x]
		for (int y=0; y<2; y++) {		// for columns 0 to 1 [y]
		chromosome.at(x).at(y) =z;		// put value z in chromosome[x][y]
		z++;}}							// increment value of z
		return z;						// return next value to be used to main()
}

int indiv::getY(int Y[], int m, int x, int z)

	{
	Y[x]=x+z;
	z++;
	cout<<"Leaving init_PM. z="<<z<<endl;
	return z;
	}	// end of init_PM


void indiv::print_out(int chr) {
for (int x=0; x<chr; x++) {
	cout<<x<<" ";
	for (int y=0; y<2; y++) {
		cout<<chromosome[x][y]<<" ";}	// end of y loop
		cout<<endl;} //end of x loop
	cout<<"sex ="<<sex<<endl<<endl;
}

void indiv::set_male() {sex='M';}

////////////////// init function

int indiv::init_PM(indiv &PM, int m, int chr, int z, int Y[], int x)
{
cout<<"In function init_PM\n";

cout<<"calling set_values ...\n";
z=PM.set_values(chr, z);		// enter incremented values to cells of vector chromosome; send number of rows (chr) & start value (z); return next start value

cout<<"calling set_male ...\n";
PM.set_male();

cout<<"calling getY ...\n";
PM.getY(Y, m, x, z);

cout<<"calling print_out ...\n";
PM.print_out(chr);				// print out content of vector to check all the above has worked

	return z;

}

//////////////////////////		main() starts here

int main() {
// declarations	
int init_PM(indiv PM, int m, int chr, int z);
int x;	// counter in loops
int chr; // number of pairs of chromosomes
int m; // number of parental males	
int z=0; // chromosome counter
int Y[m];

// get initial parameters
cout<<"How many pairs of chromosomes does this species have?\n";
cin>>chr;
cout<<"Enter number of parental males\n";
cin>>m;

// set up initial conditions ... first the parental males
vector<indiv> PM(m);			// create m instances of class indiv and call it PM (vector chromosome is empty at this point)
for(x=0; x<m; x++){
	z=PM[x].init_PM(PM[x], m, chr, z, Y, x);
	cout<<"returned from init_PM("<<x<<"): "<<z<<endl<<endl;
	}

cout<<endl<<endl<<"z = "<<z<<endl;
for (x=0; x<m; x++)
{
	cout<<x<<"  "<<Y[x]<<endl;
}

}	// end of main() 


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
// getY.cpp
#include <iostream>
using namespace std;

////////////////////////////

int getY(int Y[], int m, int x, int z)

{
Y[x]=x+z;
z++;
cout<<"Leaving init_PM. z="<<z<<endl;
return z;
}	// end of getY()

/////////////////////////////////////

int init_PM(int Y[], int m, int x, int z)

{
z=getY(Y, m, x, z);
return z;	
}
/////////////////////////////////////


int main()
{
//	declarations
int init_PM(int Y[], int m, int x, int z);

int m=1200;
int Y[m];
int x;
int z=0;

// initialise Y
for(x=0; x<m; x++)
{
	Y[x]=0;
}
for(x=0; x<m; x++)
{
	cout<<x<<"  "<<Y[x]<<endl;
}
cout<<endl;	

// actions
for(x=0; x<m; x++)
{
	cout<<"Loop "<<x<<", z="<<z<<endl;
z=init_PM(Y, m, x, z);
} // end of x loop

// print Y
for(x=0; x<m; x++)
{
	cout<<x<<"  "<<Y[x]<<endl;
}
cout<<endl;	

} // end main() 
In #03.cpp: Line 91 together with line 93 doesn't make sense. At that point m contains an arbitrary value. Then a variable length array is created from this value. Which is certainly wrong. The compiler should at least warn about this.

So either you know in advance the size of the array or you use a dynamic array (preferable std::vector).
in getY.cpp:

line 30:
1
2
3
4
int main()
{
//	declarations
int init_PM(int Y[], int m, int x, int z);

sure?
Thanks for the reply.

m is input at line 99, so if I were to move the declaration to somewhere between that and line 103 ...

yes, a quick test putting it immediately before the vector declaration in line 102 seems to be producing the sort of result I am after.

Now it has been pointed out, it's obvious and I too would have thought the compiler would have flagged it, but dev c++ didn't ...

Thanks.
sorry, ar2077, missed your reply while dealing with coder777's.

getY.cpp works

my reasoning behind that line:
* it's an int, because z (an int) is returned by init_PM()
* Y[], x and z are required parameters by getY(), which is called by init_PM()
* m is the size of Y[], which all the tutorials tell me is required to be included when you pass an array to a function

i wrote init_PM() and getY() as nested arrays because that is the way they are written in #03.cpp and I wanted to make sure there were no problems with passing things through the chain.

other than that, i'm not sure what you might be getting at ...
Topic archived. No new replies allowed.