I don't understand how a variable and a matrix are affected in this program

I copy paste part of the 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
int MaxHour = 24,
		Blocks = 10; 

	int ThermalUnits = 40, HydroUnits = 17,
		Imports = 5, Exports = 5,
		PumpingUnits = 4;	
	
	int units = ThermalUnits + HydroUnits + Imports + Exports + PumpingUnits ;

    int numcols =  MaxHour * Blocks * units ;  // Number of matrix's columns
    int numrows =  MaxHour * (2 + units) ;     // Number of matrix's rows

    m = 0 ;
    k = 0 ;
    i = 0 ;

    int t = 0, 
		u = 0,
		g = 0,
		d = 0;
	
	int nnz = numcols ; // Non-zero elements
	
    CoinBigIndex *start = new int[numcols+1];

int* index = new int[nnz];
    double* value = new double[nnz];
    double *collb = new double[numcols];
    double *colub = new double[numcols];
    double *obj = new double[numcols];
    double *rowlb = new double[numrows];
    double *rowub = new double[numrows];

    double *demand = new double[numrows]; // They are entered below in the rowlb and rowub
    double *cost = new double[units];

for (i=0 ; i<(ThermalUnits*MaxHour) ; i++)
	{		
		for (t=0 ; t<Blocks ; t++)
		{
			collb[m] = 0 ;
			colub[m] = Thr_Injection_Offers_MW[i][t] ;
			obj[m] = Thr_Injection_Offers_Price[i][t] ;
			m++;
		}		
	}
	//for (i=0 ; i<(9600) ; i++) 	cout << obj[i] << " ";
		
	m = ThermalUnits * MaxHour * Blocks ;
	for (i=0 ; i<(HydroUnits*MaxHour) ; i++)
	{		
		for (t=0 ; t<Blocks ; t++)
		{
			collb[m] = 0 ;			
			colub[m] = Hdr_Injection_Offers_MW[i][t] ;
			//cout << colub[t] << " ";
			obj[m] = Hdr_Injection_Offers_Price[i][t] ;
			m++;
		}		
	}
	//for (i=0 ; i<(9600+4080) ; i++) 	cout << colub[i] << " ";
	//for (i=0 ; i<(9600+4080) ; i++) 	cout << obj[i] << " ";
		
	m = (ThermalUnits + HydroUnits) * MaxHour * Blocks ;
	for (i=0 ; i<(Imports*MaxHour) ; i++)
	{		
		for (t=0 ; t<Blocks ; t++)
		{
			collb[m] = 0 ;			
			colub[m] = Import_Injection_Offers_MW[i][t] ;
			obj[m] = Import_Injection_Offers_Price[i][t] ;
			m++;
		}		
	}
	//for (i=0 ; i<(9600+4080+1200) ; i++) 	cout << colub[i] << " ";
	//for (i=0 ; i<(9600+4080+1200) ; i++) 	cout << obj[i] << " ";
		
	m = (ThermalUnits + HydroUnits + Imports) * MaxHour * Blocks ;
	for (i=0 ; i<(Exports*MaxHour) ; i++)
	{		
		for (t=0 ; t<Blocks ; t++)
		{
			collb[m] = 0 ;			
			colub[m] = Exports_Load_Declarations_MW[i][t] ;
			obj[m] = Exports_Load_Declarations_Price[i][t] ;
			m++;
		}		
	}
	//for (i=0 ; i<(9600+4080+1200+1200) ; i++) 	cout << colub[i] << " ";
	//for (i=0 ; i<(9600+4080+1200+1200) ; i++) 	cout << obj[i] << " ";

	m = (ThermalUnits + HydroUnits + Imports + Exports) * MaxHour * Blocks ;
	for (i=0 ; i<(PumpingUnits*MaxHour) ; i++)
	{		
		for (t=0 ; t<Blocks ; t++)
		{
			collb[m] = 0 ;			
			colub[m] = Pumping_Load_Declarations_MW[i][t] ;
			obj[m] = Pumping_Load_Declarations_Price[i][t] ;
			m++;
		}		
	}
	//for (i=0 ; i<(9600+4080+1200+1200+960) ; i++) 	cout << colub[i] << " ";
	//for (i=0 ; i<(9600+4080+1200+1200+960) ; i++) 	cout << obj[i] << " ";



 /***********************************************************************
 ***ROW BOUNDS: min = max =  arithmoi ton parastaseon (oxi metavlites)*****
 ***********************************************************************/
		  
 //PBE North
    for(t=0 ; t<MaxHour ; t++)
    {
	    // We need equality: upper/lower bounds are the same
		rowlb[t] = Load_Withdrawal[0][t] - Res_Injection[t] ;
		rowub[t] = rowlb[t] ;		
    }	
	
 //PBE South
	for(t=MaxHour ; t<(2*MaxHour) ; t++)
    {
	    // We need equality: upper/lower bounds are the same
		rowlb[t] = Load_Withdrawal[1][t-MaxHour] - Res_Injection[t-MaxHour] ;
		rowub[t] = rowlb[t] ;		
    }	

 //Thr units' min, max
    for(t=(2*MaxHour) ; t<(MaxHour*(2+ThermalUnits)) ; t++)
    {
		rowlb[t] = Thr_Avail_Pmin[t-2*MaxHour] ;
		rowub[t] = Thr_Avail_Pmax[t-2*MaxHour] ;
    }
	//for (i=0 ; i<(48+960) ; i++)	 cout << rowub[i] << " ";

 //Hdr units' min, max
	for(t=(MaxHour*(2+ThermalUnits)) ; t<(MaxHour*(2+ThermalUnits+HydroUnits)) ; t++)
    {
		rowlb[t] = Hdr_Avail_Pmin[t-(MaxHour*(2+ThermalUnits))] ;
		rowub[t] = Hdr_Avail_Pmax[t-(MaxHour*(2+ThermalUnits))] ;
    }
	//for (i=0 ; i<(48+960+408) ; i++)	 cout << rowub[i] << " ";
	
 //Imports
	m = MaxHour * (2 + ThermalUnits + HydroUnits) ;
	for (i=0 ; i<Imports ; i++)
	{
		for(t=(MaxHour*(2+ThermalUnits+HydroUnits)) ; t<(MaxHour*(2+ThermalUnits+HydroUnits+Imports)) ; t++)
		{
			rowlb[m] = 0 ;
			rowub[m] = InterConnection_Imports_NTC[i][t-(MaxHour*(2+ThermalUnits+HydroUnits))] ;
			m++;
		}
	}
	//for (i=0 ; i<(48+960+408+120) ; i++)	 cout << rowub[i] << " ";

 //Exports
	m = MaxHour * (2 + ThermalUnits + HydroUnits + Imports) ;
	for (i=0 ; i<Exports ; i++)
	{
		for(t=(MaxHour*(2+ThermalUnits+HydroUnits+Imports)) ; t<(MaxHour*(2+ThermalUnits+HydroUnits+Imports+Exports)) ; t++)
		{
			rowlb[m] = 0 ;
			rowub[m] = InterConnection_Exports_NTC[i][t-(MaxHour*(2+ThermalUnits+HydroUnits+Imports))] ;
			m++;
		}
	}
	//for (i=0 ; i<(48+960+408+120+120) ; i++)	 cout << rowub[i] << " ";
	
	//sinolo dld o pinakas tha exei (48+960+408+120+120) grammes. 

	
 //*******MATRIX*******

	cout << "*********MATRIX*********" <<endl <<endl;

  //****PBE equation****
   //Sum Thr
	
	k = 0 ;
    g = 0 ;
	m = 0 ;



