We help IT Professionals succeed at work.
Get Started

increasing a 2d string array with realloc

tomtr84 asked
Last Modified: 2012-05-11
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");

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

        j = 0;
        tmpBuffer[j] = '\0';
      } else {

        tmpBuffer[j] = line[i];
    } while(line[i] != '\0');
  return res;

Open in new window

Hope you guys can give me some clues to solve the problems :D
Watch Question
Most Valuable Expert 2014
Top Expert 2015
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE