473,320 Members | 2,112 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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
Jul 19 '05 #1
2 1914
Jonathan Halterman wrote:
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?

Jul 19 '05 #2
This is excellent. Thanks Robert.
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote in message
news:3F**************@jpl.nasa.gov...
Jonathan Halterman wrote:
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?


Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Ayende Rahien | last post by:
I want to build a application where I've a fixed sidebar, and a replacable main area. (Think frames in HTML, where you've a menu on one side, and the real data on the other.) I build the...
12
by: CJM | last post by:
How can I dynamically sort the results from a Stored Procedure? Or more importantly, what is the fastest and most efficient way? I know I can do the sorting within the recordset in ASP, but AFAIK...
11
by: Roman Hartmann | last post by:
hello, I do have a question regarding structs. I have a struct (profil) which has a pointer to another struct (point). The struct profil stores the coordinates of points. The problem is that I...
19
by: Geetesh | last post by:
Recently i saw a code in which there was a structer defination similar as bellow: struct foo { int dummy1; int dummy2; int last }; In application the above array is always allocated at...
4
by: Himanshu Singh Chauhan | last post by:
Hello All!! Can anybody tell what variable sized structures are and how can they be used? regards --Himanshu
5
by: viperdriver87 | last post by:
Greetings all, I had another post on here about a Dynamic array of ints problem, and with your help, I managed to solve that problem. Now, I've another issue. I need to dyanically allocate an array...
9
by: johndale | last post by:
hi, I am pretty new to C#, but have some experience with other lang.(delphi,php,asp...). I wanted to create dynamic array of structs type, but it wont work. So I google it, and found that C#.NET...
3
by: UncleRic | last post by:
Greetings: I'm trying to get the ASCI C syntax correct here. I want to create memory space for a variable-size array of pointers to structs. This is what I found in theScripts' archive: ...
10
by: swornavidhya.mahadevan | last post by:
Which allocation (Static / Dynamic) is suitable for the situation when we are trying to allocate for a overloaded memory when the memory is full and no space to allocate. What will happen if both...
3
by: =?Utf-8?B?UiBSZXllcw==?= | last post by:
Hi! This discussion may help other programmers get a better idea of how to save uploaded images through a website. Why? Well currently, I save 3 versions of every uploaded image on my own...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.