# Thread: Reallocating multidimensional arrays

1. ## Reallocating multidimensional arrays

Hey there,
I have a 2dimensional array of integers but I failed with changing my code to for reallocation of that array.

Here's what I came up with:

Code:
```int k;
unsigned int numeffectids = rand()&#37;10; //this is the number of entries in the 2d array.
unsigned int **effectids; //<--- this will be the array
//Basically the array should look like this: effectids[numeffectids][2]; Both dimensions should hold an integer
effectids = malloc(0); //do i always need to malloc() first to use realloc() later?
int *p = &numeffectids;

while(1)
{
realloc(effectids, sizeof(int)*numeffectids);
if(effectids == NULL)
printf("Out of memory!");
else for(k = 0; k < numeffectids; k++)
{
realloc(effectids[k], sizeof(int));
if(effectids[k] == NULL)
printf("Out of memory!");
}
//Now write stuff in the array
for(k = 0; k < numeffectids && effectids != NULL && effectids[k] != NULL; k++)
{
effectids[k][0] = (unsigned int)p;
effectids[k][1] = GetTickCount();
printf("%u - %u\n", effectids[k][0], effectids[k][1]);
Sleep(10);
}
numeffectids = rand()%10;
}```
The output is random program crashes. I think every allocation of effectids[k] needs its own malloc instead of realloc, but that would be a memory leak, all I can think of is creating a new array with old+new data and free the old one, but then I needed no dynamic allocation -_-

So no matter what numeffectids is (even 0) the 2d array should be 4*2*numeffectids bytes large and be accessible with effectids[x][y], please point me into the right direction :x
Thanks in advance, Hawk

2. Where to start on this one... First off you need to understand that realloc() returns a pointer. Secondly you should be malloc()'ing each individual index of effectids.

I hate to beat a dead horse... but why not just make this a single dimension array thus eliminating the need for multiple malloc()'s. In your particular case this would be ideal since the second dimension is always 1 in width.[/edit]

3. Why are you using realloc?
Code:
```effectids = malloc (numeffectids * sizeof(int *)); /* for the first dimension */
for (i = 0; i < numeffectids; i++)
effectids[i] = malloc (2 * sizeof(int)); /* each subarray has two elements */```

4. I think this poor misguided student assumed that is how it is used.

http://man.he.net/?topic=realloc&section=all

By the way, feel free to use the man pages for any standard C function you are confused about. msdn2.microsoft.com is oh so much slower for the same information.

5. Yes I forgot that realloc returns a new pointer sometimes :x

Yes I can use malloc and it will work as intended, BUT everytime numeffectids changes it will allocate new memory instead of reallocate the old memory to the right size.

As I couldn't get it to work I will use a workaround for now using two sole arrays. In real code I will do the error handling and I will use a temp pointer as the return value of realloc so if it failes, my old pointer is not gone.

Code:
```	int k;
int numeffectids = 0, oldnumeffectids = numeffectids;
unsigned int *effectids_val, *effectids_time;
effectids_val = malloc(0);
effectids_time = malloc(0);
while(1)
{
if(numeffectids == 0) goto L_End;
effectids_val = realloc(effectids_val, sizeof(int)*numeffectids);
if(effectids_val == NULL)
printf("Out of memory!1\n");
effectids_time = realloc(effectids_time, sizeof(int)*numeffectids);
if(effectids_time == NULL)
printf("Out of memory!2\n");
else if(numeffectids > oldnumeffectids)
{
effectids_val[oldnumeffectids] = numeffectids;
effectids_time[oldnumeffectids] = GetTickCount();
printf("&#37;u - %u\n", effectids_val[oldnumeffectids], effectids_time[oldnumeffectids]);
}
//		else printf("new: %d, old: %d\n", numeffectids, oldnumeffectids);
L_End:
oldnumeffectids = numeffectids;
rand()%2==0?numeffectids++:numeffectids--;
if(numeffectids < 0)
numeffectids = 0;
Sleep(10);
}```
I was already familiar with allocating memory space but I couldn't program much in the last time and I forgot some stuff =/
But how would I convert this piece of code to one that uses one 2d array instead of two arrays?

6. The short answer is: you don't. The long answer is: if you really really really really really really really really have to, you can, but every time you resize you'll have to either (a) free the extra effectids[i] that are going to go away, if the size goes down or (b) malloc the effectids[i] that weren't there before, if the size goes up. The only thing you can realloc is the original "array" of pointers.

Popular pages Recent additions