struct + dynamic memory allocation + free ()

Hi,
I'm studying the basis of C, C++ and Assembly languages at my university (I have two exams about these subjects, for now) ... and I have a problem ^^.
I wrote a program in C (not so optimized, this isn't our present goal for my professors) that recieves in input an integer (matrix size) and a double pointer to a square matrix and print on screen the co-ordinate of each square sub-matrix from size 2 to n - 1 size (where n is the integer that my function recieves in input) that has inside the biggest sum of its own members and the sum itself.
Then I modified the program so I created a struct in which I dynamically allocated two arrays in which i store the co-ordinate that my function finds. So I can allocate them, I can populate themm I can access their elements but I can't free them (I recieve an error regarding the corruption of the heap).
I can't figure out the problem ... I can dynamically allocate the matrix and free it, why I can't free my two arrays?

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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

typedef struct cont_
{
	int *datax;
	int *datay;
} cont;

int** mtx_a (int num);
int mtx_i (int num, int **mtx);
void mtx_p (int num, int **mtx);
int mtx_c (int num, int **mtx, cont data);
void mtx_d(int num, int **mtx);

cont mtx_sa (int num);
int mtx_ss (int num, int x, int y, cont data);
void mtx_sd (cont data);

int main ()
{
	// (1) Dichiaro le variabili.
	int num = 0;
	int check = 0;
	
	// (2) Chiedo all'utente di inserire il lato della matrice.
	do
	{
		printf("Inserire la lunghezza del lato della matrice quadrata: ");
		scanf("%d", &num);

		if (num <= 0)
			printf("Il numero deve essere maggiore di 0\n");

	} while (num <= 0);

	// (I) Store Allocation
	cont data = mtx_sa (num);

	// (3) Dichiaro dopppio puntatore ed alloco spazio per matrice.
	int **test = mtx_a (num);

	// (4) Inizializzo matrice.
	mtx_i (num, test);

	// (5) Stampo la matrice.
	mtx_p (num, test);

	// (6) Eseguo operazioni su sotto-matrici quadrate.
	mtx_c (num, test, data);

	int i = 0;
	for (i = 2; i < num; i++)
	{
		printf("%d # %d\n", data.datax [i], data.datay [i]);
	}

	// (III) Store 3
	mtx_sd (data);
	
	// (7) Libero lo spazio allocato dinamicamente per la matrice.
	mtx_d (num, test);

	// (9) Metto in pausa l'applicazione per verificare i risultati e ritorno.
	system ("pause");
	return 0;
}

int** mtx_a (int num)
{
	int i  = 0;
	int j = 0;
	
	int **tmp = (int **) calloc (num, sizeof (int *));
	for (i = 0; i < num; i++)
	{
		tmp[i] = (int *) malloc (num * sizeof (int));
	}
	
	return tmp;
}

int mtx_i (int num, int **mtx)
{
	int i = 0;
	int j = 0;
	int val = 0;

	srand ((unsigned) time (NULL));

	for (i = 0; i < num; i++)
	{
		for ( j = 0; j < num; j++)
		{
			mtx [i][j] = val;
			val = (val + j + rand ()) % 9;
		}
	}
	
	return 0;
}

void mtx_p (int num, int **mtx)
{
	int i = 0;
	int j = 0;

	for (i = 0; i < num; i++)
	{
		for (j = 0; j < num; j++)
		{
			printf("%d", mtx [i][j]);
			
			if (j == num - 1)
				printf("\n");
			else
				printf(" ");
		}
	}
	printf("\n");
	
	return;
}

int mtx_c (int num, int **mtx, cont data)
{
	int i = 0;
	int j = 0;
	int z = 0;
	
	int side = num - 1;
	int lap = side * side;
	int sommaT = 0;
	int sommaD = 0;
	int I = 0;
	int J = 0;
	int lato = 2;
	int dis = 1;
	int pos1D = 0;
	int pos2D = 0;

	do
	{	
		for (z = lap; z > 0; z--)
		{
			for (i = I; i <= I + dis; i++)
			{
				for (j = J; j <= J + dis ; j++)
				{
					sommaT = sommaT + mtx [i][j];
				}
			}
			
			if (sommaT > sommaD)
			{
				sommaD = sommaT;
				pos1D = I;
				pos2D = J;
			}

			J++;

			if (J >= side)
			{
				I = I++;
				J = 0;
			}

			sommaT = 0;
		}

		// Store 2
		mtx_ss ((num + 1) - side, pos1D, pos2D, data);

		printf("Lato: %d # Somma sotto-matrice qudarata: %d. Coord. x: %d # y: %d\n\n", ((num + 1) - side), sommaD, pos1D, pos2D);

		side--;
		lap = side * side;
		lato++;
		sommaD = 0;
		I = 0;
		J = 0;
		dis++;
			
	} while (side >= 2);

	return 0;
}

void mtx_d(int num, int **mtx)
{
	int i = 0;

	for (i = 0; i < num; i++)
	{
		free (mtx [i]);
	}
	free (mtx);

	return;
}

cont mtx_sa (int num)
{
	cont data;

	data.datax = (int *) calloc (num + 1, sizeof (int));
	data.datay = (int *) calloc (num + 1, sizeof (int));

	data.datax [0] = 0;
	data.datay [0] = 0;
	data.datax [1] = 0;
	data.datay [1] = 0;
	data.datax [num] = 0;
	data.datay [num] = 0;
	data.datax [num + 1] = 0;
	data.datay [num + 1] = 0;
	
	return data;
}

int mtx_ss (int num, int x, int y, cont data)
{
	data.datax [num] = x;
	data.datay [num] = y;
	
	return 0;
}

void mtx_sd (cont data)
{	
	free (data.datax);
	free (data.datay);

	return;
}
This is a C++ program that uses C constructs.

There's a lot to think about, but to start with:

mtx_sa allocates a 2D array of ints. The line:
cont data = mtx_sa (num);
should read:
int** data = mtx_sa (num);
But mtx_sa returns a "cont" variable, so i think could be correct to write cont data = mtx_sa (num); (I create a variable of cont type then I initialize it with mtx_sa that returns a cont variable).

P.S.
Is this a C++ program? I think I wrote it in C ... could you explain me the concept?

P.S.
Excuse for my English
You're right. I misread it.

There's a lot of procedural code there. I'll think about it some more.
In cont mtx_sa (int num), you allocate data.datax = (int *) calloc (num + 1, sizeof (int));. Then you do data.datax [num + 1] = 0;. You also do the same for datay.

This is an array bounds error and corrupts the heap.
Solved, thanks ... it was so obvious but I couldn't see it ... thanks!
Topic archived. No new replies allowed.