vector in nested struct

I am trying to define a structure to read a UDP message definition. Below is what I have. vertices_list is the field I am having problems with. I do not know before compilation time the number of vertices; so I am using a vector.

When I use this structure, the fields are all wrong (compressed). When I read a field in the header, I actually get a field value for field 3 (in the messageData).

I do know the number of vertices for a test set of data. When I create an array of this size, it works fine (i.e. all fields are properly aligned).

Any thoughts?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
struct VerticesType
{
  DOUBLE8 Latitude;
  DOUBLE8 Longitude;
} ;


#pragma pack(push, 1)
  struct Packet1
  {
    MessageHeader message_header;	
    struct messageData
    {
      DOUBLE8 field1;				 
      UINTEGER4 field2;				 
      UINTEGER4 field3;				 
                                               
      ZoneType  field4;				 
      UINTEGER1 field5;			 
      UINTEGER1 field6;		 
      UINTEGER1 field7;	 
      UINTEGER1 number_of_vertices;		 
      vector<VerticesType> vertices_list;	  
    } message_data;

    UINTEGER4 checksum;
  };


Last edited on
When I read a field in the header, I actually get a field value for field 3 (in the messageData).

You haven't shown MessageHeader, so can't comment on that.

line 23: A typedef only defines a type. It does not allocate space in the message.
Even so, you don't want a vector there. A vector has a size of it's own (overhead) in addition to the elements it stores which are not contiguous to the vector base.
The typedef was an error; debugging/testing.

I've determined a vector is not the component to use. I was hoping to stir a discussion of potential replacements.
Last edited on
I would simply replace line 23 as follows:
 
  VerticesType vertices_list[MAX_VERTICES];

if number_of_vertices is less than MAX_VERTICES, make sure the unused entries are initialized (zero), otherwise you're going to have an unpredictable checksum.

Topic archived. No new replies allowed.