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?
#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;
}