tomtr84
asked on
increasing a 2d string array with realloc
Hi there,
i am trying to increase a dynamical allocated 2d array which contains strings. This array is part of a small Database Management System and has the following structure.
If I allocate memory for a specific row count and add row by row everything runs smoothly. But if I try to realloc the array to add new lines I get a segmentation fault error.
I try to realloc the array with the following function. The maxRowCount is the actual table row count increased by one.
Hope you guys can give me some clues to solve the problems :D
i am trying to increase a dynamical allocated 2d array which contains strings. This array is part of a small Database Management System and has the following structure.
/**table structure**/
typedef char *Element; /* char * */
typedef Element *TNRow; /* char ** */
typedef TNRow *TNRows; /* char *** */
typedef struct TableNodeData * TableNodeData;
struct TableNodeData {
unsigned int rowCount; /*max. row count in the table*/
unsigned int columnCount; /*max. column count in the table*/
unsigned int maxElemCount; /*max. elements in the array*/
unsigned int elemsIn; /*current element count in the array*/
/*TNRow elems; //not used anymore*/
TNRows rows; /*table data*/
};
/*Table list structure*/
typedef struct TableNode * TableList;
struct TableNode {
char *identifier; /*tablename*/
TableNodeData tableData; /*pointer to table data structure*/
unsigned int size; /*capacity (atm not used)*/
TableList next; /*pointer to next table node*/
};
If I allocate memory for a specific row count and add row by row everything runs smoothly. But if I try to realloc the array to add new lines I get a segmentation fault error.
I try to realloc the array with the following function. The maxRowCount is the actual table row count increased by one.
TableList increaseTableArray(TableList tl, int maxRowCount, int maxColumnCount, char *line) {
int row = 0, column = 0, i = 0, j = 0;
char tmpBuffer[40];
TableList res = malloc(sizeof(*res));
if (! res)
{
perror("table.c --> createTableHeader: malloc of res failed");
exit(1);
}
res->tableData = (TableNodeData) realloc(tl->tableData, sizeof(TableNodeData) * maxRowCount);
if(res != NULL) {
printf("struct increased!\n");
}
/* Here seems to be the first problem, but I dont know why. I already tried to create a completely new structure and assign the pointer of the new structure to the old one */
res->tableData->rows = (TNRows) realloc(tl->tableData->rows, sizeof(TNRow) * maxRowCount);
if(res->tableData->rows != NULL) {
printf("rowcount increased\n");
}
/* I think here is another problem, but I dont know how I else should realloc the memory for every row */
for(row = 0; row < maxRowCount; ++row) {
res->tableData->rows[row] = (TNRow) realloc(tl->tableData->rows[row], sizeof(char *) * maxColumnCount);
if(res->tableData->rows[row] != NULL) {
printf("realloc row/column!\n");
/*tl->tableData->rows[row] = res->tableData->rows[row];*/
/*tl->tableData->rows[row] = (TNRow) realloc(tl->tableData->rows[row], sizeof(char *) * maxColumnCount);*/
}
}
for( column = 0; column < maxColumnCount; ++column) {
res->tableData->rows[maxRowCount][column] = malloc(sizeof(char) * strlen(line) + 1);
if(res->tableData->rows[maxRowCount][column] != NULL)
printf("memory for new strings allocated!\n");
}
res->tableData->rowCount = maxRowCount;
res->tableData->maxElemCount = maxRowCount * maxColumnCount;
res->tableData->elemsIn += maxColumnCount;
column = 0;
tmpBuffer[0] = '\0';
do {
if(line[i] == ',' || line[i] == 10) {
tmpBuffer[j] = '\0';
/* commented until realloc works fine
strcpy(tl->tableData->rows[maxRowCount][column], tmpBuffer);*/
++column;
j = 0;
tmpBuffer[j] = '\0';
} else {
tmpBuffer[j] = line[i];
++j;
}
++i;
} while(line[i] != '\0');
return res;
}
Hope you guys can give me some clues to solve the problems :D
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
This one get parsed and I get those strings.
I just checked the maxRowCount Value. You are right there wasn't a valid value in. I missed to assign the value in another function.
Thank you very much.