union and compiler directives

This is a discussion on union and compiler directives within the C Programming forums, part of the General Programming Boards category; Is there some compiler directive (like "no strict aliasing" or something else) which might prevent union fields to be overstored ...

  1. #1
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251

    union and compiler directives

    Is there some compiler directive (like "no strict aliasing" or something else) which might prevent union fields to be overstored in the most straight forward intuitive way?

    thank you

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    aliasing is just telling the compiler that memory pointed to by pointers may change memory pointed to by other pointers [so the pointer P1 is an alias for the memory block pointed to by P2]. It does not affect what and how things get stored in unions.

    What is the problem you are trying to solve, what does your union look like?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251
    Quote Originally Posted by matsp View Post
    aliasing is just telling the compiler that memory pointed to by pointers may change memory pointed to by other pointers [so the pointer P1 is an alias for the memory block pointed to by P2]. It does not affect what and how things get stored in unions.

    What is the problem you are trying to solve, what does your union look like?

    --
    Mats
    thank you
    I still have to read the code of a colleague which uses unions very much to interface different HW ports or devices or implement protocols but I dislike unions (personal taste) and have some worries about bugs hiding under the corner when changing platforms..
    I am not saying it's very likely to be a problem, it is just a fear..

  4. #4
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251
    basically union members overlapping can change depending on the platform (I guess, just because size of types change on different platf.) so using them relying on a specific overlap (as is done by my colleague) seems to me risky

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Unions, like many other "tools" have their benefits and their drawbacks. The purpose of a union is to make data of several formats be available at the same place - obviously, it's up to you to KNOW what the data represents, and which format can be used at which time. Unfortunately, any alternative to unions is at least as likely to fail at times - one of the obvious solutions is to use the address to the data [in a pointer] and then use casts to change the interpretation of the content. I think that, generally, means that the code gets harder to read.

    Of course, one alternative is to store the data spaced out in a struct instead, but if you have even a reasonable amount of alternatives, you can easily increase the space needed for the fields by several times, which is not a good idea [and you would still need to know which part to use at what time].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by mynickmynick View Post
    basically union members overlapping can change depending on the platform (I guess, just because size of types change on different platf.) so using them relying on a specific overlap (as is done by my colleague) seems to me risky
    Would you feel better if he used int_32t (or whatever fixed-width type is appropriate) instead of int?

  7. #7
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251
    Quote Originally Posted by tabstop View Post
    Would you feel better if he used int_32t (or whatever fixed-width type is appropriate) instead of int?
    well that works for POSIX
    what about Microsoft?

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by mynickmynick View Post
    well that works for POSIX
    what about Microsoft?
    The same thing works - you may have to make your own typedef for int32_t (etc), but the concept holds true.

    ALWAYS use your own types if size of the type matters.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Union Question
    By estarapapax in forum C Programming
    Replies: 0
    Last Post: 10-21-2008, 10:08 AM
  2. Replies: 16
    Last Post: 10-29-2006, 04:04 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Reading game port with a simple compiler like BuilderX?
    By MaxxMan-X in forum Game Programming
    Replies: 7
    Last Post: 02-27-2005, 10:15 AM
  5. Union In Malloc Sort
    By Delboy in forum C Programming
    Replies: 0
    Last Post: 10-16-2001, 06:15 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21