# Thread: swap dynamically allocated arrays

1. ## swap dynamically allocated arrays

Why the following codes don't produce the same results?

declaration (common part)
Code:
```double **before, **temp;
before = malloc(N * sizeof(double *));
for ( i = 0; i < M; i++)
{
before[i] = malloc(M * sizeof(double));
}

double **after;
after = malloc(N * sizeof(double *));
for ( i = 0; i < M; i++)
{
after[i] = malloc(M * sizeof(double));
}```
First code (we are only interested in before getting the values of after.
Code:
```// for each line in the array
for (i = 0; i < n; i++)
{
// for each column in the array
for (j = 0; j < m ; j++)
{
before[i][j] = after[i][j];
}
}```
Second try:

Code:
```temp = before;
before = after;
after = temp;```
The second try seems not to work properly. Why? Shouldn't they produce the same results?

2. >The second try seems not to work properly.

How so?

3. Code:
```    for (k = 0; diff > epsilon && k <= 1000; k++)
{
diff = 0;
// for each line in the array
for (i = 1; i < n - 1; i++)
{
// for each column in the array
for (j = 1; j < m - 1; j++)
{
// calculate the values according to Jacobi's algorithm
after[i][j] = 0.25 * (before[i - 1][j] + before[i + 1][j] + before[i][j - 1] + before[i][j + 1]);

/* calculate the difference of the new value in comparison with the previous one
* and keep only the greatest difference */
if (absolute(after[i][j] - before[i][j]) > diff)
{
diff = absolute(after[i][j] - before[i][j]);
}
}
}

// for each line in the array
for (i = 1; i < n - 1; i++)
{
// for each column in the array
for (j = 1; j < m - 1; j++)
{
before[i][j] = after[i][j];
}
}```
doesn't seem to produce the same results as:

Code:
```    for (k = 0; diff > epsilon && k <= 1000; k++)
{
diff = 0;
// for each line in the array
for (i = 1; i < n - 1; i++)
{
// for each column in the array
for (j = 1; j < m - 1; j++)
{
// calculate the values according to Jacobi's algorithm
after[i][j] = 0.25 * (before[i - 1][j] + before[i + 1][j] + before[i][j - 1] + before[i][j + 1]);

/* calculate the difference of the new value in comparison with the previous one
* and keep only the greatest difference */
if (absolute(after[i][j] - before[i][j]) > diff)
{
diff = absolute(after[i][j] - before[i][j]);
}
}
}

temp = before;
before = after;
after = temp;
}```

4. In the first version, 'after' remains the same and its content is copied into before, in the second version 'before' and 'after' are exchanged so the content of after does change. As you're still using 'after' in your algorithm, you get different results.

5. But as you can see, I don't use the elements of the array after in the program. I merely write in the array after the results, so I don't care what were its previous values.
What I am really interested in, is before to contain the updated values.

6. But as you can see, I don't use the elements of the array after in the program

There is an index error in your arrays init. :
Code:
```before = malloc(N * sizeof(double *));
for ( i = 0; i < M; i++)
{
...
after = malloc(N * sizeof(double *));
for ( i = 0; i < M; i++)
{
...```
but I'm not convinced it would lead to the problem discussed here.

doesn't seem to produce the same results as:...
Could you detail that? how do you check the result? The method should work anyway, there must be a bug somewhere.

7. What is the index error?
Valgrind doesn't return any warning for possible errors when I test my code.

...I print the results and are not the same. It's weird. I miss something.

EDIT: You mean I have used N, M capitals in the loop where the allocation of the memory is done. Thanks for noticing, N and M are constants which happens to be the same as n, m but the appropriate way was to use n, m.

8. Code:
```        // for each line in the array
for (i = 1; i < n - 1; i++)
{
// for each column in the array
for (j = 1; j < m - 1; j++)
{
before[i][j] = after[i][j];
}
}```
Is NOT the same as
Code:
```        temp = before;
before = after;
after = temp;```
Every iteration you alternate before and after, that's not the same as setting before to after. Any reason why you're starting at '1' rather than 0 also?

I don't use the elements of the array after in the program. I merely write in the array after the results, so I don't care what were its previous values.
No. It's in a loop, perhaps the real issue is the naming of your variables.

9. Each iteration alters the values of after and only after, and then I calculate the maximum difference with the previous values. So, I don't care if it's not exactly the same (and it's not), I am wondering why they don't do the same thing in this case.
I would like to avoid the memory copy of such a large array.

NOTE: As far as I am able to see in the code, the values of the array after before the loop which applies the jacob's algorithm are don't care values. In the loop the array takes the new values without any explicit use of the previous ones. That's why I am wondering about the difference in the results.

EDIT:
Originally Posted by zacs7
Every iteration you alternate before and after, that's not the same as setting before to after. Any reason why you're starting at '1' rather than 0 also?
These elements are considered as constant and I don't change their values. Also, you can observe that if I was starting at '0' I would be at of bounds.

Originally Posted by zacs7
No. It's in a loop, perhaps the real issue is the naming of your variables.