473,785 Members | 2,282 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
16 1754
steflhermitte wrote:
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.
Then this is just another reason for using std::vector instead of fiddling
around with raw arrays. std::vector is a class that encapsulates the nasty
details about arrays, especially the dynamic memory handling.
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
No, this is just a constructor.
metasegment::me tasegment(unsig ned int NbLayers)
{
id=0;
num = new int[NbLayers];
mean = new int[NbLayers];
done=0;
}
If you do the dynamic memory yourself, you must provide a destructor that
properly destroys the dynamic arrays. Futher, you need to provide a
user-defined copy constructor and assignment operator. The compiler
generates those if you don't write you own, but in this case, the
compiler-generated ones don't do what you want.
Again, std::vector would handle this for you.
};
------------------------------------------------

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

vector <metasegment(10 )> testvector;

but is does not work.
Between the < and > has to be a class. metasegment(10) is not a class.
I assume I have to work with typedef, but I don't know how to solve this
problem. Any advice?


Just do:

vector<metasegm ent> testvector; //define the vector
testvector.push _back(metasegme nt(10)); //append an instance of your class
//to it, initialized with 10.

Jul 23 '05 #11
steflhermitte wrote:

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.
You opted for the worst version you could do.
namespace test
{
class metasegment
{
public:
// constructor
metasegment(uns igned int NbLayers);
// porperties
int id;
int *num;
int *mean;
bool done;
};
};
That class is incomplete.
You are missing:
* a destructor
* a copy constructor
* an assignment operator

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

vector <metasegment(10 )> testvector;

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


Look up the 'Rule of three'.
Then implement
* a destructor
* a copy constructor
* an assignment operator

if you implement them correctly, it will work.

Or save yourself all the hassle and use a std::vector as was
suggested by lots of replies.

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 23 '05 #12

"steflhermi tte" <st************ ***@agr.kuleuve n.ac.be> skrev i en meddelelse
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


You should definitely use a std::vector.

/Peter
Jul 23 '05 #13
John Carson wrote:
Robbie Hatley wrote:

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


My mistake. I was confusing arrays in classes with arrays in
std::containers (which isn't allowed, because assignment isn't
defined for arrays).

On studying the standard, I see in 12.8, clause 8, paragraph 3,
regarding requirments for class implicit copy constructors:

"if the subobject is an array, each element is copied,
in the manner appropriate to the element type"

I find this sort of amusing, because that means if type
Type1 is a class with an array member, I can say:

Type1 t1;
Type1 t2(t1);

However, I'm not allowed to do any of THESE things:

typedef int ArrayOfFiveInts[5];
ArrayOfFiveInts t1 = {9, 1, 1, 17, 4};

// Error, assignment not allowed:
ArrayOfFiveInts t2 = t1;

// Error, cast not allowed:
ArrayOfFiveInts t3 = ArrayOfFiveInts (t1);

// Error, initialization not allowed:
ArrayOfFiveInts t4(t1);

Which I think is dumb. Why not allow those things?

The following works, of course, but is very ugly:

ArrayOfFiveInts t5;for(int i=0;i<5;++i)t5[i]=t1[i];

--
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 #14
John Carson wrote:
Robbie Hatley wrote:

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


My mistake. I was confusing arrays in classes with arrays in
std::containers (which isn't allowed, because assignment isn't
defined for arrays).

On studying the standard, I see in 12.8, clause 8, paragraph 3,
regarding requirments for class implicit copy constructors:

"if the subobject is an array, each element is copied,
in the manner appropriate to the element type"

I find this sort of amusing, because that means if type
Type1 is a class with an array member, I can say:

Type1 t1;
Type1 t2(t1);

However, I'm not allowed to do any of THESE things:

typedef int ArrayOfFiveInts[5];
ArrayOfFiveInts t1 = {9, 1, 1, 17, 4};

// Error, assignment not allowed:
ArrayOfFiveInts t2 = t1;

// Error, cast not allowed:
ArrayOfFiveInts t3 = ArrayOfFiveInts (t1);

// Error, initialization not allowed:
ArrayOfFiveInts t4(t1);

Which I think is dumb. Why not allow those things?

The following works, of course, but is very ugly:

ArrayOfFiveInts t5;for(int i=0;i<5;++i)t5[i]=t1[i];

--
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 #15
"Robbie Hatley" <lonewolfintj at pacbell dot net> wrote:
On studying the standard, I see in 12.8, clause 8, paragraph 3,
regarding requirments for class implicit copy constructors:

"if the subobject is an array, each element is copied,
in the manner appropriate to the element type"

I find this sort of amusing, because that means if type
Type1 is a class with an array member, I can say:

Type1 t1;
Type1 t2(t1);

However, I'm not allowed to do any of THESE things:

typedef int ArrayOfFiveInts[5];
ArrayOfFiveInts t1 = {9, 1, 1, 17, 4};

// Error, assignment not allowed:
ArrayOfFiveInts t2 = t1;
This is not an assignment.
// Error, cast not allowed:
ArrayOfFiveInts t3 = ArrayOfFiveInts (t1);
This would be the same as the one before, just with an additional copy.
// Error, initialization not allowed:
ArrayOfFiveInts t4(t1);
One reason why typedefs for arrays are usually avoided.
Which I think is dumb. Why not allow those things?
Good question.
The following works, of course, but is very ugly:

ArrayOfFiveInts t5;for(int i=0;i<5;++i)t5[i]=t1[i];


Or:
ArrayOfFiveInts t6 = { t1[0], t1[1], t1[2], t1[3], t1[4] };
Jul 23 '05 #16
Robbie Hatley wrote:

I find this sort of amusing, because that means if type
Type1 is a class with an array member, I can say:

Type1 t1;
Type1 t2(t1);

However, I'm not allowed to do any of THESE things:

typedef int ArrayOfFiveInts[5];
ArrayOfFiveInts t1 = {9, 1, 1, 17, 4};

// Error, assignment not allowed:
ArrayOfFiveInts t2 = t1;

// Error, cast not allowed:
ArrayOfFiveInts t3 = ArrayOfFiveInts (t1);

// Error, initialization not allowed:
ArrayOfFiveInts t4(t1);

Which I think is dumb. Why not allow those things?


Because of The Rule. In the above expressions, 't1' is
converted to a pointer to int, before the '=' is processed.

To allow your code, you would either have to revoke The Rule
(which I think would cause too much incompatibility with
existing code), or allow this:

int *ptr = foo();
ArrayOfFiveInts t2 = ptr;

which seems possible but I think it is dangerous.

Jul 23 '05 #17

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
9403
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
2148
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
4059
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
10350
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10157
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...
0
9957
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
8983
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7505
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5386
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
5518
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3658
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2887
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.