for(i=0 ; i<(ThermalUnits) ; i++)
	{   
		for (t=0 ; t<MaxHour ; t++)
		{
			for (u=0 ; u<Blocks ; u++)
			{	
				start[g] = k ;	
				g++;
				k=k+3;

								
			}
		}		
	}	

for (i=0 ; i<240 ; i++)	 cout << start[i] << " ";



This program gets memory error when trying to run the last line.

But if i change nnz to numcols*2 there is no problem, why?


I think that the problem is in the section "row bounds"



The error message is:

Windows has triggeres a breakpoint in das.exe.

This may be due to a corruption of the heap, which indicates a bug in das.exe or any of the DLLs it has loaded.

This may also be due to the user pressing F12 while das.exe has focus.

The output window may have more diagnostic information.
Last edited on
That's pretty confusing. You're really not helped yourself writing the code like that.

And you've clearly not posted the relevant code. There are all sorts of things that you don't have declarations for. And you suspect the error is in the use of nnz, but you don't show where you use it.
The whole code is too big to post it.

But i am quite sure that the problem is in here.

If i delete lines 145-167 the problem disappears....


What do you mean that writing code like that is not helpful for me?
How should i write it?
Last edited on
That may be true, but there's no way anyone else can check that. I, do however, have some suggestions.
1. Declare variables where they're used.
2. Declare constants as const.
3. Use mnemonic names for variables.
4. As it's not obvious that your arrays are indexed within bounds, use std::vector instead and use at() instead of [].
5. Don' call something a matrix if it's not and in general, make your comments meaningful.
Topic archived. No new replies allowed.