I stumbled upon this problem while trying to make a programm that solves linear systems of equasions using Gaussian elimination. I am sorry for this long part of the code but i really need help and i think that this way you can see if i made some mistakes that i have overlooked.
This Program works perfect untill i try to name my b matrix or c matrix "solution" and i have accidentaly discovered this because with every other name everything works just fine. Also if i run the debugger this failuer does not occur even if give the name solution When i give this input for example: Coeff matrix rows:4 Coeff matrix columns:4 Coeff matrix name:Test
Rs. matrix rows:4 Rs. matrix columns:1 Rs. matrix name:Rs
Sol. Matrix rows:4 Sol. Matrix columns:1 Sol. Matrix name:solution
the program crashes. Same thing happens if i give the name solution to my Rs(b) matrix.
It is really strange that this does not happen if i name my first matrix solution. Also i have another case in my code which allocates only one matrix and that matrix can also be named solution.
It is even more strange that when those errors occur, sometimes but only sometimes my programm prints the message Failed to allocate memory for matrix rows before crashing. My input stays allways the same I also tried changing calloc to malloc and so on..
Thanks in advance!
typedef struct matrix_s //This is the structure
{
char *name;
double **element;
long columns;
long rows;
}matrix_t;
matrix_t *matrix_allocate(long columns, long rows, char *name)//This is my function that fails
{
if(rows<=0 || columns<=0)
{
fprintf(stderr,"Number of rows/columns is not valid\n");
return 0;
}
matrix_t *mymatrix=calloc(1,sizeof(matrix_t));
long i;
if(!mymatrix)
{
fprintf(stderr,"Failed to allocate memory for matrix\n");
return 0;
}
mymatrix->name=calloc(strlen(name),sizeof(char));
if(!mymatrix->name)
{
fprintf(stderr,"Failed to allocate memory for the name of matrix\n");
free(mymatrix);
mymatrix=0;
return 0;
}
strcpy(mymatrix->name,name);
mymatrix->element=calloc(rows,sizeof(double *));
if(!mymatrix->element)
{
fprintf(stderr,"Failed to allocate memory for matrix rows\n");
free(mymatrix->name);
mymatrix->name=0;
free(mymatrix);
mymatrix=0;
return 0;
}
for(i=0;i<rows;i++)
{
mymatrix->element[i]=calloc(columns,sizeof(double));
if(!mymatrix->element[0])
{
fprintf(stderr,"Failed to allocate columns for the first row\n");
free(mymatrix->name);
mymatrix->name=0;
free(mymatrix->element);
mymatrix->element=0;
free(mymatrix);
mymatrix=0;
return 0;
}
if(!mymatrix->element[i]) {
fprintf(stderr,"Failed to allocate columns for the row w number:%ld\n",i);
for(i=i-1;i>=0;i--)
{
free(mymatrix->element[i]);
mymatrix->element[i]=0;
}
free(mymatrix->name);
mymatrix->name=0;
free(mymatrix->element);
mymatrix->element=0;
free(mymatrix);
mymatrix=0;
return 0;
}
}
mymatrix->columns=columns;
mymatrix->rows=rows;
return mymatrix;
}
int main()//Short version of the main programm
{
matrix_t *a=0, *b=0, *c=0;
char matrixname[50];
long rows,cols;
...
case 5:
clearbuff();//while(getchar()!='\n')
printf("Coeff. matrix rows:");
scanf("%ld",&row);
clearbuff();
printf("Coeff. matrix columns:");
scanf("%ld",&cols);
clearbuff();
printf("Coeff. matrix name:");
fgets(matrixname,49,stdin);
*strrchr(matrixname,'\n')=0;
a=matrix_allocate(cols,row,matrixname);
clearbuff();
printf("Rs. matrix rows:");
scanf("%ld",&row);
clearbuff();
printf("Rs. matrix columns:");
scanf("%ld",&cols);
clearbuff();
printf("Rs. matrix name:");
fgets(matrixname,49,stdin);
*strrchr(matrixname,'\n')=0;
b=matrix_allocate(cols,row,matrixname);
clearbuff();
printf("Sol. matrix rows:");
scanf("%ld",&row);
clearbuff();
printf("Sol. matrix columns:");
scanf("%ld",&cols);
clearbuff();
printf("Sol. matrix name:");
fgets(matrixname,49,stdin);
*strrchr(matrixname,'\n')=0;
c=matrix_allocate(cols,row,matrixname);
clearbuff():
....
....
break;
}