Dynamic sized structs

J

Jonathan Halterman

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

E. Robert Tisdale

Jonathan said:
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:

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(instream& 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;
}
};
 
J

Jonathan Halterman

This is excellent. Thanks Robert.


E. Robert Tisdale said:
Jonathan said:
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:

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(instream& 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;
}
};

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,481
Members
44,900
Latest member
Nell636132

Latest Threads

Top