1

So the following code compiles with no warnings etc with gcc, but for some reason, the swap code doesn't actually modify the array by swapping the values...What could be going on here? One interesting thing is that tempalways contains what I want, it just doesn't get used.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

//STRUCTURES
struct complex_
{
  double re, im;
};

typedef struct complex_  complex;

//PROTOTYPES
int reverseBits(int x,int elements);
complex new_complex(double re, double im);

//MAIN
int main()
{
    int n,m,elements = 8;
    complex temp,**v;

    //Allocate memory for the struct array...
    v = malloc(sizeof(complex*));
    *v = malloc(sizeof(complex)*elements);

    //Initialize the struct array...
    for (n = 0; n < elements; n++)
    {
        (*v)[n] = new_complex(n,0);
    }

    //View the initialized struct array contents...
    for (n = 0; n < elements; n++){printf("%f+%fi\n", (*v)[n].re,(*v)[n].im);}

    //Swap elements for the bit reversal...
    for (n = 0; n < elements; n++)
    {
        m = reverseBits(n,elements);
        temp = (*v)[n];
        (*v)[n] = (*v)[m];
        (*v)[m] = temp;
    }

    //View the new swapped struct array contents...
    for (n = 0; n < elements; n++){printf("%f+%fi\n", (*v)[n].re,(*v)[n].im);}

    return 0;
}

//FUNCTION DEFINITIONS
int reverseBits(int x,int elements)
{
    //This function performs a binary bit reversal
    //for example 3 = 011 => 110 = 6...
    int num_bits = log2(elements);
    int reverse_x = 0;
    int i;

    for (i = 0; i < num_bits; i++)
    {
        if((x & (1 << i)))
            reverse_x |= 1 << ((num_bits - 1) - i);
    }
    return reverse_x;
}

complex new_complex(double re, double im)
{
    //This function creates a new complex number.
    complex r;
    r.re = re;
    r.im = im;
    return r;
}
3
  • 2
    You're not doing the allocation right. You're now first allocating a pointer to complex*, and then allocating an array of complex, which the first pointer points to. You'd better just allocate an array of complex* pointers - malloc(sizeof(complex*)*elements), and fill it using new_complex. Also, you should free all the allocated memory - first the complexes, and then the array. Commented Aug 17, 2011 at 6:29
  • The reason I've created a pointer to an array is because I will modify that array in other functions. Is this still a problem? Also, I agree--I should free the memory. Commented Aug 17, 2011 at 6:55
  • you can still pass around the array the way I suggest you create it. If you'd like some other function to create the array or replace it with another one, you can pass &v to it, and there do *v = some_other_complex_star_star. But if you'd like to change the array's content, you can pass it as is - v. Commented Aug 17, 2011 at 7:17

1 Answer 1

3

If you swap all items of the array once with the item at the "reversed" index, then you'll end up with the start state again. ie., for an array size of 8, these swaps are done :

  • swap item at index 0 with item at index 0
  • swap item at index 1 with item at index 4 (a)
  • swap item at index 2 with item at index 2
  • swap item at index 3 with item at index 6 (b)
  • swap item at index 4 with item at index 1 (a)
  • swap item at index 5 with item at index 5
  • swap item at index 6 with item at index 4 (b)
  • swap item at index 7 with item at index 7

Notice that the swaps marked with the same letter ((a) or (b)) cancel each other out, and the others are no-ops.

Sign up to request clarification or add additional context in comments.

2 Comments

That makes sense. What is the proper way to achieve the desired value swap, then?
@nick_name : what's the desired value swap ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.