By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,411 Members | 1,023 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,411 IT Pros & Developers. It's quick & easy.

Array of objects as array of POD

P: n/a
hey all

While maintaining some legacy code I came across such a class

class Point
{
double coordinate[3];
};

This class doesn't have any virtual functions and is aligned by
default without any padding bits such that sizeof(Point)=
3*sizeof(double).

In several places a pointer to this class is interchangebly used as
double array of size 3. Is this undefined behaviour ? Even if it is I
guess I can fix by defining double* operator and making sure Point* is
not used as double* and Point is used as double*

Also huge arrays of Points are passed as 3 * array of doubles to
other functions that expect double arrays? Is this legal according to
standard ? If so, can this made legal without actually making a copy ?

Any reference to the standard would be greatly appreciated.
Thanks
Raj

Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
ra******@hotmail.com wrote:
hey all

While maintaining some legacy code I came across such a class

class Point
{
double coordinate[3];
};

This class doesn't have any virtual functions and is aligned by
default without any padding bits such that sizeof(Point)=
3*sizeof(double).

In several places a pointer to this class is interchangebly used as
double array of size 3. Is this undefined behaviour ? Even if it is I
guess I can fix by defining double* operator and making sure Point* is
not used as double* and Point is used as double*

Also huge arrays of Points are passed as 3 * array of doubles to
other functions that expect double arrays? Is this legal according to
standard ? If so, can this made legal without actually making a copy ?

Any reference to the standard would be greatly appreciated.
Thanks
Raj


Here are two suggestions:
1. Convert the class into a typedef.

2. Convert all instances of point as an array to type Point.

I would not rely on the array of doubles being the same as
the class. A typedef may be safer for legacy code, since
this is a structure of POD, and no member functions. However,
a typedef won't allow expansion, but that is up to the
future of the legacy code.

--
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.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
Jul 23 '05 #2

P: n/a
Thomas Matthews wrote:
ra******@hotmail.com wrote:
hey all

While maintaining some legacy code I came across such a class

class Point
{
double coordinate[3];
};

Here are two suggestions:
1. Convert the class into a typedef.


Which will make it non-copy-constructible and non-assignable, which
will render it useless with standard containers.
2. Convert all instances of point as an array to type Point.

I would not rely on the array of doubles being the same as
the class. A typedef may be safer for legacy code, since
this is a structure of POD
Really? It seems that 'coordinate' is a private member. Is it still
POD if it has private members?
, and no member functions. However,
a typedef won't allow expansion, but that is up to the
future of the legacy code.


V
Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.