Expansion of two-dimensional array

Hi, this is my first topic in this site. So I'm going to go directly to the problem with reallocating two dimensional array with realloc, because I want to expand the 2D array without lousing previous data.

So I came with idea.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <stdio.h>
#include <stdlib.h> 

int **asd;

int size = 10;
int main(void)
{
 
 int i,j;
    
   asd = (int **)realloc(asd, sizeof(int)*(size*size));
   for(i = 0 ; i < size ; i++)
   asd[i] = (int *)realloc(asd[i], sizeof(int)*size);
   
    for(i = 0 ; i < size ; i++)
     for(j = 0 ; j < size ; j++)
     asd[i][j] = 10;
     
   for(i = 0 ; i < size ; i++){
     for(j = 0 ; j < size ; j++){  
     printf(" %d ", asd[i][j]);
     }
     putchar('\n');
     } 
             
 return 0;   
}


But after few failure attempts I realized, that maybe I'm destroying the other pointers after the first cycle of reallocating the first pointer.

Next I to do try something else to see if it works, and it did work. I replace the realloc with malloc.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <stdio.h>
#include <stdlib.h> 

int **asd;

int size = 10;

int main(void)
{
 
 int i,j;
    
   asd = (int **)realloc(asd, sizeof(int)*(size*size));
   for(i = 0 ; i < size ; i++)
   asd[i] = (int *)malloc(sizeof(int)*size);
   
    for(i = 0 ; i < size ; i++)
     for(j = 0 ; j < size ; j++){            
     asd[i][j] = 10;
     }
     
   for(i = 0 ; i < size ; i++){
     for(j = 0 ; j < size ; j++){  
     printf(" %d ", asd[i][j]);
     }
     putchar('\n');
     } 
            
 return 0;   
}


I still didn't try to see if data stay whole after using malloc, but i dont belive it will.

Maybe I'm going into the wrong direction, so I need help.

Now I'm going to explain the whole idea why I need this. The thing is, that I'm learning how draw graphic primitives, and most of the code, wich involves mathematics, I'm trying to write by myself.
And I'm using one 2D matrix for all objects to tell the program wich objects are related. I did that and now I want to optimize the code by changing the 2D matrix size when object is added or removed. The 2D matrix will be used to save the current graphics in a file.

And I have another question, when reallocate memory, must I have even set of bytes when divided by four to equals zero.

For my last question I'm not shure if I ask correctly.
Last edited on
I did it. I solved my problem.

The answer were quite simple: all i was needing was one double pointer variable and one single pointer variable. Then i reallocate the double pointer and the single pointer, after that i start a cycle to connect the double pointers with the singles, and reallocate memory to the singles pointers. It was that simple really.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <stdio.h>
#include <stdlib.h> 

int **Prows;
int *Pcol;


int size = 10;
int addsize = 10;

int main(void)
{
 
 int i,j;
    
   Prows = (int **)realloc(Prows, sizeof(int)*size);
   Pcol = (int *)realloc(Pcol, sizeof(int)*size);
   for(i = 0 ; i < size ; i++)
   Prows[i] = (int *)realloc(Pcol, sizeof(int)*size);
   
   
    for(i = 0 ; i < size ; i++)
     for(j = 0 ; j < size ; j++){            
     Prows[i][j] = 10;
     }
    
   Prows = (int **)realloc(Prows, sizeof(int)*(size+addsize));
   Pcol = (int *)realloc(Pcol, sizeof(int)*(size+addsize));
   for(i = 0 ; i < size+addsize ; i++)
   Prows[i] = (int *)realloc(Pcol, sizeof(int)*(size+addsize));
   
    for(i = size ; i < size+addsize ; i++)
     for(j = size ; j < size+addsize ; j++){            
     Prows[i][j] = 20;
     }
    
     for(i = 0 ; i < size+addsize ; i++){
     for(j = 0 ; j < size+addsize ; j++){   
     printf("%d ", Prows[i][j]);
   
     }
     putchar('\n');
     } 
     
     system("pause");        
 return 0;   
}



