Here is your code with indentation and some debug print statements added.
Code: Select all
#define DEBUG 1
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
/*
* DEFINES
*/
#define MAXN 1001
#define FALSE 0
#define TRUE 1
#define INF 2147483647
#define sFinalMatrix(a,b) resultMatrix[a+18279*b]
#define bool char
/*
* GLOBAL VARIABLES
*/
typedef struct
{
int value;
char resolved;
char *p;
} cCell;
int iNumCols = 0;
int iNumRows = 0;
//static cCell sFinalMatrix[MAXN][18280];
cCell resultMatrix[MAXN*18280];
long iFnSumCell( char *str, int x, int y )
{
long sum = 0;
int i = 1;
int len = 0;
int rowN = 0;
int colN = 0;
long temp = 0;
if(sFinalMatrix(x,y).p == NULL)
{
/* It is a pure number, return "column"*/
return x;
}
len = strlen( str );
if (len < MAXN)
{
str[len] = '+';
str[len + 1] = '\0';
}
else len = MAXN;
/* Check boundaries */
if(x>MAXN) x = MAXN;
if(y>18278) y = 18278;
while ((str[i]) && (i<18278))
{
if( isalpha(str[i]) )
{
if(islower(str[i])) str[i] = toupper(str[i]);
colN = colN * 26 + ( str[i] - 'A' + 1 );
}
else
if ( isdigit(str[i]) )
rowN += rowN * 10 + ( str[i] - '0' );
else if ( rowN == x && colN == y )
{
rowN = 0;
colN = 0;
return -INF;
}
else if ( sFinalMatrix(rowN,colN).resolved == FALSE )
{
/* Checking Matrix boundaries */
if(rowN > MAXN) rowN = MAXN-1;
if(colN > 18278) colN = 18277;
temp = iFnSumCell( (char *)sFinalMatrix(rowN,colN).p, rowN, colN );
if ( temp == -INF ) return -INF;
sum += temp;
sFinalMatrix(rowN,colN).value = temp;
sFinalMatrix(rowN,colN).resolved = TRUE;
if(DEBUG)
printf("test free1 row %d, col %d, string %s, mem %d\n", rowN, colN, sFinalMatrix(rowN,colN).p, sFinalMatrix(rowN,colN).p);
free( sFinalMatrix(rowN,colN).p );
if(DEBUG)
printf("test free1 done\n");
rowN = 0;
colN = 0;
}
else
{
sum += sFinalMatrix(rowN,colN).value;
if(DEBUG)
printf("test free2 row %d, col %d, string %s, mem %d\n", rowN, colN, sFinalMatrix(rowN,colN).p, sFinalMatrix(rowN,colN).p);
free( sFinalMatrix(rowN,colN).p );
if(DEBUG)
printf("test free2 done\n");
rowN = 0;
colN = 0;
}
i++;
}
return sum;
}
bool FnCalcMatrix(void)
{
int r,c;
for ( r = 1; r <= iNumRows; r++ )
for ( c = 1; c <= iNumCols; c++ )
if ( sFinalMatrix(r,c).resolved == FALSE )
{
long temp = iFnSumCell( sFinalMatrix(r,c).p, r, c );
if ( temp == -INF ) return FALSE;
else
{
sFinalMatrix(r,c).value = temp;
sFinalMatrix(r,c).resolved = TRUE;
free(sFinalMatrix(r,c).p);
}
}
return TRUE;
}
int main(void)
{
unsigned int uiNumSheets = 0;
unsigned int uiSheetsIx = 0;
/* Enter number of sheets */
scanf("%d", &uiNumSheets);
for(uiSheetsIx = 0; uiSheetsIx < uiNumSheets; uiSheetsIx++)
{
int iColsIx = 0;
int iRowsIx = 0;
/* Enter number of rows and columns*/
scanf("%d%d", &iNumCols, &iNumRows);
if(iNumCols > 18278) return 0;
if(iNumRows > 1000) return 0;
/* Rows Input */
for(iRowsIx = 1; iRowsIx <= iNumRows; iRowsIx++)
{
/* Cols Input */
for(iColsIx = 1; iColsIx <= iNumCols; iColsIx++)
{
/* Check if it is a value or a formula */
if ( scanf("%d",&sFinalMatrix(iRowsIx,iColsIx).value) == 0 )
{
int inputLen = 0;
sFinalMatrix(iRowsIx,iColsIx).p = (char *)malloc( 2048 * sizeof(char) );
if ( NULL == sFinalMatrix(iRowsIx,iColsIx).p) return 0;
inputLen = scanf("%1024s", sFinalMatrix(iRowsIx,iColsIx).p );
if(DEBUG)
printf("malloc row %d, col %d, string %s, mem %d\n", iRowsIx, iColsIx, sFinalMatrix(iRowsIx,iColsIx).p, sFinalMatrix(iRowsIx,iColsIx).p);
if (inputLen > MAXN) return 0;
if(sFinalMatrix(iRowsIx,iColsIx).p[0] == '=')
{
sFinalMatrix(iRowsIx,iColsIx).resolved = FALSE;
sFinalMatrix(iRowsIx,iColsIx).value = 0;
}
else
{
/* Invalid formula! */
sFinalMatrix(iRowsIx,iColsIx).resolved = TRUE;
sFinalMatrix(iRowsIx,iColsIx).value = 0;
sFinalMatrix(iRowsIx,iColsIx).p = NULL;
}
}
else
{
sFinalMatrix(iRowsIx,iColsIx).resolved = TRUE;
sFinalMatrix(iRowsIx,iColsIx).p = NULL;
}
}
}
FnCalcMatrix();
/* Show Final Matrix - Results */
for ( iRowsIx = 1; iRowsIx <= iNumRows; iRowsIx ++ )
{
for ( iColsIx = 1; iColsIx <= iNumCols; iColsIx++ )
{
if (iColsIx !=1 ) printf(" ");
printf( "%d", sFinalMatrix(iRowsIx,iColsIx).value );
}
printf("\n");
}
}
return 0;
}
Here is the output running this on my machine with the sample input:
Code: Select all
malloc row 1, col 4, string =A1+B1+C1, mem 298024976
malloc row 2, col 4, string =A2+B2+C2, mem 298027040
malloc row 3, col 1, string =A1+A2, mem 298029104
malloc row 3, col 2, string =B1+B2, mem 298031168
malloc row 3, col 3, string =C1+C2, mem 298033232
malloc row 3, col 4, string =D1+D2, mem 298035296
test free2 row 1, col 1, string (null), mem 0
test free2 done
test free2 row 1, col 2, string (null), mem 0
test free2 done
test free2 row 1, col 3, string (null), mem 0
test free2 done
test free2 row 2, col 1, string (null), mem 0
test free2 done
test free2 row 2, col 2, string (null), mem 0
test free2 done
test free2 row 2, col 3, string (null), mem 0
test free2 done
test free2 row 1, col 1, string (null), mem 0
test free2 done
test free2 row 2, col 1, string (null), mem 0
test free2 done
test free2 row 1, col 2, string (null), mem 0
test free2 done
test free2 row 2, col 2, string (null), mem 0
test free2 done
test free2 row 1, col 3, string (null), mem 0
test free2 done
test free2 row 2, col 3, string (null), mem 0
test free2 done
test free2 row 1, col 4, string ?6??*, mem 298024976
*** glibc detected *** double free or corruption (!prev): 0x0000000011c38010 ***Abort