I am growing a 2D array by it's column and row size in c programming. 

```
#include <stdio.h>
#include <malloc.h>
//#include <conio.h>
//#include <stdlib.h>

void func(int** p, int d, int** sizes)
{
    static int count = 0;
    int* item = NULL;
    int* temp = NULL;
    if (d == 5) return;
    *sizes = (int*) realloc(*sizes, (d + 1) * sizeof(*sizes));
    *sizes[count] = d + 1; // <=== in second recursive call it throws memory allocation errors here in runtime 

    //p = (int**) realloc(p, (count + 1) * sizeof(int*));

    //printf("%d\n", *sizes[count]);

    //item = (int*) realloc(*(p + d) , *sizes[count] * sizeof(int));
    //if (item) *(p + *sizes[count]) = item;

    ++count;
    func(p, d + 1, sizes);
}

int main()
{
    int* sizes = (int*) malloc(sizeof(int)); // different column sizes of rows
    int** p = (int**) malloc(sizeof(int*));
    *p = (int*) malloc(sizeof(int));
    printf("Hello World!\n");
    func(p, 0, &sizes);
    printf("Hello End!\n");
    getchar();
    free(p);
    return 0;
}

```

But I am stuck in memory allocation errors.
[![enter image description here](https://i.stack.imgur.com/XQYhw.png)](https://i.stack.imgur.com/XQYhw.png)
Would Someone please help? Am I using realloc() in wrong way? please point my mistakes. Actually I was going to solve a problem in leetcode.com where I required a variable column sized 2D array in c programming. Thus I thought this brainstorinming would help me understand the dynamic 2D array first. Then I will move into the real problem.

EDIT:

```
#include <stdio.h>
#include <malloc.h>
//#include <conio.h>
//#include <stdlib.h>

void __cdecl func(int** p, int d, int** sizes, int* rows)
{
    int* item = NULL;
    if (d == 5) return;
    *sizes = (int*) realloc(*sizes, (d + 1) * sizeof(*sizes));
    (*sizes)[*rows] = d + 1;

    p = (int**) realloc(p, (*rows + 1) * sizeof(int*));

    printf("%d\n", (*sizes)[*rows]);

    item = (int*) malloc((*sizes)[*rows] * sizeof(int));
    item = (int*) realloc(item , (*sizes)[*rows] * sizeof(int));

    if (item) *(p + *rows) = item;
    p[*rows][(*sizes)[*rows] - 1] = 1;
    printf("item[%d][%d]: %d\n", *rows, (*sizes)[*rows] - 1, p[*rows][(*sizes)[*rows] - 1]);
    ++(*rows);
    func(p, d + 1, sizes, rows);
}

int main()
{
    int rows = 0, i = 0, j = 0;
    int* sizes = (int*) malloc(sizeof(int));
    int** p = (int**) malloc(sizeof(int*));

    *p = (int*) malloc(sizeof(int));

    printf("Hello World!\n");
    func(p, 0, &sizes, &rows);
    printf("Hello End!\n");

    for (; i < rows; ++i)
    {
        int j = sizes[i] - 1;
        printf("p[%d][%d]\n", i, j, p[i][j]); // CAN'T ACCESS HERE
    }

    //for (; i < rows; ++i)
    //{
    //  if (*(p + i))
    //  {
    //      free(*(p + i));
    //      *(p + i) = NULL;
    //  }
    //}
    //free(p);
    //free(sizes);

    getchar();
    return 0;
}

```

I can't access the array when the recursive function exits performing.
I have printed the diagonal values of the 2D array only malloced during recursion. But outside it's showing access violation.

[![enter image description here](https://i.stack.imgur.com/kMJ0i.png)](https://i.stack.imgur.com/kMJ0i.png)