Array of objects as array of POD

R

rajkumar

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
 
T

Thomas Matthews

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
 
V

Victor Bazarov

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

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top