Invalid Allocation Size in VC10

I am a beginner in C++ and I am trying to create a matrix calculator for addition and subtraction, 2Ddynamic array while avoiding to use #include <vector>

I wrote this so far and every time I run it in Visual Studio 2010 I get the following error:

Debug Error!
Program...this one
path\projectfile\thisprogram.exe

Invalid Allocation size: 4294967295 bytes



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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258


#include <iostream>
#include <cctype>

class mathvar
{	private:
		int ROWS_A;
		int COLUMNS_A;
		int i , j;
		int ROWS_B;
		int COLUMNS_B;
		int h, k;
		char choice;
		
		
	public:
		mathvar();
		~mathvar();
		int array_sum();
		int array_rest();
		char menu();	
};

mathvar::mathvar()
{
	int ROWS_A = 0;
	int COLUMNS_A = 0;
	int ROWS_B = 0;
	int COLUMNS_B = 0;
}


using namespace std;

int main()
{
	
		mathvar calculator;
		calculator.menu();
		
		
	return 0;	
}


int mathvar::array_sum()
{
		cout<<"Enter values for Array A"<<endl;
		cout<<"Enter a number for ROWS: ";
		cin>>ROWS_A;
		
		cout<<"Enter a number for COLUMNS: ";
		cin>>COLUMNS_A;
		
		int **dynamicArray_A = new int*[ROWS_A];
		int **dynamicArray_B = new int*[ROWS_B];
		
		for (i = 0; i < ROWS_A; i++)
		{
			dynamicArray_A[i] = new int[COLUMNS_A];
			cout<<"Enter elements for row number "<<i+1<<": "<<endl;
			for (j = 0; j < COLUMNS_A; j++)
			{
				cout<<"Number "<<j+1<<": ";
				cin>>*(dynamicArray_A[i] + j);
			}
		}//end for for dynamic memory alloc for array A
	
		cout<<"Enter values for Array B"<<endl;
		cout<<"Enter a number for ROWS: ";
		cin>>ROWS_B;
		
		cout<<"Enter a number for COLUMNS: ";
		cin>>COLUMNS_B;
		
			for (h = 0; h < ROWS_B; h++)
		{
			dynamicArray_B[h] = new int[COLUMNS_B];
			cout<<"Enter elements for row number "<<h+1<<": "<<endl;
			for (k = 0; k < COLUMNS_B; k++)
			{
				cout<<"Number "<<k+1<<": ";
				cin>>*(dynamicArray_B[h] + k);
			}
		}//end for for dynamic memory alloc for array B
		
		cout<<"Matrix A"<<endl;
		for (i = 0; i < ROWS_A; i++)//prints Array A
		{
			for (j = 0; j < COLUMNS_A; j++)
			{
				cout<<*(dynamicArray_A[i] + j)<<"\t";
				
			}	
			cout<<endl;//forces to next line
		}//end of print array A
		cout<<endl;
		cout<<" + "<<endl;
		cout<<"Matrix B"<<endl;
		
		for (i = 0; i < ROWS_B; i++)//prints Array B
		{
			for (j = 0; j < COLUMNS_B; j++)
			{
				cout<<*(dynamicArray_B[i] + j)<<"\t";
			}	
			cout<<endl;//forces to next line
		}//end of print array B
		
		cout<<" = "<<endl;
		
		for (int count = 0; count < ROWS_B; count++)
		{
			for (int count2 = 0; count2 < COLUMNS_B; count2++)
			{
				cout<<(*(dynamicArray_A[count] + count2)) + (*(dynamicArray_B[count] + count2))<<"\t";
			}
			cout<<endl;
		}
	
	for (int i = 0; i < ROWS_A; i++)
		{
			delete []dynamicArray_A[i];
		}
		delete[]dynamicArray_A;//deletes array A
		cout<<endl;
		
			for (int h = 0; h < ROWS_B; h++)
		{
			delete []dynamicArray_B[h];
		}
		delete[]dynamicArray_B;//deletes array B
		cout<<endl;
	
	
};


