Reallocating multidimensional arrays

This is a discussion on Reallocating multidimensional arrays within the C Programming forums, part of the General Programming Boards category; Hey there, I have a 2dimensional array of integers but I failed with changing my code to for reallocation of ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    68

    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()%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
    Last edited by Hawkin; 04-26-2008 at 07:43 PM.

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    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.

    [edit]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]
    Last edited by master5001; 04-26-2008 at 07:46 PM.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    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. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    68
    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?
    Last edited by Hawkin; 04-27-2008 at 12:04 PM.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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 subscribe to a feed

Similar Threads

  1. Multidimensional Arrays
    By jordanguyoflove in forum C Programming
    Replies: 4
    Last Post: 10-16-2008, 07:16 PM
  2. Multidimensional Arrays?
    By CreatedByShadow in forum C++ Programming
    Replies: 7
    Last Post: 01-13-2006, 10:35 PM
  3. Pointers to Multidimensional Arrays
    By kidburla in forum C Programming
    Replies: 10
    Last Post: 10-29-2005, 11:45 PM
  4. Multidimensional arrays in Korn shell
    By Yasir_Malik in forum Tech Board
    Replies: 3
    Last Post: 04-11-2004, 03:16 PM
  5. Replies: 4
    Last Post: 11-05-2001, 02:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21