casting part of a struct

Question: Is it safe to cast only part of a structure as an array if your guaranteed that another programmer isn't going to change the order of objects in the struct?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
struct Forces
{
// Translational forces
  double Fx; 
  double Fy;
  double Fz;
  
// Rotational moments
  double Mx;
  double My;
  double Mz;
};

void NormalizeVector(double v[3]);

void myFunc()
{
  Forces aForce;
  NormalizeVector(&aForce.Fx);
}
As your structure is a standard layout structure I do not see any problem. Acccording to the C++ Standard
An object of trivially copyable or standard-layout type (3.9) shall occupy contiguous bytes of storage.
That line in the standard doesn't guarantee that this program is well defined.
I have this struct (which I'm not going to change) and I have this function that does what I want.

I suppose it's safe to use both instead of making a double array and mapping each index from the struct. The latter just seems unnecessary.

Thanks!
Why not do something like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
struct Vector
{
  double x; 
  double y;
  double z;
};

struct Forces
{
// Translational forces
  Vector F;
  
// Rotational moments
  Vector M;
};

void NormalizeVector(Vector& v);

void myFunc()
{
  Forces aForce;
  NormalizeVector(aForce.F);
}
Last edited on
You can define the structure the following way

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
enum { x, y, z, dim };  

struct Forces
{
// Translational forces
  double F[dim]; 
// Rotational moments
  double M[dim];
};

void NormalizeVector(double v[dim]);

void myFunc()
{
  Forces aForce;
  NormalizeVector( aForce.F);
}

And access elements of the array as v[x], v[y] and v[z] inside NormalizeVector

I do not think that you should define a vector if your arrays have a fixed size.



The ordering generally stays the same but sometimes the compiler will optimise the struct and pad out the elements, so that an array form is lost.

In general, if part of your struct could be an array, then make it an array.

1
2
3
4
5
6
7
8
9
10
11
12
13
struct Forces
{
  double f[3];
  double m[3];
};

void NormalizeVector(double v[3]);

void myFunc()
{
  Forces aForce;
  NormalizeVector(aForce.f);
}


Surely this is easier and more appropriate?

EDIT: Yes I'm aware this is basically what vlad was saying, but it seems irresponsible using up the names x, y and z as enumeration values!
Last edited on
@Veltas
EDIT: Yes I'm aware this is basically what vlad was saying, but it seems irresponsible using up the names x, y and z as enumeration values!


Why is the using of enumerators x, y, and z irresponsible?
Because x, y and z are common names. I'd avoid calling any enumeration values single-letters.

EDIT: It seems harsh taking it up when it's so easy to access aForce.f[0], aForce.f[1] and aForce.f[2], instead of aForce.f[x], aForce.f[y] and aForce.f[z].
Last edited on
I can not agree bacause if he deals with a three-dimensial space the names x, y and z can be reserved to name three coordinates.
closed account (o1vk4iN6)
Why not go the object oriented path and create a normalize member function ? That way if the structure ever changes it'll be easy to look over what needs to be re-implemented.
Topic archived. No new replies allowed.