# Thread: I need help with my code

1. ## I need help with my code

Hi! I'm working on a program that can swipe two dynamically allocated 2d arrays. I'm new to dynamic allocation and i can't figure out where is the problem. I believe that i messed up with reallocation of the arrays because in some exemples it works and in others it doesn't. I've searched for a solution and i can't find anything to solve this. I would also really appreciate any tips, links, books etc. to understand dynamic allocation better.
Here is the code:
Code:
```void alloc(int*** matrix, const int lin, const int col)
{
*matrix = (int**)malloc(sizeof(int*) * lin);
if (*matrix == NULL) {
printf("Error");
exit(1);
}
for (int i = 0;i < lin;i++) {
(*matrix)[i] = (int*)malloc(sizeof(int) * col);
}
for (int i = 0;i < lin;i++)
for (int j = 0;j < col;j++)
{
(*matrix)[i][j] = i + j;
}
}
int** copy(int** matrix, const int lin, const int col)
{
int** buffer;
alloc(&buffer, lin, col);
for (int i = 0;i < lin;i++)
for (int j = 0;j < col;j++)
{
buffer[i][j] = matrix[i][j];
}
return buffer;
}

//
// PROBLEM IS OVER HERE
//
void reall(int*** matrix1, const int lin1, const int col1, int** matrix2,
const int lin2, const int col2)
{
int** temp1 = (int**)realloc(*matrix1, lin2 * sizeof(int*));
for (int i = 0;i < lin2;i++) {
temp1[i] = NULL;
}
for (int i = 0;i < lin2;i++) {
temp1[i] = (int*)realloc((*matrix1)[i], col2 * sizeof(int));
}
*matrix1 = temp1;
for (int i = 0;i < lin2;i++)
for (int j = 0;j < col2;j++)
(*matrix1)[i][j] = matrix2[i][j];
}
int main() {
int** matrix1, ** matrix2, ** twin;
int n1, m1, n2, m2;
printf("Number of lines for Ist matrix:");
scanf("%d", &n1);
printf("Number of collums for Ist matrix:");
scanf("%d", &m1);
printf("Number of lines for IInd matrix:");
scanf("%d", &n2);
printf("Number of collums for IInd matrix:");
scanf("%d", &m2);
alloc(&matrix1, n1, m1);
alloc(&matrix2, n2, m2);
twin = copy(matrix1, n1, m1);
reall(&matrix1, n1, m1, matrix2, n2, m2);
reall(&matrix2, n2, m2, twin, n1, m1);
//
//PRINT MATRIX
//
for (int i = 0;i < n2;i++)
{
for (int j = 0;j < m2;j++)
{
printf("%d ", matrix1[i][j]);
}
printf("\n");
}
printf("\n\n");
for (int i = 0;i < n1;i++)
{
for (int j = 0;j < m1;j++)
{
printf("%d ", matrix2[i][j]);
}
printf("\n");
}
//
//DEALLOCATION
//
for (int i = 0; i < m1; ++i)
free(twin[i]);
free(twin);

for (int i = 0; i < m2; ++i)
free(matrix1[i]);
free(matrix1);
for (int i = 0; i < m1; ++i)
free(matrix2[i]);
free(matrix2);
}```

2. Your problem is that you are realloc()ing the existing rows, then initializeing the whole row to zero.

You only need to initialize any new items added to the row.

You also do not need to do the copy (because the realloc() ensures the existing items are still there).

You should also allow for realloc() and malloc() failing.

For 2D arrays you can have two options.

Option 1: Allocate a 1D array for row pointers
Then allocate each row, in a for loop.

This allows you to keep using the following syntax when addressing things:

Code:
`array[y][x] = ...`
Option 2: Allocate a 1-D W*H array, and address it as

Code:
`array[y*width+x] = ...`
You can always slice the array up as needed:
Code:
```row = array+y*width;
row[x] = ...```
Sometimes one option is easier than the other., For example, if you know you will need to zero out the array, for option one you need to call memset(array,0,row_size) for each row, for option two you can just memset(array,0,array_size).

3. Code:
```void reall(int*** matrix1, const int lin1, const int col1, int** matrix2,
const int lin2, const int col2)
{
int** temp1 = (int**)realloc(*matrix1, lin2 * sizeof(int*));
for (int i = 0;i < lin2;i++) {
temp1[i] = NULL;
}
for (int i = 0;i < lin2;i++) {
temp1[i] = (int*)realloc((*matrix1)[i], col2 * sizeof(int));
}
*matrix1 = temp1;
for (int i = 0;i < lin2;i++)
for (int j = 0;j < col2;j++)
(*matrix1)[i][j] = matrix2[i][j];
}```
Needs to follow this pseudocode.
Code:
```If the new matrix has less rows.
free() the un-needed rows.
realloc() the matrix smaller

If the matrix is getting narrower
reallocate() all the existing rows smaller
else if it is getting wider
reallocate() all the existing rows wider
zero the new items added to each row.

if is getting more rows
realloc() the matrix larger
allocate and zero out new rows.```

