473,770 Members | 1,861 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Stuctures of variable length

Dear cpp-ians,

I am working with a structure:

struct meta_segment
{
long double id;
long double num;
long double mean;
bool done;
};

but I want to store multiple elements for some of the elements of my
structure. I was thinking of using arrays in my structure. E.g., when I
have 5 elements for 'num' and 'mean':

struct meta_segment
{
long double id;
long double num[5];
long double mean[5];
bool done;
};

The problem is that only at run-time the program knows how long my
'num' and 'mean' will be. So what I want to do is make a structure,
where I can incorporate the length into the structure and use that
length as an argument.

struct meta_segment
{
long double id;
long double num[NbElements];
long double mean[NbElements];
bool done;
};

I something like this possible? Or should I look for other solutions to
solve this problem?

Thank you very much in advance,
Stef

Jul 23 '05 #1
16 1753

"steflhermi tte" <st************ ***@agr.kuleuve n.ac.be> wrote in message
news:11******** *************@g 14g2000cwa.goog legroups.com...
Dear cpp-ians,

I am working with a structure:

struct meta_segment
{
long double id;
long double num;
long double mean;
bool done;
};

but I want to store multiple elements for some of the elements of my
structure. I was thinking of using arrays in my structure. E.g., when I
have 5 elements for 'num' and 'mean':

struct meta_segment
{
long double id;
long double num[5];
long double mean[5];
bool done;
};

The problem is that only at run-time the program knows how long my
'num' and 'mean' will be. So what I want to do is make a structure,
where I can incorporate the length into the structure and use that
length as an argument.

struct meta_segment
{
long double id;
long double num[NbElements];
long double mean[NbElements];
bool done;
};

I something like this possible? Or should I look for other solutions to
solve this problem?

Thank you very much in advance,
Stef


Why dont you use a class and allocate the memory in the constructor? You
dont have to add any methods, and making the variables public means you can
use the class in exactly the same way you would use your structure.
Allan
Jul 23 '05 #2
steflhermitte wrote:

Dear cpp-ians,
[snip]
I something like this possible? Or should I look for other solutions to
solve this problem?


You want a dynamically sizeable array.
Use std::vector for this.

#include <vector>

struct meta_segment
{
long double id;
std::vector< long double > num;
std::vector< long double > mean;
bool done;
};
Now the num and mean members can grow dynamically as needed.

int main()
{
meta_segment TheData;

TheData.num.pus h_back( 5.0 ); // add 1 entry to num
TheData.num.pus h_back( 7.0 ); // and another one

for( int i = 0; i < 200; ++i ) // what the heck ...
TheData.num.pus h_back( i ); // ... add a lot of them
}

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 23 '05 #3
steflhermitte wrote:
Dear cpp-ians,

I am working with a structure:

struct meta_segment
{
long double id;
long double num;
long double mean;
bool done;
};

but I want to store multiple elements for some of the elements of my
structure. I was thinking of using arrays in my structure. E.g., when I
have 5 elements for 'num' and 'mean':

struct meta_segment
{
long double id;
long double num[5];
long double mean[5];
bool done;
};

The problem is that only at run-time the program knows how long my
'num' and 'mean' will be. So what I want to do is make a structure,
where I can incorporate the length into the structure and use that
length as an argument.

struct meta_segment
{
long double id;
long double num[NbElements];
long double mean[NbElements];
bool done;
};

I something like this possible? Or should I look for other solutions to
solve this problem?


You can use std::vector:

#include <vector>
#include <iostream>

struct meta_segment
{
long double id;
std::vector<lon g double> num;
std::vector<lon g double> mean;
bool done;
};

int main()
{
meta_segment seg;
seg.num.push_ba ck(12345);
seg.num.push_ba ck(54321);
std::cout << seg.num[1] << '\n';
}

Jul 23 '05 #4
steflhermitte wrote:
The problem is that only at run-time the program knows how long my
'num' and 'mean' will be. So what I want to do is make a structure,
where I can incorporate the length into the structure and use that
length as an argument.


use std::vector

Jul 23 '05 #5
"steflhermi tte" <st************ ***@agr.kuleuve n.ac.be> wrote in message
news:11******** *************@g 14g2000cwa.goog legroups.com...
Dear cpp-ians,

I am working with a structure:

struct meta_segment
{
long double id;
long double num;
long double mean;
bool done;
};

but I want to store multiple elements for some of the elements of my
structure. I was thinking of using arrays in my structure. E.g., when I
have 5 elements for 'num' and 'mean':

struct meta_segment
{
long double id;
long double num[5];
long double mean[5];
bool done;
};

The problem is that only at run-time the program knows how long my
'num' and 'mean' will be. So what I want to do is make a structure,
where I can incorporate the length into the structure and use that
length as an argument.

struct meta_segment
{
long double id;
long double num[NbElements];
long double mean[NbElements];
bool done;
};

I something like this possible? Or should I look for other solutions to
solve this problem?

Thank you very much in advance,
Stef


Arrays in structs or classes are a poor idea.
(No copy constructor, fixed size, etc.)

If you need variable-length members in structs,
use std::vector instead of arrays:

#include <iostream>
#include <iomanip>
#include <vector>
using std::vector;
using std::cout;
using std::endl;
using std::setprecisi on;
struct meta_segment
{
long double id;
std::vector<lon g double> num;
std::vector<lon g double> mean;
bool done;
};
int main()
{
meta_segment S;
S.num.push_back (3278.2054);
S.num.push_back (7284.0355);
S.mean.push_bac k(6342.3967);
S.mean.push_bac k(3968.2853);
cout << "S.num[0] = " << setprecision(10 ) << S.num[0] << endl;
cout << "S.num[1] = " << setprecision(10 ) << S.num[1] << endl;
cout << "S.mean[0] = " << setprecision(10 ) << S.mean[0] << endl;
cout << "S.mean[1] = " << setprecision(10 ) << S.mean[1] << endl;
return 0;
}
--
Cheers,
Robbie Hatley
Tustin, CA, USA
email: lonewolfintj at pacbell dot net
web: home dot pacbell dot net slant earnur slant

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Jul 23 '05 #6
"Robbie Hatley" <lonewolfintj at pacbell dot net> wrote in message
news:42******** **@spool9-west.superfeed. net

Arrays in structs or classes are a poor idea.
(No copy constructor, fixed size, etc.)


What do you mean by no copy constructor? The array itself doesn't have one
of course, but array members are successfully copied when one struct
containing an array is used to initialise another, e.g.,

#include <iostream>

struct Test
{
int x[5];
};

int main()
{
Test t1;
for(int i=0; i<5; ++i)
t1.x[i]=i;

Test t2(t1);
for(int i=0; i<5; ++i)
std::cout << t2.x[i] << std::endl;

return 0;
}
--
John Carson

Jul 23 '05 #7
> I something like this possible?

Yes, its definately possible.
Or should I look for other solutions to
solve this problem?


IMHO, yes. Use std::vector.

Jul 23 '05 #8
Me
<snip about NbElements is not an integral constant expression>
struct meta_segment
{
long double id;
long double num[NbElements];
long double mean[NbElements];
bool done;
};

I something like this possible? Or should I look for other solutions to
solve this problem?


Some C++ compilers allow C99's flexible array members as an extension.
If your compiler does, you can try:

struct meta_stuff {
long double num;
long double mean;
};

struct meta_segment {
bool done;
long double id;
meta_stuff stuff[];
};

meta_segment *m = (meta_segment*) malloc(sizeof(m eta_segment) +
sizeof(meta_stu ff)*NbElements) ;

Jul 23 '05 #9
Thanks folks, you helped me a lot!

I'm am not an experienced c++-user, so things that are evident are not
so evident for me.

I opted for making a class. I made:

TEST.H
------------------------------------------------
#ifndef TEST_H_
#define TEST_H_

using namespace std;
namespace test
{
class metasegment
{
public:
// constructor
metasegment(uns igned int NbLayers);
// porperties
int id;
int *num;
int *mean;
bool done;
};
};
#endif
------------------------------------------------

TEST.CPP
------------------------------------------------
#include "test.h"
#include <iostream>
#include <stdlib.h>

namespace test
{
// constructor and destructor
metasegment::me tasegment(unsig ned int NbLayers)
{
id=0;
num = new int[NbLayers];
mean = new int[NbLayers];
done=0;
}
};
------------------------------------------------

Now I want to make a vector based on this class:

vector <metasegment(10 )> testvector;

but is does not work. I assume I have to work with typedef, but I don't
know how to solve this problem. Any advice?

Thanks again for your help!

Kind regards,
Stef

Jul 23 '05 #10

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

Similar topics

6
2751
by: BigDadyWeaver | last post by:
I am using the following code in asp to define a unique and unpredictable record ID in Access. <% 'GENERATE UNIQUE ID Function genguid() Dim Guid guid = server.createobject("scriptlet.typelib").guid guid=Left(guid,instr(guid,"}")) genguid=guid
5
11739
by: MLH | last post by:
I'm working with lots of long strings now, it seems. I have to import them & parse them constantly. The A97 memo field type supports only 32768 chars. What happens when this is processed... Dim MyString As String Am I getting VLS declaration or a FLS declaration? Can I control which I get somehow? I have done some homework, but I don't understand
18
9402
by: Panchal V | last post by:
I want to access a variable length record in C, the format is as follows : +---+---+-----------+ | A | L | D A T A | +---+---+-----------+ A - Some Data (1 BYTE) L - Length the Data that follows (1 BYTE) then actual data
14
5849
by: Luiz Antonio Gomes Pican?o | last post by:
How i can store a variable length data in file ? I want to do it using pure C, without existing databases. I'm thinking to use pages to store data. Anyone has idea for the file format ? I want to store data like a database: ---------------------------------- Custumer:
19
2147
by: Skybuck Flying | last post by:
Hi, I think I might have just invented the variable bit cpu :) It works simply like this: Each "data bit" has a "meta data bit". The meta data bit describes if the bit is the ending bit of a possibly large structure/field.
5
1760
by: gmelcer | last post by:
Hi, I need to use the data type "struct" in a class. Could someone show me a simple example of how to use stuct in a class and access the data type which is declared in the private section of the class. Here is an exmaple of what I am trying to do. If someone could quickly write a module to acces the structure I would highly appreciate it #include <iostream> #ifndef POLYLINE #define POLYLINE
3
4058
by: chandra.krothapalli | last post by:
Hi, I am writing a program to read database logs using db2Readlog/ db2ReadLogNoConn API. I am able to parse the data of "FIXED format data" and link them to appropriate columns for a given UPDATE/INSERT/DELETE. I am having difficulty in parsing the "Variable format data". In the log record I am not able find information about data length of each
2
1222
by: UncleRic | last post by:
Does anyone know how to return a usable array of pointers to structures? Here's the structure: typedef struct { char* name; // '\0'-terminated C string int number; } SomeSeq;
3
1417
by: =?Utf-8?B?SXNsYXkgUm9kcmlndWV6IEpyLg==?= | last post by:
How do you share glogal data stuctures between VB.net and C++ or C# ? Thanks, Islay -- Islay
0
9453
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10099
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10036
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9904
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6710
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5481
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3607
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.