salvodel
28-01-2008, 23:27
Salve a tutti, continuo ad avere un problema con il comando free quando vado a liberare la memoria di una matrice. Al momento ho riscritto tutto il programma ed ho messo solo la parte dell'allocazione dinamica della memoria per vedere dov'era l'errore. Il programma deve leggere un file di 7 colonne con un numero di righe non noto. Dopo di che lo salva.
Quello che ho scritto io fa esattamente questo ma se alla fine vado a liberare la memoria si blocca.:confused: :muro:
Perché:wtf:
Gtrazie:help:
Il codice è il seguente e senza il free finale con MSVC funziona
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <float.h>
#define nomefile "OUTPUT.DAT"
struct mydata {
double **matrice;
double INFO[10];
double **uscite;
int step;
};
int leggi(struct mydata *dati)
{
FILE *fdati;
int t;
int punti=20;
printf("Numero dipunti da leggere %d\nLettura - ",punti);
if((fdati=fopen(nomefile,"r"))==NULL)
printf("Impossibile aprire il file");
else
{
t=0;
while(fscanf(fdati,"%LE %LE %LE %LE %LE %LE %LE",&dati->matrice[t][0],&dati->matrice[t][1],&dati->matrice[t][2],&dati->matrice[t][3],&dati->matrice[t][4],&dati->matrice[t][5],&dati->matrice[t][6])>0)
{
if(t>=punti)
break;
t++;
}
}
fclose(fdati);
printf("OK\n");
return t;
}
int salva(struct mydata *data)
{
FILE *fsalva;
int i,t,punti;
punti=data->step;
if((fsalva=fopen("STORIA.dat","w"))==NULL)
printf("Impossibile scrivere\n");
else
{
for(t=0;t<punti;t++)
{
for(i=0;i<7;i++)
{
fprintf(fsalva,"%.4LE\t",data->matrice[t][i]);
}
fprintf(fsalva,"\n");
}
}
return 1;
}
int main()
{
int colonne, righe, tempo;
int t,j;
struct mydata insieme;
system("cls");
colonne=7;
righe=30;
insieme.matrice = (double **)malloc(righe*sizeof(double *));
for(t=0;t<righe;t++)
insieme.matrice[t] = (double *)malloc(colonne*sizeof(double *));
tempo=leggi(&insieme);
insieme.step=tempo;
for(t=0;t<tempo;t++)
{
for(j=0;j<3;j++)
printf("M[%2d][%d]=%2.3g\t",t+1, j+1, insieme.matrice[t][j]);
printf("\n");
}
salva(&insieme);
/*for(t=0;t<tempo;t++)
free(insieme.matrice[t]);
free(insieme.matrice);
system("PAUSE");*/
return 1;
}
Quello che ho scritto io fa esattamente questo ma se alla fine vado a liberare la memoria si blocca.:confused: :muro:
Perché:wtf:
Gtrazie:help:
Il codice è il seguente e senza il free finale con MSVC funziona
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <float.h>
#define nomefile "OUTPUT.DAT"
struct mydata {
double **matrice;
double INFO[10];
double **uscite;
int step;
};
int leggi(struct mydata *dati)
{
FILE *fdati;
int t;
int punti=20;
printf("Numero dipunti da leggere %d\nLettura - ",punti);
if((fdati=fopen(nomefile,"r"))==NULL)
printf("Impossibile aprire il file");
else
{
t=0;
while(fscanf(fdati,"%LE %LE %LE %LE %LE %LE %LE",&dati->matrice[t][0],&dati->matrice[t][1],&dati->matrice[t][2],&dati->matrice[t][3],&dati->matrice[t][4],&dati->matrice[t][5],&dati->matrice[t][6])>0)
{
if(t>=punti)
break;
t++;
}
}
fclose(fdati);
printf("OK\n");
return t;
}
int salva(struct mydata *data)
{
FILE *fsalva;
int i,t,punti;
punti=data->step;
if((fsalva=fopen("STORIA.dat","w"))==NULL)
printf("Impossibile scrivere\n");
else
{
for(t=0;t<punti;t++)
{
for(i=0;i<7;i++)
{
fprintf(fsalva,"%.4LE\t",data->matrice[t][i]);
}
fprintf(fsalva,"\n");
}
}
return 1;
}
int main()
{
int colonne, righe, tempo;
int t,j;
struct mydata insieme;
system("cls");
colonne=7;
righe=30;
insieme.matrice = (double **)malloc(righe*sizeof(double *));
for(t=0;t<righe;t++)
insieme.matrice[t] = (double *)malloc(colonne*sizeof(double *));
tempo=leggi(&insieme);
insieme.step=tempo;
for(t=0;t<tempo;t++)
{
for(j=0;j<3;j++)
printf("M[%2d][%d]=%2.3g\t",t+1, j+1, insieme.matrice[t][j]);
printf("\n");
}
salva(&insieme);
/*for(t=0;t<tempo;t++)
free(insieme.matrice[t]);
free(insieme.matrice);
system("PAUSE");*/
return 1;
}