4. Originally Posted by hamster_nz
You also do not need to do the copy (because the realloc() ensures the existing items are still there).
I don't exactly understand what are you reffering to. I don't need the copy function or i don't need to copy the temp1 in matrix1?

5. This is my code so far. I'm lost. I don't know where I a wrong.
Code:
```void alloc(int*** matrix, const int lin, const int col) {
*matrix = (int**)malloc(sizeof(int*) * lin);
if (*matrix == NULL) {
printf("Error");
exit(1);
}
for (int i = 0;i < lin;i++) {
(*matrix)[i] = (int*)malloc(sizeof(int) * col);
if ((*matrix)[i] == NULL) {
printf("Error");
exit(1);
}
}

for (int i = 0;i < lin;i++)
for (int j = 0;j < col;j++)
{
(*matrix)[i][j] = i + j;
}
}
int** copy(int** matrix, const int lin, const int col) {
int** buffer;
alloc(&buffer, lin, col);
for (int i = 0;i < lin;i++)
for (int j = 0;j < col;j++)
{
buffer[i][j] = matrix[i][j];
}
return buffer;
}
void reall(int*** matrix1,const int lin1,const int col1, int** matrix2,
const int lin2, const int col2) {
if (lin1 > lin2)
{
for (int i = lin1 - 1;i >= lin2;i--)
{
free((*matrix1)[i]);
}
int** temp1 = (int**)realloc(*matrix1, sizeof(int*) * lin2);
if (temp1== NULL) {
printf("Error");
exit(1);
}
*matrix1 = temp1;
}
if (col1 > col2)
{
for (int i = 0;i < lin2;i++)
{
int* temp2 = (int*)realloc((*matrix1)[i], sizeof(int) *
col2);
if (temp2 == NULL) {
printf("Error");
exit(1);
}
(*matrix1)[i] = temp2;
}

}
if (col1 < col2)
{
for (int i = 0;i < lin2;i++)
{
int* temp2 = (int*)realloc((*matrix1)[i], sizeof(int) *
col2);
if (temp2 == NULL) {
printf("Error");
exit(1);
}
(*matrix1)[i] = temp2;
}
for (int j = 0;j < lin2;j++)
{
for (int i = col1;i < col2;i++)
{
(*matrix1)[j][i] = 0;
}
}
}
if (lin1 < lin2)
{
int** temp1 = (int**)realloc(*matrix1, sizeof(int*) * lin2);
if (temp1 == NULL) {
printf("Error");
exit(1);
}
*matrix1 = temp1;
for (int i = lin1;i < lin2;i++) {
(*matrix1)[i] = (int*)malloc(sizeof(int) * col2);
if ((*matrix1[i]) == NULL) {
printf("Error");
exit(1);
}
}
for (int j = lin1;j < lin2;j++)
{
for (int i = 0;i < col2;i++)
{
(*matrix1)[j][i] = 0;
}
}
}
for (int i = 0;i < lin2;i++)
for (int j = 0;j < col2;j++)
(*matrix1)[i][j] = matrix2[i][j];
}
int main() {
int** matrix1, ** matrix2, ** twin;
int n1, m1, n2, m2;
printf("Number of lines for Ist matrix:");
scanf("%d", &n1);
printf("Number of collums for Ist matrix:");
scanf("%d", &m1);
printf("Number of lines for IInd matrix:");
scanf("%d", &n2);
printf("Number of collums for IInd matrix:");
scanf("%d", &m2);
alloc(&matrix1, n1, m1);
alloc(&matrix2, n2, m2);
twin = copy(matrix1, n1, m1);
reall(&matrix1, n1, m1, matrix2, n2, m2);
reall(&matrix2, n2, m2, twin, n1, m1);
for (int i = 0;i < n2;i++)
{
for (int j = 0;j < m2;j++)
{
printf("%d ", matrix1[i][j]);
}
printf("\n");
}
printf("\n\n");
for (int i = 0;i < n1;i++)
{
for (int j = 0;j < m1;j++)
{
printf("%d ", matrix2[i][j]);
}
printf("\n");
}
for (int i = 0; i < m1; i++)
free(twin[i]);
free(twin);

for (int i = 0; i < m2; i++)
free(matrix1[i]);
free(matrix1);
for (int i = 0; i < m1; i++)
free(matrix2[i]);
free(matrix2);
}```

6. Can you describe in words what reall is supposed to do?

Is it supposed to be like 'copy' but for matrices with different sizes?

7. Yes.It is supposed to first realloc the matrix1 with the dimensions of the matrix 2 and then put the elements from matrix2 in matrix1

Popular pages Recent additions