Subj : Re: Why not unions To : comp.programming From : Phlip Date : Tue Jul 26 2005 08:38 pm Shwetabh wrote: > Hi, my question is related with data structures. > Why are only structures used for implementing data structures. > Why not unions when they are more memory efficient than structures. Because the most important resource to optimize is programmer time. Unions waste programmer time when they cause subtle bugs. Writing to one member and reading from another is the moral equivalent of a typecast, and the compiler will not stop you. Typechecks help programmers go faster by attempting to move the failure from runtime to compile time. So, don't typecast, don't use unions, and rely on practices that fail as early and loud as possible. Unions fail late and quiet. Google "premature optimization is the root of all evil". It's easier to make clean code fast than make fast code clean. If you guess while writing code what might be slow, you will write unecessarily complex code. Instead, write clean code that works, and measure how efficient it is. Measuring things gives you hard data. You can optimize only the parts of code that will have an effect on size or speed. To learn to use unions safely, where they solve a problem, research VARIANT. That is the "anything" type at the heart of all scripting languages. -- Phlip http://www.c2.com/cgi/wiki?ZeekLand .