Does order of elements within struct matter?

2 posts / 0 new
Last post
Offline
Last seen: 3 months 1 week ago
Joined: 04/22/2019
Posts: 6
Does order of elements within struct matter?

Does the order of elements within a struct matter?

 

struct Person {

  int age;

  String name;

}

vs

struct Person {

  String name;

  int age;

}

 

I assumed once these were compiled, the order wouldn't matter, but testing from a colleague seems to indicate otherwise. 

I couldn't find anything about this in the IDL Spec...if you have that, a link would be much appreciated.

Organization:
Howard's picture
Offline
Last seen: 22 hours 54 min ago
Joined: 11/29/2012
Posts: 618

Hi,

How do you mean "matter"?  What were the testing results that your colleague found?

Generally speaking, yes, how a data structure is layed out in memory depends on the programming language and CPU architecture as well as compiler (and compiler options).  And it can be different from platform to platform in addition to the order of members.

With respect to DDS, how a data structure is serialized into a buffer that can be exchanged on the network will also depend on the order of members...as well as the type of members, mainly due to padding requirements of the XCDR standard (used for serialization).  Fundamentally, that's a tradeoff between CPU required to serialize/deserialize versus size of the serialized buffer.  But that's specified by the standard and all DDS implementations must follow the same serialization standard to be interoperable.