size and order of declaration

Discussion in 'C++' started by Gary Wessle, Dec 29, 2006.

  1. Gary Wessle

    Gary Wessle Guest

    Hi

    I read some where about the order variables are declared in a class
    may help space optimization.
    find the size of bits a variable takes and declare them from biggest
    to the lowest.
    so in my system that would be.

    double
    int
    string
    char
    bool

    is the principle correct? if so, then at the top of the list should be
    things like
    vector<double>
    vector<int>
    ....
    and same for map(s) and so on...
    but what about function declaration?
    where do then fit, what is the size of a function declaration?

    thanks
    Gary Wessle, Dec 29, 2006
    #1
    1. Advertising

  2. Gary Wessle wrote:
    > Hi
    >
    > I read some where about the order variables are declared in a class
    > may help space optimization.
    > find the size of bits a variable takes and declare them from biggest
    > to the lowest.
    > so in my system that would be.
    >
    > double
    > int
    > string
    > char
    > bool


    bool is often implemented as an int. So it should go next to int.

    >
    > is the principle correct?


    No, not quite. It's the alignment of your type that is important, not
    the size.


    .... if so, then at the top of the list should be
    > things like
    > vector<double>
    > vector<int>
    > ...
    > and same for map(s) and so on...
    > but what about function declaration?


    The alignment of a vector<double> is likely to be exactly the same as
    vector<int>.

    > where do then fit, what is the size of a function declaration?


    I don't think it will ever matter to you what the alignment of a
    function is.
    Gianni Mariani, Dec 29, 2006
    #2
    1. Advertising

  3. Gary Wessle

    John Carson Guest

    "Gary Wessle" <> wrote in message
    news:
    > Hi
    >
    > I read some where about the order variables are declared in a class
    > may help space optimization.
    > find the size of bits a variable takes and declare them from biggest
    > to the lowest.
    > so in my system that would be.
    >
    > double
    > int
    > string
    > char
    > bool
    >
    > is the principle correct?


    Usually, but it depends on the alignment requirements of your system (which
    you may be able to alter with a compiler switch). Almost certainly, it will
    do no harm space-wise to organise your code this way.

    > if so, then at the top of the list should be
    > things like
    > vector<double>
    > vector<int>


    That depends on sizeof(vector<int>) vs sizeof(vector<double>). On my system
    they are the same size, notwithstanding that double is twice as large as
    int. This is because storage for vectors is allocated on the heap, not in
    the class object, which only stores a pointer.

    > ...
    > and same for map(s) and so on...
    > but what about function declaration?
    > where do then fit, what is the size of a function declaration?


    Function declarations/definitions do not add to the size of an object
    (except that if you have at least one virtual function, then your class
    object will have a vtable pointer --- or at least that is how it is normally
    implemented). Functions exist at the level of the class, not at the level of
    the individual object.

    I would quote Stroustrup on this whole issue.

    "However, it is usually best to order members for readability and sort them
    by size only if there is a demonstrated need to optimize." (TCPL, Special
    3rd ed., p. 103).

    --
    John Carson
    John Carson, Dec 29, 2006
    #3
  4. Gary Wessle

    Gary Wessle Guest

    Gianni Mariani <> writes:

    > Gary Wessle wrote:
    > > Hi
    > > I read some where about the order variables are declared in a class
    > > may help space optimization.
    > > find the size of bits a variable takes and declare them from biggest
    > > to the lowest.
    > > so in my system that would be.
    > > double
    > > int
    > > string
    > > char
    > > bool

    >
    > bool is often implemented as an int. So it should go next to int.
    >
    > > is the principle correct?

    >
    > No, not quite. It's the alignment of your type that is important, not
    > the size.
    >


    Stroustrup 3rd ed. top of p. 103
    "You can minimize wasted space by simply ordering members by size
    (largest member first)."

    is this statement correct only inside the structure and not for
    general use?

    thanks
    Gary Wessle, Dec 29, 2006
    #4
  5. Gary Wessle

    John Carson Guest

    "Gary Wessle" <> wrote in message
    news:
    > Gianni Mariani <> writes:
    >
    >> Gary Wessle wrote:
    >>> Hi
    >>> I read some where about the order variables are declared in a class
    >>> may help space optimization.
    >>> find the size of bits a variable takes and declare them from biggest
    >>> to the lowest.
    >>> so in my system that would be.
    >>> double
    >>> int
    >>> string
    >>> char
    >>> bool

    >>
    >> bool is often implemented as an int. So it should go next to int.
    >>
    >>> is the principle correct?

    >>
    >> No, not quite. It's the alignment of your type that is important,
    >> not the size.
    >>

    >
    > Stroustrup 3rd ed. top of p. 103
    > "You can minimize wasted space by simply ordering members by size
    > (largest member first)."
    >
    > is this statement correct only inside the structure and not for
    > general use?
    >
    > thanks


    It is not clear what you mean by "general use". However, an implementation
    is only required to store members in the order in which they are declared
    when there are no intervening access specifiers (section 9.2/12 of the
    Standard). Thus given:

    class Test
    {
    private:
    double d;
    int i;
    public:
    bool b;
    };

    d must precede i, but the access specifier that occurs between d and i, on
    the one hand, and b on the other hand, means that the placement of b
    relative to d and i could be different from the order of declaration.
    Specifically, b might come first.

    --
    John Carson
    John Carson, Dec 29, 2006
    #5
  6. Gary Wessle

    Jim Langston Guest

    "Gary Wessle" <> wrote in message
    news:...
    > Hi
    >
    > I read some where about the order variables are declared in a class
    > may help space optimization.
    > find the size of bits a variable takes and declare them from biggest
    > to the lowest.
    > so in my system that would be.
    >
    > double
    > int
    > string
    > char
    > bool
    >
    > is the principle correct? if so, then at the top of the list should be
    > things like
    > vector<double>
    > vector<int>
    > ...
    > and same for map(s) and so on...
    > but what about function declaration?
    > where do then fit, what is the size of a function declaration?
    >
    > thanks


    Yes and no. Consider a structure (or class) such as:

    class MyClass
    {
    char Char1;
    int Int1;
    char Char2;
    };

    What is the size of that class?

    (on my system with sizeof int being 4)
    Well, the compiler will allocate one byte for the char, then skip 3 bytes so
    the int is aligned properly, then 4 bytes for the int, then 1 byte for the
    second char, then skip 3 more bytes so an array of this class will be
    aligned properly. So 12 bytes, when only 6 are actually used.

    So we can arrange it and make the largest first so it would become:

    class MyClass
    {
    int Int1;
    char Char1;
    char Char2;
    };

    What's the size of this class?

    4 bytes for the int, 1 byte for the first char, 1 byte for the 2nd char,
    skip 2 bytes for alignment. 8 bytes total.

    But we get the same result with:

    class MyClass
    {
    char Char1;
    char Char2;
    int Int1;
    };

    1 byte, 1 byte, skip 2, 4 bytes.

    Generally, the compler will align a type on it's size. If a variable is 4
    bytes, it will align it every 4 bytes. 2 bytes, 2, etc... I said
    generally, there may be exceptions with OS/compilers/CPUs, etc...

    As long as you understand what's going on it should be relatively easy to
    figure out how to get rid of wasted space. I.E. This is extremly wasteful:

    class MyClass
    {
    char Type1;
    int Val1;
    char Type2;
    int Val2;
    char Type3;
    int Val3;
    };

    24 bytes.

    A little rearranging:

    class MyClass
    {
    char Type1;
    char Type2;
    char Type3;
    int Val1;
    int Val2;
    int Val3;
    };

    16 bytes. I could of put the ints first.

    However, I tend to arrange my class variables on their usage, grouping like
    with like. Just use a little common sense and things should be fine.
    Jim Langston, Dec 29, 2006
    #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. Neil Zanella
    Replies:
    1
    Views:
    477
    Jonathan Bromley
    Oct 17, 2003
  2. Bhushit Joshipura
    Replies:
    18
    Views:
    504
    David Harmon
    Jan 19, 2004
  3. kelvSYC
    Replies:
    6
    Views:
    7,209
    Richard Herring
    May 17, 2005
  4. Bhushit Joshipura

    Order of evaluation and better declaration of functions

    Bhushit Joshipura, Jan 4, 2004, in forum: C Programming
    Replies:
    26
    Views:
    585
    August Derleth
    Jan 21, 2004
  5. Jason Cavett

    Preferred Size, Minimum Size, Size

    Jason Cavett, May 23, 2008, in forum: Java
    Replies:
    5
    Views:
    12,523
    Michael Jung
    May 25, 2008
Loading...

Share This Page