Thread: swap dynamically allocated arrays

  1. #1
    Registered User
    Join Date
    Apr 2007
    Location
    Greece
    Posts
    52

    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. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    >The second try seems not to work properly.

    How so?

  3. #3
    Registered User
    Join Date
    Apr 2007
    Location
    Greece
    Posts
    52
    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. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    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. #5
    Registered User
    Join Date
    Apr 2007
    Location
    Greece
    Posts
    52
    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. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    But as you can see, I don't use the elements of the array after in the program
    Indeed, I misread the code, please ignore my comment on that.

    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. #7
    Registered User
    Join Date
    Apr 2007
    Location
    Greece
    Posts
    52
    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.
    Last edited by myle; 05-18-2008 at 05:43 AM.

  8. #8
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    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.
    Last edited by zacs7; 05-18-2008 at 05:52 AM.

  9. #9
    Registered User
    Join Date
    Apr 2007
    Location
    Greece
    Posts
    52
    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:
    Quote Originally Posted by zacs7 View Post
    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.


    Quote Originally Posted by zacs7 View Post
    No. It's in a loop, perhaps the real issue is the naming of your variables.
    I don't get your point.
    Last edited by myle; 05-18-2008 at 06:04 AM.

  10. #10
    Registered User
    Join Date
    Apr 2007
    Location
    Greece
    Posts
    52
    Problem solved.
    I have found the error. The swapping had been done fine. The problem was in the initialization of the two arrays. I wasn't careful with the values at the first and last row/column.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing dynamicaly allocated 2d arrays
    By s_siouris in forum C Programming
    Replies: 6
    Last Post: 05-25-2008, 04:19 PM
  2. Deleting Dynamically Allocated Array Members
    By theJ89 in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2006, 07:37 PM
  3. Dynamically allocated memory question
    By caduardo21 in forum C Programming
    Replies: 3
    Last Post: 01-13-2006, 07:37 PM
  4. Run-time error with dynamically allocated 3-D array
    By OceanDesigner in forum C Programming
    Replies: 2
    Last Post: 10-21-2005, 02:29 PM
  5. Dynamically allocated arrays
    By axe786 in forum C Programming
    Replies: 6
    Last Post: 12-03-2004, 01:41 AM