Re: A question about variables declaration in C++

Discussion in 'C++' started by Greg Comeau, Jul 30, 2003.

  1. Greg Comeau

    Greg Comeau Guest

    In article <bg7n3d$irl$>, Dog <> wrote:
    >Hi,
    >A question about variables declaration.
    >There're 3 variables declared in a class.
    >Which one is better? Why?
    >
    >(A)
    >{
    > int i1;
    > double d;
    > int i2;
    >}
    >
    >(B)
    >{
    > int i1;
    > int i2;
    > double d;
    >}
    >
    >(C)
    >{
    > double d;
    > int i1;
    > int i2;
    >}
    >
    >I've no idea about this.
    >Could you please provide me some hints.



    There may be some mild alignment or struct packing considerations
    you may have (which would favor C since a double is usually
    larger than an int, and with a most severe alightment constraint),
    but generally speaking, no way is better.

    BTW, this is not a C question, so you shouldn't post it
    to comp.lang.c
    --
    Greg Comeau/ 4.3.0.1: FULL CORE LANGUAGE, INCLUDING TC1
    Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
    World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
    Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
     
    Greg Comeau, Jul 30, 2003
    #1
    1. Advertising

  2. Greg Comeau

    Dan Pop Guest

    In <bg8f0a$cko$> (Greg Comeau) writes:

    >In article <bg7n3d$irl$>, Dog <> wrote:
    >>Hi,
    >>A question about variables declaration.
    >>There're 3 variables declared in a class.
    >>Which one is better? Why?
    >>
    >>(A)
    >>{
    >> int i1;
    >> double d;
    >> int i2;
    >>}
    >>
    >>(B)
    >>{
    >> int i1;
    >> int i2;
    >> double d;
    >>}
    >>
    >>(C)
    >>{
    >> double d;
    >> int i1;
    >> int i2;
    >>}
    >>
    >>I've no idea about this.
    >>Could you please provide me some hints.

    >
    >There may be some mild alignment or struct packing considerations
    >you may have (which would favor C since a double is usually
    >larger than an int, and with a most severe alightment constraint),
    >but generally speaking, no way is better.
    >
    >BTW, this is not a C question, so you shouldn't post it
    >to comp.lang.c


    Substitute "class" by "structure" and it becomes a C question :)

    The issue is exactly the same in both languages. It is preferrable to
    put the wider types at the beginning, but it's not always obvious which
    type is wider. An educated guess would favour the following order for
    modern implementations:

    long double
    double
    long
    pointer
    float
    int
    short
    char

    long long (if supported) should be either right above or right below
    double.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Jul 30, 2003
    #2
    1. Advertising

  3. Greg Comeau

    Greg Comeau Guest

    In article <bg8nde$hnn$>, Dan Pop <> wrote:
    >In <bg8f0a$cko$> (Greg Comeau) writes:
    >>In article <bg7n3d$irl$>, Dog <> wrote:
    >>>A question about variables declaration.
    >>>There're 3 variables declared in a class.
    >>>Which one is better? Why?
    >>>(A)
    >>>{
    >>> int i1;
    >>> double d;
    >>> int i2;
    >>>}
    >>>(B)
    >>>{
    >>> int i1;
    >>> int i2;
    >>> double d;
    >>>}
    >>>(C)
    >>>{
    >>> double d;
    >>> int i1;
    >>> int i2;
    >>>}
    >>>I've no idea about this.
    >>>Could you please provide me some hints.

    >>
    >>There may be some mild alignment or struct packing considerations
    >>you may have (which would favor C since a double is usually
    >>larger than an int, and with a most severe alightment constraint),
    >>but generally speaking, no way is better.
    >>
    >>BTW, this is not a C question, so you shouldn't post it
    >>to comp.lang.c

    >
    >Substitute "class" by "structure" and it becomes a C question :)


    Maybe. I'll play for now though :) ....

    >The issue is exactly the same in both languages. It is preferrable to
    >put the wider types at the beginning, but it's not always obvious which
    >type is wider. An educated guess would favour the following order for
    >modern implementations:
    >
    > long double
    > double
    > long
    > pointer
    > float
    > int
    > short
    > char
    >
    >long long (if supported) should be either right above or right below
    >double.


    .... so in that case (this being a C and C++ question), don't
    forget _Bool/bool, enum's, the ever lovely _Complex/_Imaginary
    and the always dashing bitfield. :)
    --
    Greg Comeau/ 4.3.0.1: FULL CORE LANGUAGE, INCLUDING TC1
    Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
    World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
    Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
     
    Greg Comeau, Jul 30, 2003
    #3
    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. Noah
    Replies:
    5
    Views:
    992
  2. Alex Vinokur
    Replies:
    4
    Views:
    632
    Jonathan Turkanis
    Apr 5, 2004
  3. Ovidesvideo
    Replies:
    4
    Views:
    557
    Andrey Tarasevich
    Dec 10, 2004
  4. kelvSYC
    Replies:
    6
    Views:
    7,374
    Richard Herring
    May 17, 2005
  5. Replies:
    4
    Views:
    1,135
    Richard Tobin
    Dec 12, 2006
Loading...

Share This Page