Thread: Unions of the same size and pointers

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    12

    Unions of the same size and pointers

    So I have two union typedefs:

    Code:
    typedef union LOWFIXED20_12  
    {
    	int full;
    	struct lowPart20_12
    	{
    		int fraction: 12,
    			integer: 4;
    	};
    };
    
    typedef union MIDFIXED20_12
    {
    	int full;
    	struct midPart20_12
    	{
    		int fraction: 8,
    			integer: 8;
    	};
    };
    And I have 4 arrays, 2 of each type. I also have a pointer to each array. I have a function that "swaps" the arrays by simply just exchanging the pointers.

    Considering that both of these unions are of the same size, int, I was wondering if there was a way to write one function that would take a pointer of either type . . . or do I have to write two separate functions because these are technically of different types?

    I know the function is small and fast, but I guess it was more of general question than something that is important to what I am doing.

    Thanks

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, but how would your function know which one it had?

    (Unless it only ever uses the int part, in which case just pass the int.)

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Why not just have a function that takes a void *?

    Assuming everything will line up right, which given how your structures are designed that seems mighty overly-presumptuous.

    Example:
    Code:
    int swap(void *a, void *b, size_t size)
    {
      void *tmp = malloc(size);
    
      if(tmp)
      {
        memcpy(tmp, a, size);
        memcpy(a, b, size);
        memcpy(b, tmp, size);
        free(tmp);
        return 1;
      }
      return 0;
    }

  4. #4
    Registered User
    Join Date
    Jul 2008
    Posts
    12

    Wink

    Wow, thanks for the fast replies.

    I guess I could just use the int part, that seems to make the most sense to me and, for input into the array, I am only using the int part.

    And master5001 thanks for pointing out the void *, I hadn't considered it. But, why are you concerned about how it will line up?

    But I think you might be confused. I am still fairly new to programming, but it seems like you are copying the entire array, which is not what I am trying to do. I am simply swapping the pointers to the array. Copying the entire array eats up too much time.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    He's worried about the memory line up, since just copying bits changes the meaning -- fraction won't line up with fraction and integer won't line up with integer.

    The reason he wrote it that way is that swapping pointers doesn't swap the arrays. Example:
    Code:
    int a[5] = {1,2,3,4,5};
    int b[5] = {6,7,8,9,10};
    int *c = a;
    int *d = b;
    
    //swap c and d;
    int *temp = d;
    d = c;
    c = temp;
    
    if (a[2] != 8) {
        printf("Didn't work. :(\n");
    }

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I write general purpose code on the board most of the time. My function is far from optimal for this purpose. The good news is you could have it swap two of anything. Whether it is a char being swapped with a char, or an array of 50 floats being swapped with another array of 50 floats.

    Your int values won't necessarily translate across the way you are wanting.

    If you do LOWFIXED20_12.full = MIDFIXED20_12.full instead of copying the fraction to the fraction and the integer to the integer you will end up with the bits that make up those numbers out of whack.

    The integer part of LOWFIXED20_12 is 12bits unlike the MIDFIXED20_12 integer, which is 8 bits. So both the quantity that these numbers can store and the memory locations of them within the structure are physically different.

  7. #7
    Registered User
    Join Date
    Jul 2008
    Posts
    12
    Ah, I see what you are driving at and your function makes more sense to me now.

    Thanks again for the help.

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    For what you are doing I wouldn't use my function since it is doing allocations (which is slow) and not going to keep your alignments correct.

    The unfortunate toils of fixed point math. Which is what I assume you are using these things for.

  9. #9
    Registered User
    Join Date
    Jul 2008
    Posts
    12
    Yup, good old fixed point. The "best" part about it is that I really only have the time to do the math on 16 bit variables and the additions, at one point, lead to values that way exceed what I can hold in 16 bits. Thus the reason why I have multiple FIXED20_12 definitions (I actually have a third as well) that are only 16 bits.

    Its frustrating because, of course, we are on a deadline and I kind of just got stuck with the programming (I am a digital designer by trade, but I work in a small company and they said "oh look, you can program a little, you do this") and I have been developing this AND the GUI (with .net) over the past year. . .and now that I am coming back to code I wrote way at the beginning of my real coding experience I constantly think "wtf was I thinking?" Of course I don't really have the time to go back and fix it all.

    Honestly, I just made a function to swap each one individually. Code size doesn't appear to be a problem in my design, while time certainly is.

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Yeah stick to the individual swaps. Alas that is the only way to retain your bits correctly. Anything else will screw you up.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 05-21-2009, 05:47 PM
  2. Changing size of array of pointers
    By carrotcake1029 in forum C Programming
    Replies: 1
    Last Post: 11-15-2008, 10:24 PM
  3. Pointers trouble
    By saahmed in forum C Programming
    Replies: 39
    Last Post: 03-24-2006, 04:08 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM