0
 int row = 5, col =5 ;
arr = (int **) malloc(sizeof(int*) * row);

   for (int index=0; index<row; index++)
    {
          *(arr + index) = (int *) malloc(sizeof(int) * col);

    }

I using this code to declare a double pointer array. How to use realloc to increase both the rows and columns, if needed ? We don't know the number of inputs we are going to get :

void increase(int** arr)
{


  *arr =  (int *) realloc(*arr, 5 * sizeof (int));

}

I don't think that it's working. I need to reallocate both rows and columns. I inserted the condition:

if(var % 5 == 4)

then call the function increase and reallocate but it doesn't seems to be working.

  • Welcome to Stack Overflow! [Please see this discussion on why not to cast the return value of `malloc()` and family in `C`.](http://stackoverflow.com/q/605845/2173917). – Sourav Ghosh Dec 11 '16 at 16:42
  • I don't get it could please explain it to me ? Because I am getting an error main.cpp:27:18: error: invalid conversion from ‘void*’ to ‘int**’ [-fpermissive] arr = malloc(row * sizeof(int*) ); – Nilay Baranwal Dec 11 '16 at 16:47
  • 5
    so stop using a C++ compiler for compiling C code. – Sourav Ghosh Dec 11 '16 at 16:55
  • 1
    your `increase`-method doesn't even have a parameter to specify the dimensions of the new matrix... and to be honest, I think the problem isn't reallocating the memory in the first place, but that you don't have a clue of what your supposed to do. –  Dec 11 '16 at 16:57
  • @Paul I don't understand how it is suppose to be working in a double pointer array could you please explain ? – Nilay Baranwal Dec 11 '16 at 17:06
  • "it doesn't seems to be working." --> Post the code that calls this code and itself shows "not working" as the scant description is insufficient. – chux - Reinstate Monica Dec 11 '16 at 17:07
  • When describing a double pointer problem, any discussion about it is more clear is the row and column are not the same value. Suggest `int row = 3, col = 4;` – chux - Reinstate Monica Dec 11 '16 at 17:09
  • @SouravGhosh I used the gcc compile my file and it's working but I am getting a segmentation fault when I take input for more than five columns as the realloc is not working. – Nilay Baranwal Dec 11 '16 at 17:11
  • @chux When I input more than 5 columns I get a segmentation fault (core dumped). That means it's not reallocating the column value. – Nilay Baranwal Dec 11 '16 at 17:16
  • See [comment](http://stackoverflow.com/questions/41088500/how-to-use-realloc-in-a-double-pointer-array?noredirect=1#comment69382319_41088500). Your description still lack details that code would provide. – chux - Reinstate Monica Dec 11 '16 at 17:22
  • @NilayBaranwal there isn't much to explain. Reallocate each row to the appropriate size, reallocate the array holding the rows to the appropriate size, and allocate additional memory for the rows that are newly added to the matrix. The main problem is still that your question is pure chaos and I doubt you've understood the basics of the topic youre working on from what this question looks like. –  Dec 11 '16 at 17:26
  • Are the number of columns variable? For example, are you reading something row-by-row that has 5 columns in one row and 12 columns in the next? If not, you might consider using a pointer to an array instead of a jagged array. This would make your code a bit more manageable since reallocation would involve only changing the number of rows, not the number of columns. –  Dec 11 '16 at 17:50
  • Do you know how to use structures? You should use a structure to describe the array: the number of rows and columns, and a pointer to the start of the array of pointers. Your 'increase' function is given the current array structure and the new size. Assuming both new dimensions are at least as large as before, adding space is not too hard. If the number of columns increases, you have to reallocate each row to the new size. If the number of rows increases, you have to add rows of the appropriate size. Your question shows none of the logic required to handle such growth or shrinkage. – Jonathan Leffler Dec 11 '16 at 19:21
  • If you don't (yet) know structures, then you'll have to use three parameters to describe the current array, and two more to describe the new size. – Jonathan Leffler Dec 11 '16 at 19:21

2 Answers2

0
  1. To make your code reusable for different data-type in C (int/double). You need to use the concept of generic data-type.

  2. To use generic data-type, it might require you to implement data-type specific custom free function. For int and double you do not need to use custom specific free function; but free is not trivial if you are new to C.

  3. To grow your data-structure, you need to define a function which grows the data-structure. Check this stack implementation from Stanford (Lecture: [4, 8)) https://www.youtube.com/playlist?list=PL9D558D49CA734A02

One advice. Do not re-cast the output of malloc(). Not required! Change:

arr = (int **) malloc(sizeof(int*) * row);

To:

arr = malloc(sizeof(int*) * row);
Ehsan
  • 1,338
  • 14
  • 13
0

I am getting an error main.cpp…

Don't use the file name suffix .cpp for C programs - .cpp may cause gcc to compile the source code for another language.

I need to reallocate both rows and columns.

Since you don't pass dimensions to your increase() function, you apparently want it to increase the number of both rows and columns by 5. Since you used realloc(*arr, 5 * sizeof (int)), you apparently overlooked that the size passed to realloc() is not an increment, but rather the total size of the new memory space. Also, you should account for the possibility that the reallocation in increase() fails, and allow it to return an error indication. The following example returns the new (maybe moved) pointer or NULL in case of failure. As a bonus, this increase() function can also be used for the initial creation.

int row, col;

#include <malloc.h>

int **increase(int **arr)
{   // enlarge the "matrix" by 5 rows and 5 columns
    int rownew = row+5, colnew = col+5;
    arr = realloc(arr, rownew * sizeof *arr);   // increase the rows
    if (!arr) return NULL;                      // realloc error
    do arr[row++] = NULL; while (row < rownew); // initialize new row pointers
    for (rownew = 0; rownew < row; ++rownew)
    {   // increase the columns in each row
        int *newptr = realloc(arr[rownew], colnew * sizeof *newptr);
        if (!newptr) return NULL;               // realloc error
        arr[rownew] = newptr;
    }
    col = colnew;
    return arr;
}

main()
{
    int **arr = increase(NULL);     // create initial 5x5 "matrix"
    int **newarr = increase(arr);   // enlarge to new 10x10 "matrix"
    if (newarr) arr = newarr;
    else /* realloc error handling */;
}

Note that in something other than that toy example, we'd probably pass the dimensions as parameters rather than as globals.

Armali
  • 18,255
  • 14
  • 57
  • 171