Thread: Help understanding unions

  1. #1
    Registered User
    Join Date
    May 2009
    Location
    Look in your attic, I am there...
    Posts
    92

    Help understanding unions

    I was reading through a C++ template whose purpose was to allocate memory blocks (_type_ is the generic type passed). It seems to me the purpose of this code would be to create a byte aligned buffer to store a _type_ object, but I am confused as to why, since buffer is not a pointer, the other elements are not.

    Code:
    #define CONST_MAX( x, y )  ( (x) > (y) ? (x) : (y) )
    #define BLOCK_ALLOC_ALIGNMENT 16
    ...
    union element_t{
      _type_*    data; // this is a hack to make sure the save game system marks _type_ as saveable
      element_t* next;
      byte       buffer[(CONST_MAX(sizeof( _type_ ), sizeof(element_t *)) + (BLOCK_ALLOC_ALIGNMENT - 1)) & ~(BLOCK_ALLOC_ALIGNMENT - 1)];
    };

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    buffer is an array that is large enough to contain a _type_ or a pointer to element_t (plus additional alignment).

    At a guess, the code involves compiler-specific trickery, given the comment.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    May 2009
    Location
    Look in your attic, I am there...
    Posts
    92
    Why would you want to allocate a buffer of size _type_ (plus padding) if the first couple bytes were to be interpreted as a pointer?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    I take it that this is some code you "found on the net" somewhere. If it is, then telling us where you got it would allow those of us curious enough to go and find a hell of a lot more context for your question. Because 5 lines with one meaningless comment just doesn't cut it.

    For example, if it came from a project on one of the source repositories like sourceforge, then you should be able to find out which author put that line of code in there, then go and ask them directly what the idea is.
    Likewise, if there is any kind of forum for that project, then that forum would be a really good place to ask.

    An alternative theory is the original author doesn't know what they're doing at all, and that line of code can easily be attributed to
    Cargo cult programming - Wikipedia, the free encyclopedia

    These two sentences are near anagrams, and sum up the situation nicely
    "There is an awful lot of code on the net"
    "There is a lot of awful code on the net"
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    May 2009
    Location
    Look in your attic, I am there...
    Posts
    92
    Haha, that is quite a turn of phrase there. But this code is actually from the Doom 3 BFG GPL released recently so I assume its organization has a purpose.

    A link to the file: https://github.com/id-Software/DOOM-...o/idlib/Heap.h
    The line in question is #207. Although the CONST_MAX macro was from a different file.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Having written pretty much the exact same thing many years ago, I recognise this as the internals of a free-list tracking type of thing. The memory is either used for an object or for a free-list pointer.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User
    Join Date
    May 2009
    Location
    Look in your attic, I am there...
    Posts
    92
    Quote Originally Posted by iMalc View Post
    Having written pretty much the exact same thing many years ago, I recognise this as the internals of a free-list tracking type of thing. The memory is either used for an object or for a free-list pointer.
    Ah, so if I understand this correctly the union could have been a structure with 2 members (a pointer and an element of _type_), but to save memory this was not done since the structure would only contain one of those at any one time.

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by 127.0.0.1 View Post
    Ah, so if I understand this correctly the union could have been a structure with 2 members (a pointer and an element of _type_), but to save memory this was not done since the structure would only contain one of those at any one time.
    Exactly!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    Registered User
    Join Date
    May 2009
    Location
    Look in your attic, I am there...
    Posts
    92
    Thanks, iMalc I guess the mystery is solved now!

    Just one more quick question, do you happen to know of any resources I could look at to further understand how this object (or objects that share a similar function) works, and why it was designed the way it was? I have a general understanding from the source code, but I would love to see a formal write-up.
    Last edited by 127.0.0.1; 12-03-2012 at 05:16 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unions - Understanding
    By Vber in forum C Programming
    Replies: 2
    Last Post: 04-14-2003, 08:16 AM
  2. unions
    By mickle in forum C Programming
    Replies: 6
    Last Post: 02-27-2003, 11:46 PM
  3. Unions in c++
    By The Gweech in forum C++ Programming
    Replies: 5
    Last Post: 08-06-2002, 08:14 AM
  4. Unions?
    By hostensteffa in forum C++ Programming
    Replies: 3
    Last Post: 06-08-2002, 06:01 AM
  5. Unions
    By coug2197 in forum C++ Programming
    Replies: 1
    Last Post: 05-02-2002, 12:41 PM