10 10 10 10 10 10 10 10 10 10 20 20 20 20 20 20 20 20 20 20
10 10 10 10 10 10 10 10 10 10 20 20 20 20 20 20 20 20 20 20
10 10 10 10 10 10 10 10 10 10 20 20 20 20 20 20 20 20 20 20
10 10 10 10 10 10 10 10 10 10 20 20 20 20 20 20 20 20 20 20
10 10 10 10 10 10 10 10 10 10 20 20 20 20 20 20 20 20 20 20
10 10 10 10 10 10 10 10 10 10 20 20 20 20 20 20 20 20 20 20
10 10 10 10 10 10 10 10 10 10 20 20 20 20 20 20 20 20 20 20
10 10 10 10 10 10 10 10 10 10 20 20 20 20 20 20 20 20 20 20
10 10 10 10 10 10 10 10 10 10 20 20 20 20 20 20 20 20 20 20
10 10 10 10 10 10 10 10 10 10 20 20 20 20 20 20 20 20 20 20
10 10 10 10 10 10 10 10 10 10 20 20 20 20 20 20 20 20 20 20
10 10 10 10 10 10 10 10 10 10 20 20 20 20 20 20 20 20 20 20
10 10 10 10 10 10 10 10 10 10 20 20 20 20 20 20 20 20 20 20
10 10 10 10 10 10 10 10 10 10 20 20 20 20 20 20 20 20 20 20
10 10 10 10 10 10 10 10 10 10 20 20 20 20 20 20 20 20 20 20
10 10 10 10 10 10 10 10 10 10 20 20 20 20 20 20 20 20 20 20
10 10 10 10 10 10 10 10 10 10 20 20 20 20 20 20 20 20 20 20
10 10 10 10 10 10 10 10 10 10 20 20 20 20 20 20 20 20 20 20
10 10 10 10 10 10 10 10 10 10 20 20 20 20 20 20 20 20 20 20
10 10 10 10 10 10 10 10 10 10 20 20 20 20 20 20 20 20 20 20
Press any key to continue . . .


That is how you make dynamic two-dimensional array.

Also I came whit it laying in bed, after watching "doctor who - A Cristmas Carol". Thrilled from the movie, thinking how he always asks the hard questions, and how he is the one answering them.

On the other hand, I never find anything like my code in any tutorial. So its free for using. And the most important it allows to expand two-dimensional array whitout losing the previous data.
This code you have is wrong. All of the prows[i] point to the same data. If you don't believe me, change prows[3][3] to 4, and watch what happens. This is what you must do to reallocate to a n by m array:
1
2
prows=realloc(prows,sizeof(int*)*n);
for(int i=0;i<n;i++)prows[i]=realloc(prows[i],sizeof(int)*m);

Your code was reallocating the same memory to the same size over and over, while assigning the same pointer to all of the rows.
std::vector?
Yeah, you are right.


32D20 32D24 32D28 32D2C 32D30 32D34 32D38 32D3C 32D40 32D44
32D20 32D24 32D28 32D2C 32D30 32D34 32D38 32D3C 32D40 32D44
32D20 32D24 32D28 32D2C 32D30 32D34 32D38 32D3C 32D40 32D44
32D20 32D24 32D28 32D2C 32D30 32D34 32D38 32D3C 32D40 32D44
32D20 32D24 32D28 32D2C 32D30 32D34 32D38 32D3C 32D40 32D44
32D20 32D24 32D28 32D2C 32D30 32D34 32D38 32D3C 32D40 32D44
32D20 32D24 32D28 32D2C 32D30 32D34 32D38 32D3C 32D40 32D44
32D20 32D24 32D28 32D2C 32D30 32D34 32D38 32D3C 32D40 32D44
32D20 32D24 32D28 32D2C 32D30 32D34 32D38 32D3C 32D40 32D44
32D20 32D24 32D28 32D2C 32D30 32D34 32D38 32D3C 32D40 32D44


