c++ - extra data elements in a union structure -


i'm trying encode relatively complex message union structure can generate array of uint8_t can feed serial communications protocol.

however, when looking @ array generated when union filled data, uint8_t element appears after command type element. please see union structure below:

union myint16_t{         uint16_t ui16;         int16_t i16;         uint8_t data[2];     };  union {     struct {         uint8_t commandtype;         myint16_t upperlimits[4];         myint16_t lowerlimits[4];         myint16_t jointzeros[4];         int8_t jointpolarity[4];         myint16_t p[4];         myint16_t i[4];         myint16_t d[4];      };     uint8_t data[53]; }; 

as can see there second anonymous union references union myint16_t. if fill values of anonymous union , print out underlying data[53] array, second value (data[1]) 0 , not part of first element of upperlimits[4]. please see code fill union , print out elements.

char q = 'c';        hmmv4_configmsg msg;  msg.commandtype =(uint8_t) q; msg.upperlimits[0].ui16 = 784; msg.upperlimits[1].ui16  = 784; msg.upperlimits[2].ui16  = 784; msg.upperlimits[3].ui16  = 784; msg.lowerlimits[0].ui16  = 223; msg.lowerlimits[1].ui16  = 223; msg.lowerlimits[2].ui16  = 223; msg.lowerlimits[3].ui16  = 223; msg.jointzeros[0].ui16  = 512; msg.jointzeros[1].ui16  = 512; msg.jointzeros[2].ui16  = 512; msg.jointzeros[3].ui16  = 512; msg.jointpolarity[0] = -1; msg.jointpolarity[1] =-1; msg.jointpolarity[2] =-1; msg.jointpolarity[3] =-1; msg.p[0].i16=4000; msg.p[1].i16=4000; msg.p[2].i16=4000; msg.p[3].i16=4000; msg.i[0].i16=1; msg.i[1].i16=1; msg.i[2].i16=1; msg.i[3].i16=1; msg.d[0].i16=24; msg.d[1].i16=24; msg.d[2].i16=24; msg.d[3].i16=24;  //msg.change_endian();  while(1) {         for(int =0; i<54; i++)         {             writebuf[i]=msg.data[i];              printf("d: %d,  %d \n", i, msg.data[i]);         }          printf("l0: %d, %d, %d", msg.p[0].i16, msg.p[0].data[0], msg.p[0].data[1]);          int r =jointencoder.xfer1(writebuf, readbuf, 54); } 

the output of printf (the offending element d:1 -- shouldn't there):

d: 0,  99  d: 1,  0  d: 2,  16  d: 3,  3  d: 4,  16  d: 5,  3  d: 6,  16  d: 7,  3  d: 8,  16  d: 9,  3  d: 10,  223  d: 11,  0  d: 12,  223  d: 13,  0  d: 14,  223  d: 15,  0  d: 16,  223  d: 17,  0  d: 18,  0  d: 19,  2  d: 20,  0  d: 21,  2  d: 22,  0  d: 23,  2  d: 24,  0  d: 25,  2  d: 26,  255  d: 27,  255  d: 28,  255  d: 29,  255  d: 30,  160  d: 31,  15  d: 32,  160  d: 33,  15  d: 34,  160  d: 35,  15  d: 36,  160  d: 37,  15  d: 38,  1  d: 39,  0  d: 40,  1  d: 41,  0  d: 42,  1  d: 43,  0  d: 44,  1  d: 45,  0  d: 46,  24  d: 47,  0  d: 48,  24  d: 49,  0  d: 50,  24  d: 51,  0  d: 52,  24  d: 53,  0  l0: 4000, 160, 15joint encoder transferred  

my question why d:1 there? understanding of unions , structures because command type uint8_t, should occupy 1 data space, , therefore upperlimits[0] should start on d:1, seems command_type acting uint16_t , posting bit. why so?

note: may see index goes count data[53] should out of bounds, need read , send able deconstruct data @ other end.

there padding byte between commandtype , upperlimits; 2-byte myint16_t data type aligned on byte boundary.

struct {     uint8_t commandtype;     myint16_t upperlimits[4];     ... 

if print size of anonymous structure , union, you're find @ least 54 bytes (where think should 53). 1 of disadvantages of untagged structure types embedded anonymous members of union there no easy way print size of structure. give structure tag (struct tag { uint8_t commandtype; ...) can print size out.

there isn't easy fix using current framework.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -