structs of double as double[]

S

sb

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?
 
T

Thomas Matthews

sb said:
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
 
D

Default User

sb said:
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
 
N

Nick Hounsome

Default User said:
sb said:
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]), ..... {}
};
 
A

Alberto Barbati

Nick said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top