M
matt
Dear all,
I'm not actually working with the data struct that I'm going to use
in my example, indeed the data structs which I work with are by far more
complex, but I think it should easily show the point.
Let us suppose that I want implement a library handling (among other
things) points in 3D space. I would choose between the 2 following
implementations:
1.
typedef struct point3d_ {
double x;
double y;
double z;
} point3d;
2.
typedef struct point3d_ {
double data[3];
} point3d;
Let us say that I choose solution 2, but also that I want write my
code so that I can change the internal point3d representation in future,
without having to change the whole code I (and other users) wrote. For
this purpose, I would define some macros like
Point3DInit(point, xval, yval, zval) /* initialize the 3D point */
Point3DSetX(point, xval) /* set the value xval in the X-component
of point */
Point3DSetY(point, yval) /* ... */
Point3DSetZ(point, zval) /* ... */
Point3DExtX(xval, point) /* extract the X-component of point and
put its value in xval */
Point3DExtY(yval, point) /* ... */
Point3DExtZ(zval, point) /* ... */
which, of course, vary as the internal implementation of point3d change.
My question is: which would be, in your opinion, the best way to
consider the variable point in the macros above? A point3d *object* or a
*pointer* to point3d objects?
In my opinion, to consider the variable point a *pointer* to point3d
objects would be the most suitable solution, since it would make the use
of the generic macro more similar to the use of a typical C function
modifying structs, and this should facilitate all the users using it (at
least for those macros which actually change the values of the 3D point,
i.e. Init and SetX,Y,Z). Anyway, are you aware of which the most elegant
solution to this problem is (if any)?
Also, I'm wondering if the following piece of code
point3d point;
Point3DInit(&point, 1, 2, 3);
which is preprocessed to
point3d point;
((&point)->data[0]=1, (&point)->data[1]=2, (&point)->data[2]=3);
leads to an object code whose performances are worse than those of the
object code produced by the compilation of the following code
point3d point;
point.data[0]=1, point.data[1]=2, point.data[2]=3;
Of course this depends on the adopted compiler, but I'm referring to
wide-use compiler like gcc or MS Visual C++.
Thanks in advance.
I'm not actually working with the data struct that I'm going to use
in my example, indeed the data structs which I work with are by far more
complex, but I think it should easily show the point.
Let us suppose that I want implement a library handling (among other
things) points in 3D space. I would choose between the 2 following
implementations:
1.
typedef struct point3d_ {
double x;
double y;
double z;
} point3d;
2.
typedef struct point3d_ {
double data[3];
} point3d;
Let us say that I choose solution 2, but also that I want write my
code so that I can change the internal point3d representation in future,
without having to change the whole code I (and other users) wrote. For
this purpose, I would define some macros like
Point3DInit(point, xval, yval, zval) /* initialize the 3D point */
Point3DSetX(point, xval) /* set the value xval in the X-component
of point */
Point3DSetY(point, yval) /* ... */
Point3DSetZ(point, zval) /* ... */
Point3DExtX(xval, point) /* extract the X-component of point and
put its value in xval */
Point3DExtY(yval, point) /* ... */
Point3DExtZ(zval, point) /* ... */
which, of course, vary as the internal implementation of point3d change.
My question is: which would be, in your opinion, the best way to
consider the variable point in the macros above? A point3d *object* or a
*pointer* to point3d objects?
In my opinion, to consider the variable point a *pointer* to point3d
objects would be the most suitable solution, since it would make the use
of the generic macro more similar to the use of a typical C function
modifying structs, and this should facilitate all the users using it (at
least for those macros which actually change the values of the 3D point,
i.e. Init and SetX,Y,Z). Anyway, are you aware of which the most elegant
solution to this problem is (if any)?
Also, I'm wondering if the following piece of code
point3d point;
Point3DInit(&point, 1, 2, 3);
which is preprocessed to
point3d point;
((&point)->data[0]=1, (&point)->data[1]=2, (&point)->data[2]=3);
leads to an object code whose performances are worse than those of the
object code produced by the compilation of the following code
point3d point;
point.data[0]=1, point.data[1]=2, point.data[2]=3;
Of course this depends on the adopted compiler, but I'm referring to
wide-use compiler like gcc or MS Visual C++.
Thanks in advance.