Hi,
I have a little programm that uses an array of pointers to a structure. Everything works fine until I free up the memory.
Here is the sample code:
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#define MAX_SMS_LINES 15
struct SMSInfoType {
char SMSNumber[5];
char TelNumber[20];
char Time[20];
char lines[MAX_SMS_LINES][255];
};
void main()
{
int i, nrows;
struct SMSInfoType **arraySMS;
nrows=5;
arraySMS=malloc(nrows*sizeof(struct SMSInfoType *));
if(arraySMS==NULL){
printf("Memory allocation error\n");
exit(-1);
}
for(i=0;i<nrows;i++){
arraySMS[i]=malloc(sizeof(struct SMSInfoType));
if(arraySMS[i]==NULL){
printf("Memory allocation error\n");
exit(-1);
}
strcpy((*arraySMS)[i].SMSNumber,"3");
strcpy((*arraySMS)[i].TelNumber,"1234567890123456789");
strcpy((*arraySMS)[i].Time,"14:30:00");
}
for(i=0;i<nrows;i++){
printf("SMSNumber[%d]: %s TelNumber[%d]: %s Time[%d]: %s\n",i,(*arraySMS)[i].SMSNumber,i,(*arraySMS)[i].TelNumber,i,(*arraySMS)[i].Time);
}
for(i=0;i<nrows;i++){
printf("Free arraySMS[%d]\n",i);
/***************** and here it crashes !!! ***********/
free(arraySMS[i]);
}
printf("Free arraySMS\n");
free(arraySMS);
printf("Everything freed\n");
printf("end\n");
}
Has anyone an idea, what is wrong in this code and why I get an error when trying to free up the memory?
Thanks a lot