Dynamic sized structs

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jonathan Halterman

    Dynamic sized structs

    I have data that is coming in from a network stream which I would like to
    format into a struct, but I do not know the size of all the pieces of the
    struct until I receive the data, so I cannot simply cast the data into my
    struct as I would normally do with organized network data. The data I am
    receiving looks something like this:

    long lvalue1
    long lvalue2
    char flag
    long data1len //size of data1
    unsigned char data1[x]
    long data2len //size of data2
    unsigned char data2[x]
    long data3len //size of data3
    unsigned char data3[x]

    If I have a pointer to the data all together, how can I separate it based on
    the structure above, correctly sizing each of the data charater arrays?

    thanks
    jonathan


  • E. Robert Tisdale

    #2
    Re: Dynamic sized structs

    Jonathan Halterman wrote:
    [color=blue]
    > I have data that is coming in from a network stream which I would like to
    > format into a struct, but I do not know the size of all the pieces of the
    > struct until I receive the data, so I cannot simply cast the data into my
    > struct as I would normally do with organized network data. The data I am
    > receiving looks something like this:[/color]

    class Data {
    private:
    long lvalue1;
    long lvalue2;
    char flag;
    long data1len; //size of data1
    unsigned char* data1;
    long data2len; //size of data2
    unsigned char* data2;
    long data3len; //size of data3
    unsigned char* data3;
    public:
    Data(void): lvalue1(0), lvalue2(0), flag('\0'),
    data1len(0), data1(0),
    data2len(0), data2(0),
    data3len(0), data3(0) { }
    Data(const Data& d);
    Data& operator=(const Data& d);
    ~Data(void) {
    delete [] data1;
    delete [] data2;
    delete [] data3;
    }
    Data& receive(instrea m& ins) {
    ins >> lvalue1 >> lvalue2 >> flag >> data1len;
    data1 = new char[data1len];
    for (long j = 0; j < data1len; ++j)
    ins >> data1[j];
    ins >> data2len;
    data2 = new char[data2len];
    for (long j = 0; j < data2len; ++j)
    ins >> data2[j];
    ins >> data3len;
    data3 = new char[data3len];
    for (long j = 0; j < data3len; ++j)
    ins >> data3[j];
    return *this;
    }
    };

    [color=blue]
    > If I have a pointer to the data all together, how can I separate it based on
    > the structure above, correctly sizing each of the data character arrays?[/color]


    Comment

    • Jonathan Halterman

      #3
      Re: Dynamic sized structs

      This is excellent. Thanks Robert.


      "E. Robert Tisdale" <E.Robert.Tisda le@jpl.nasa.gov > wrote in message
      news:3F8C844E.5 030804@jpl.nasa .gov...[color=blue]
      > Jonathan Halterman wrote:
      >[color=green]
      > > I have data that is coming in from a network stream which I would like[/color][/color]
      to[color=blue][color=green]
      > > format into a struct, but I do not know the size of all the pieces of[/color][/color]
      the[color=blue][color=green]
      > > struct until I receive the data, so I cannot simply cast the data into[/color][/color]
      my[color=blue][color=green]
      > > struct as I would normally do with organized network data. The data I am
      > > receiving looks something like this:[/color]
      >
      > class Data {
      > private:
      > long lvalue1;
      > long lvalue2;
      > char flag;
      > long data1len; //size of data1
      > unsigned char* data1;
      > long data2len; //size of data2
      > unsigned char* data2;
      > long data3len; //size of data3
      > unsigned char* data3;
      > public:
      > Data(void): lvalue1(0), lvalue2(0), flag('\0'),
      > data1len(0), data1(0),
      > data2len(0), data2(0),
      > data3len(0), data3(0) { }
      > Data(const Data& d);
      > Data& operator=(const Data& d);
      > ~Data(void) {
      > delete [] data1;
      > delete [] data2;
      > delete [] data3;
      > }
      > Data& receive(instrea m& ins) {
      > ins >> lvalue1 >> lvalue2 >> flag >> data1len;
      > data1 = new char[data1len];
      > for (long j = 0; j < data1len; ++j)
      > ins >> data1[j];
      > ins >> data2len;
      > data2 = new char[data2len];
      > for (long j = 0; j < data2len; ++j)
      > ins >> data2[j];
      > ins >> data3len;
      > data3 = new char[data3len];
      > for (long j = 0; j < data3len; ++j)
      > ins >> data3[j];
      > return *this;
      > }
      > };
      >
      >[color=green]
      > > If I have a pointer to the data all together, how can I separate it[/color][/color]
      based on[color=blue][color=green]
      > > the structure above, correctly sizing each of the data character arrays?[/color]
      >
      >[/color]


      Comment

      Working...