difference between structure and union ,, can anyone help me?

Discussion in 'C++' started by manish sahu, Feb 13, 2008.

  1. manish sahu

    manish sahu Guest

    difference between structure and union ,, can anyone help me?
     
    manish sahu, Feb 13, 2008
    #1
    1. Advertising

  2. manish sahu

    Christopher Guest

    On Feb 13, 1:49 pm, manish sahu <> wrote:
    > difference between structure and union ,, can anyone help me?


    Union:
    http://msdn2.microsoft.com/en-us/library/5dxy4b7b(VS.80).aspx

    It essentially hold one data member which can be various types under
    various names, with one block of memory allocated for the largest
    type.
    IMHO you should never be using a union in C++

    Structure:
    Same thing as a class except everything is public by default

    IMHO you should never be using structures in C++, use a class instead.
    If you want a simple POD (plain old data) structure, make a class with
    public member data fields.

    To the best of my knowledge, Structure and Union are both remnants of
    C and support for them was included for backwards compatibility. Like
    I said, I don't think you should be using either one if you can help
    it.
     
    Christopher, Feb 13, 2008
    #2
    1. Advertising

  3. Christopher wrote:
    > On Feb 13, 1:49 pm, manish sahu <> wrote:
    >> difference between structure and union ,, can anyone help me?

    >
    > Union:
    > http://msdn2.microsoft.com/en-us/library/5dxy4b7b(VS.80).aspx
    >
    > It essentially hold one data member which can be various types under
    > various names, with one block of memory allocated for the largest
    > type.
    > IMHO you should never be using a union in C++


    Why? The requirement to save memory never comes up in what people do?

    > Structure:
    > Same thing as a class except everything is public by default
    >
    > IMHO you should never be using structures in C++, use a class instead.
    > If you want a simple POD (plain old data) structure, make a class with
    > public member data fields.


    So, you're against any short-hand? Do you use 'while' loops or 'for'
    or just use 'if' and 'goto'?

    > To the best of my knowledge, Structure and Union are both remnants of
    > C and support for them was included for backwards compatibility. Like
    > I said, I don't think you should be using either one if you can help
    > it.


    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Feb 13, 2008
    #3
  4. manish sahu

    Christopher Guest

    On Feb 13, 2:46 pm, "Victor Bazarov" <> wrote:
    > Christopher wrote:
    > > On Feb 13, 1:49 pm, manish sahu <> wrote:
    > >> difference between structure and union ,, can anyone help me?

    >
    > > Union:
    > >http://msdn2.microsoft.com/en-us/library/5dxy4b7b(VS.80).aspx

    >
    > > It essentially hold one data member which can be various types under
    > > various names, with one block of memory allocated for the largest
    > > type.
    > > IMHO you should never be using a union in C++

    >
    > Why? The requirement to save memory never comes up in what people do?


    Again, my opinion, but I'd rather have easily readable code than any
    of the obfuscated structures of unions of unions that I've commonly
    seen. Or the use of unions to do conversions of types, etc. 9 times
    out of 10 when you see a union in someone's code the rest is garbage,
    but I might just be jaded.


    >
    > > Structure:
    > > Same thing as a class except everything is public by default

    >
    > > IMHO you should never be using structures in C++, use a class instead.
    > > If you want a simple POD (plain old data) structure, make a class with
    > > public member data fields.

    >
    > So, you're against any short-hand? Do you use 'while' loops or 'for'
    > or just use 'if' and 'goto'?


    I don't know how the difference between using a struct and a class
    with public members can be remotely compared to while vs if and goto,
    but no, you will never see a goto statement in my code.

    If you see that as short hand, and that is your style, fine by me, but
    there is no functional reason that I can see to use one over the other
    and that is what I wanted to make apparent. In C there were no
    classes, there were structs. The _necessity_ to use a struct is no
    longer there in C++. We can use classes now. That's my opinion.
     
    Christopher, Feb 14, 2008
    #4
  5. manish sahu

    James Kanze Guest

    On Feb 13, 8:55 pm, Christopher <> wrote:
    > On Feb 13, 1:49 pm, manish sahu <> wrote:


    > > difference between structure and union ,, can anyone help me?


    > Union:http://msdn2.microsoft.com/en-us/library/5dxy4b7b(VS.80).aspx


    > It essentially hold one data member which can be various types
    > under various names, with one block of memory allocated for
    > the largest type. IMHO you should never be using a union in
    > C++


    Why not? They aren't common, but there are a few legitimate
    uses for them. If you're using a tool like yacc or bison, you
    don't have a choice:). (In practice, most of my unions are for
    different pointer types, but I do have one with double, int and
    some pointer types.)

    > Structure:
    > Same thing as a class except everything is public by default


    The keyword "struct" declares a class. There are no
    "structures" in C++. (For that matter, according to the
    standard, the keyword "union" also declares a class. A very
    special sort of class, however.)

    > IMHO you should never be using structures in C++, use a class
    > instead. If you want a simple POD (plain old data) structure,
    > make a class with public member data fields.


    Different people use different conventions. My convention is
    that I use the keyword struct if all of the data are public,
    even if the object isn't a POD. (My convention is also that
    either all of the data are public, or all are private.) Others
    have other conventions. I don't think that there is a
    universally accepted convention here. Choose one, and be
    consistent.

    > To the best of my knowledge, Structure and Union are both
    > remnants of C and support for them was included for backwards
    > compatibility.


    No, although many uses of union in C are better handled by
    inheritance in C++.

    > Like I said, I don't think you should be using either one if
    > you can help it.


    I use union's from time to time, and I also use the keyword
    struct. (I don't use structures, of course, because they don't
    exist in C++.)

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Feb 14, 2008
    #5
  6. manish sahu

    James Kanze Guest

    On Feb 14, 3:02 am, Christopher <> wrote:
    > On Feb 13, 2:46 pm, "Victor Bazarov" <> wrote:


    > > Christopher wrote:
    > > > On Feb 13, 1:49 pm, manish sahu <> wrote:
    > > >> difference between structure and union ,, can anyone help me?


    > > > Union:
    > > >http://msdn2.microsoft.com/en-us/library/5dxy4b7b(VS.80).aspx


    > > > It essentially hold one data member which can be various
    > > > types under various names, with one block of memory
    > > > allocated for the largest type. IMHO you should never be
    > > > using a union in C++


    > > Why? The requirement to save memory never comes up in what
    > > people do?


    > Again, my opinion, but I'd rather have easily readable code than any
    > of the obfuscated structures of unions of unions that I've commonly
    > seen. Or the use of unions to do conversions of types, etc. 9 times
    > out of 10 when you see a union in someone's code the rest is garbage,
    > but I might just be jaded.


    That's not a legal use of a union in either C or C++ (although
    most compilers support it as an extension). On the other hand,
    the need for such conversions is very, very rare, and they are
    intrinsically machine dependent, so it wouldn't bother me to use
    a compiler extension to implement them.

    Of course, there is a lot of code which uses such conversions
    when they aren't appropriate. But that's a design error, not a
    problem with union---if they'd done it by means of a
    reinterpret_cast, the code wouldn't be any better.

    As Victor said, unions are used (legitimately) to save memory.

    [...]
    > > So, you're against any short-hand? Do you use 'while' loops or 'for'
    > > or just use 'if' and 'goto'?


    > I don't know how the difference between using a struct and a class
    > with public members can be remotely compared to while vs if and goto,
    > but no, you will never see a goto statement in my code.


    I think his point was that underneath it all, a while is a short
    form of an if with a goto. It's probably not a good example,
    because of course, the reason you use the while isn't to save
    typing, but to indicate that you are using a very particular
    form of structured flow control, and not just anything that
    could be done with an if and a goto.

    Of course, the reason most people I know use struct rather than
    class is also to communicate some additional information about
    the program. The only difference here is that the information
    communicated depends on an arbitrary convention, and is not
    imposed by the language. But it is usually a useful
    distinction: is the class just a data container, for example,
    even if it contains constructors and maybe some other functions
    (that's my convention)?

    > If you see that as short hand, and that is your style, fine by
    > me, but there is no functional reason that I can see to use
    > one over the other and that is what I wanted to make apparent.


    There's no "functional" reason to prefer:

    while ( someCondition )
    doSomething ;

    over

    label1:
    if ( ! someCondition ) goto label2 ;
    doSomething ;
    goto label1 ;
    label2:

    I think that's what Victor wanted to make apparent.

    There are very definite stylistic reasons to prefer the first,
    however, and depending on local conventions, there can be
    stylistic reasons for preferring struct to class in some cases.

    > In C there were no classes, there were structs.


    And in C++, there are no structs, there are classes.
    Functionally, there was no need for Stroustrup to add the
    keyword class to the language. The fact that we have two
    keywords which mean almost the same thing does allow us to
    assign additional meaning, for the reader, to our choice of
    which one to use.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Feb 14, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jakk
    Replies:
    4
    Views:
    12,390
  2. csudha

    Difference between Structure & Union

    csudha, Sep 10, 2004, in forum: C Programming
    Replies:
    4
    Views:
    1,789
  3. Replies:
    9
    Views:
    25,406
    Lal Bahadur Singh
    Nov 11, 2011
  4. Nick Timkovich
    Replies:
    0
    Views:
    73
    Nick Timkovich
    Feb 25, 2014
  5. Skip Montanaro
    Replies:
    0
    Views:
    71
    Skip Montanaro
    Feb 25, 2014
Loading...

Share This Page