int mathvar::array_rest()
{
		cout<<"Enter values for Array A"<<endl;
		cout<<"Enter a number for ROWS: ";
		cin>>ROWS_A;
		
		cout<<"Enter a number for COLUMNS: ";
		cin>>COLUMNS_A;
		
		int **dynamicArray_A = new int*[ROWS_A];
		int **dynamicArray_B = new int*[ROWS_B];
		
		for (i = 0; i < ROWS_A; i++)
		{
			dynamicArray_A[i] = new int[COLUMNS_A];
			cout<<"Enter elements for row number "<<i+1<<": "<<endl;
			for (j = 0; j < COLUMNS_A; j++)
			{
				cout<<"Number "<<j+1<<": ";
				cin>>*(dynamicArray_A[i] + j);
			}
		}//end for for dynamic memory alloc for array A
	
		cout<<"Enter values for Array B"<<endl;
		cout<<"Enter a number for ROWS: ";
		cin>>ROWS_B;
		
		cout<<"Enter a number for COLUMNS: ";
		cin>>COLUMNS_B;
		
			for (h = 0; h < ROWS_B; h++)
		{
			dynamicArray_B[h] = new int[COLUMNS_B];
			cout<<"Enter elements for row number "<<h+1<<": "<<endl;
			for (k = 0; k < COLUMNS_B; k++)
			{
				cout<<"Number "<<k+1<<": ";
				cin>>*(dynamicArray_B[h] + k);
			}
		}//end for for dynamic memory alloc for array B
		
		cout<<"Matrix A"<<endl;
		for (i = 0; i < ROWS_A; i++)//prints Array A
		{
			for (j = 0; j < COLUMNS_A; j++)
			{
				cout<<*(dynamicArray_A[i] + j)<<"\t";
				
			}	
			cout<<endl;//forces to next line
		}//end of print array A
		cout<<endl;
		cout<<" + "<<endl;
		cout<<"Matrix B"<<endl;
		
		for (i = 0; i < ROWS_B; i++)//prints Array B
		{
			for (j = 0; j < COLUMNS_B; j++)
			{
				cout<<*(dynamicArray_B[i] + j)<<"\t";
			}	
			cout<<endl;//forces to next line
		}//end of print array B
		
		cout<<" = "<<endl;
		
		for (int count = 0; count < ROWS_B; count++)
		{
			for (int count2 = 0; count2 < COLUMNS_B; count2++)
			{
				cout<<(*(dynamicArray_A[count] + count2)) - (*(dynamicArray_B[count] + count2))<<"\t";
			}
			cout<<endl;
		}
	
	for (int i = 0; i < ROWS_A; i++)
		{
			delete []dynamicArray_A[i];
		}
		delete[]dynamicArray_A;//deletes array A
		cout<<endl;
		
			for (int h = 0; h < ROWS_B; h++)
		{
			delete []dynamicArray_B[h];
		}
		delete[]dynamicArray_B;//deletes array B
		cout<<endl;	
};

char mathvar::menu()
{
		cout<<"Matrix Calculator"<<endl;
		cout<<"A. Sum"<<endl;
		cout<<"B. Substract"<<endl;
		cin.get(choice);
		cin.ignore();
		
		if (cin.fail())
		{
			cout<<"ERROR...program will now terminate..."<<endl;
			return -1;
		}//end if fail
		
		else
		{
			switch(toupper(choice))
			{
				case 'A':cout<<"You selected sum: "<<endl<<endl;
					array_sum();
					break;
				case 'B':cout<<"You selected Substract: "<<endl<<endl;
					array_rest();
					break;
				default: cout<<"You did not enter a valid option..."<<endl;			
			}//end switch
			
		}//end else
};


Strange enough, this code does compiles, generates an .exe fila and runs smoothly in C-Free 4.0 so I don't know what is the problem with Visual Studio 2010.

I already tried searching the forums but can't find an answer to why my code won't run in Visual Studio 2010...

Invalid Allocation size: 4294967295 bytes


This sounds like you were given a negative value that got cast to unsigned, resulting in that extremely large number.
this is just a suggestion.. look at line 57 in yer code up there ^

int **dynamicArray_B = new int*[ROWS_B];

well it may happen somewhere else but it's possible you're using a variable which is declared but not assigned a value, as a delimiter.. I could be wrong but.. maybe get rid of your constructor:
1
2
3
4
5
6
7
mathvar::mathvar()
{
	int ROWS_A = 0;
	int COLUMNS_A = 0;
	int ROWS_B = 0;
	int COLUMNS_B = 0;
}

and let it use the default constructor, and see if the same problem occurs?

I am just as curious as you are to the problem. Also curious, when you compile this in C-Free and run it, does it actually work the way you want?
25
26
27
28
29
30
31
mathvar::mathvar()
{
	int ROWS_A = 0;
	int COLUMNS_A = 0;
	int ROWS_B = 0;
	int COLUMNS_B = 0;
}
This does not initialize the member variables, it creates new variables inside this constructor function which have no relation to the ones one lines 8-14.

Also, you should not make these names in ALL_CAPS unless they are constant - in your code they can change and are not constant, so having their names in ALL_CAPS is misleading as conventionally an ALL_CAPS variable name means it is constant.
Last edited on
Thank you, I am still getting into it. I used your suggestions and the code finally runs (yes!!).

I'll break the habit of ALL_CAPS for non constants.

Again thank you :-)
Topic archived. No new replies allowed.