473,324 Members | 2,400 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

structs of double as double[]

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?
Jul 22 '05 #1
4 1301
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

Jul 22 '05 #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, 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
Jul 22 '05 #3

"Default User" <fi********@boeing.com.invalid> wrote in message
news:40***************@boeing.com.invalid...
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

Jul 22 '05 #4
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
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Roman Hartmann | last post by:
hello, I do have a question regarding structs. I have a struct (profil) which has a pointer to another struct (point). The struct profil stores the coordinates of points. The problem is that I...
3
by: Christian F | last post by:
Hi, I'm a C-newbie and I would like to know if I am doing something wrong in the code below. It is working, but I'm afraid it might not be correct because I don't really understand everything of...
5
by: Gomaw Beoyr | last post by:
Hello Is there any explanation why Microsoft chose to implement arrays as objects allocated on the heap instead of structs allocated on the stack? For "mathematical stuff", one normally...
17
by: goldfita | last post by:
I saw some code that appeared to do something similar to this struct foo { char offset; int d; }; struct foo { int a; int b;
5
by: Bidule | last post by:
Hi, I'm trying to sort structs defined as follows: struct combinationRec { float score; char* name; }; The number of structs and the length of the "name" field are not known
61
by: Marty | last post by:
I am new to C# and to structs so this could be easy or just not possible. I have a struct defined called Branch If I use Branch myBranch = new Branch(i); // everything works If I use Branch...
4
by: veera sekhar kota | last post by:
I read structs are stored at stack area or inline-heap based objects. What is meant by inline-heap based objects? I didnt get that. Thanks, Veera.
12
by: Venkataramana | last post by:
Hi Can anyone tell me how C# allocates memory for structs in C#? I am having problems figuring out how much memory will be allocated. Consider the following snippet struct S1 { int i;...
29
by: Dom | last post by:
I'm really confused by the difference between a Struct and a Class? Sometimes, I want just a group of fields to go together. A Class without methods seems wrong, in that it carries too much...
13
by: JohnQ | last post by:
The implementation of classes with virtual functions is conceptually easy to understand: they use vtables. Which begs the question about POD structs: how are they associated with their member...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.