structs of double as double[]

Discussion in 'C++' started by sb, Feb 19, 2004.

  1. sb

    sb Guest

    Are there any guarantees in the language that, for example

    the members of

    struct s {
    double a;
    double b;
    double c;
    };

    can be accessed (after casting) as the elements of double[3] in the
    order they are declared?
    sb, Feb 19, 2004
    #1
    1. Advertising

  2. sb wrote:
    > Are there any guarantees in the language that, for example
    >
    > the members of
    >
    > struct s {
    > double a;
    > double b;
    > double c;
    > };
    >
    > can be accessed (after casting) as the elements of double[3] in the
    > order they are declared?


    No. The compiler is allowed to add "padding" bytes
    between structure/class members.

    A rule: The size of a structure may not be equal to
    the sum of the size of its members.

    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.raos.demon.uk/acllc-c /faq.html
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
    http://www.sgi.com/tech/stl -- Standard Template Library
    Thomas Matthews, Feb 19, 2004
    #2
    1. Advertising

  3. sb

    Default User Guest

    sb wrote:
    >
    > Are there any guarantees in the language that, for example
    >
    > the members of
    >
    > struct s {
    > double a;
    > double b;
    > double c;
    > };
    >
    > can be accessed (after casting) as the elements of double[3] in the
    > order they are declared?



    No, and if you are trying to do that then you probably have a serious
    design flaw. If you need the values in the form of an array, put them in
    an array:

    struct s {
    double data[3];
    };



    Brian Rodenborn
    Default User, Feb 19, 2004
    #3
  4. "Default User" <> wrote in message
    news:...
    > sb wrote:
    > >
    > > Are there any guarantees in the language that, for example
    > >
    > > the members of
    > >
    > > struct s {
    > > double a;
    > > double b;
    > > double c;
    > > };
    > >
    > > can be accessed (after casting) as the elements of double[3] in the
    > > order they are declared?

    >
    >
    > No, and if you are trying to do that then you probably have a serious
    > design flaw. If you need the values in the form of an array, put them in
    > an array:
    >
    > struct s {
    > double data[3];
    > };


    And if you also need them as a,b,c just add inline access methods
    OR
    if you have plenty of space add a ctor and references (ugh!)
    struct s
    {
    double data[3];
    double& a;
    double& b;
    double& c;
    s() : a(&data[0]), ..... {}
    };
    >
    >
    >
    > Brian Rodenborn
    Nick Hounsome, Feb 19, 2004
    #4
  5. Nick Hounsome wrote:
    > And if you also need them as a,b,c just add inline access methods
    > OR
    > if you have plenty of space add a ctor and references (ugh!)
    > struct s
    > {
    > double data[3];
    > double& a;
    > double& b;
    > double& c;
    > s() : a(&data[0]), ..... {}
    > };


    If care about memory but you don't care about performances you can write
    this:

    struct s {
    double a;
    double b;
    double c;

    double& operator[](int n)
    {
    return n == 0 ? a : n == 1 ? b : c;
    }

    double operator[](int n) const
    {
    return n == 0 ? a : n == 1 ? b : c;
    }
    };

    If you invoke [] with constants, the compiler may be able to optimize
    the tests away so you shouldn't even incur in a performance loss.

    Alberto
    Alberto Barbati, Feb 19, 2004
    #5
    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. Sydex
    Replies:
    12
    Views:
    6,488
    Victor Bazarov
    Feb 17, 2005
  2. Patricia  Van Hise

    structs with fields that are structs

    Patricia Van Hise, Apr 5, 2004, in forum: C Programming
    Replies:
    5
    Views:
    635
    Al Bowers
    Apr 5, 2004
  3. Chris Hauxwell

    const structs in other structs

    Chris Hauxwell, Apr 23, 2004, in forum: C Programming
    Replies:
    6
    Views:
    557
    Chris Hauxwell
    Apr 27, 2004
  4. Paminu
    Replies:
    5
    Views:
    640
    Eric Sosman
    Oct 11, 2005
  5. Daniel Rudy
    Replies:
    15
    Views:
    1,395
    Keith Thompson
    Apr 10, 2006
Loading...

Share This Page