Tell me, is your version work properly rocketboy9000. And if it is, what compiler are you using.
std::vector could be used, but he's not using iostream or new[], so I assume he's doing C not C++. I think there should be a renew[], but maybe that's just me. Maybe it'll be in c++0x.
about my version I haven't tested it, hold on, I'll test it.
Last edited on
OK I have replaced your reallocations with my code and it works:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <stdio.h>
#include <stdlib.h> 

int **Prows=NULL;
int *Pcol=NULL;


int size = 10;
int addsize = 10;

int main(void)
{
 
 int i,j;
    
   Prows=realloc(Prows,sizeof(int*)*size);
	for(i=0;i<size;i++)Prows[i]=realloc(Prows[i],sizeof(int)*size);
   
    for(i = 0 ; i < size ; i++)
     for(j = 0 ; j < size ; j++){            
     Prows[i][j] = 10;
     }
    
    Prows[3][4]=4;//notice that only ONE cell is affected.
    
  Prows=realloc(Prows,sizeof(int*)*(size+addsize));
	for(i=0;i<size+addsize;i++)Prows[i]=realloc(Prows[i],sizeof(int)*(size+addsize));
 
    for(i = size ; i < size+addsize ; i++)
     for(j = size ; j < size+addsize ; j++){            
     Prows[i][j] = 20;
     }
     
      printf("here?\n");
     for(i = 0 ; i < size+addsize ; i++){
     for(j = 0 ; j < size+addsize ; j++){   
     printf("%d ", Prows[i][j]);
   
     }
     putchar('\n');
     } 
     
     system("pause");        
 return 0;   
}

10 10 10 10 10 10 10 10 10 10 0 0 0 0 0 0 0 0 0 0 
10 10 10 10 10 10 10 10 10 10 0 0 0 0 0 0 0 0 0 0 
10 10 10 10 10 10 10 10 10 10 0 0 0 0 0 0 0 0 0 0 
10 10 10 10 4 10 10 10 10 10 0 0 0 0 0 0 0 0 0 0 
10 10 10 10 10 10 10 10 10 10 0 0 0 0 0 0 0 0 0 0 
10 10 10 10 10 10 10 10 10 10 0 0 0 0 0 0 0 0 0 0 
10 10 10 10 10 10 10 10 10 10 0 0 0 0 0 0 0 0 0 0 
10 10 10 10 10 10 10 10 10 10 0 0 0 0 0 0 0 0 0 0 
10 10 10 10 10 10 10 10 10 10 0 0 0 0 0 0 0 0 0 0 
10 10 10 10 10 10 10 10 10 10 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 20 20 20 20 20 20 20 20 20 20 
0 0 0 0 0 0 0 0 0 0 20 20 20 20 20 20 20 20 20 20 
0 0 0 0 0 0 0 0 0 0 20 20 20 20 20 20 20 20 20 20 
0 0 0 0 0 0 0 0 0 0 20 20 20 20 20 20 20 20 20 20 
0 0 0 0 0 0 0 0 0 0 20 20 20 20 20 20 20 20 20 20 
0 0 0 0 0 0 0 0 0 0 20 20 20 20 20 20 20 20 20 20 
0 0 0 0 0 0 0 0 0 0 20 20 20 20 20 20 20 20 20 20 
0 0 0 0 0 0 0 0 0 0 20 20 20 20 20 20 20 20 20 20 
0 0 0 0 0 0 0 0 0 0 20 20 20 20 20 20 20 20 20 20 
0 0 0 0 0 0 0 0 0 0 20 20 20 20 20 20 20 20 20 20 
Yes it work. Actually the problem was that on DEV-C++ with the default gcc compiler it crashes. Even your code.
I feel silly, for not changing te compiler sooner.
Thanks rocketboy9000.
The compiler was not the problem. I am using GCC myself. The code I posted above works fine.
Topic archived. No new replies allowed.