Link to home
Start Free TrialLog in
Avatar of tomtr84
tomtr84Flag for Germany

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.

/**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*/
};

Open in new window


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;
}

Open in new window


Hope you guys can give me some clues to solve the problems :D
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tomtr84

ASKER

The data I use comes from a script file and look like the following ones.

APPEND TO table01 S001,"Schlagbohrmaschine 1",5,99
APPEND TO table01 S002,"Schlagbohrmaschine 2",5,199
APPEND TO table01 S003,"Schlagbohrmaschine 3",5,299
APPEND TO table01 S004,"Schlagbohrmaschine 4",5,399
APPEND TO table01 S005,"Schlagbohrmaschine 5",5,499

This one get parsed and  I get those strings.

S001,"Schlagbohrmaschine 1",5,99
S002,"Schlagbohrmaschine 2",5,199
S003,"Schlagbohrmaschine 3",5,299
S004,"Schlagbohrmaschine 4",5,399
S005,"Schlagbohrmaschine 5",5,499

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.