Stuctures of variable length

S

steflhermitte

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
 
A

Allan Bruce

steflhermitte said:
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
 
K

Karl Heinz Buchegger

steflhermitte said:
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.push_back( 5.0 ); // add 1 entry to num
TheData.num.push_back( 7.0 ); // and another one

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

Rolf Magnus

steflhermitte said:
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<long double> num;
std::vector<long double> mean;
bool done;
};

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

Shezan Baig

steflhermitte said:
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
 
R

Robbie Hatley

steflhermitte said:
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::setprecision;
struct meta_segment
{
long double id;
std::vector<long double> num;
std::vector<long double> mean;
bool done;
};
int main()
{
meta_segment S;
S.num.push_back(3278.2054);
S.num.push_back(7284.0355);
S.mean.push_back(6342.3967);
S.mean.push_back(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
 
J

John Carson

Robbie Hatley said:
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;

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

return 0;
}
 
M

Me

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(meta_segment) +
sizeof(meta_stuff)*NbElements);
 
S

steflhermitte

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(unsigned 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::metasegment(unsigned 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
 
K

Karl Heinz Buchegger

steflhermitte said:
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(unsigned 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.
 
R

Rolf Magnus

steflhermitte said:
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(unsigned 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::metasegment(unsigned 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 said:
I assume I have to work with typedef, but I don't know how to solve this
problem. Any advice?

Just do:

vector<metasegment> testvector; //define the vector
testvector.push_back(metasegment(10)); //append an instance of your class
//to it, initialized with 10.
 
P

Peter Koch Larsen

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

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
474,298
Messages
2,571,542
Members
48,283
Latest member
RitaVui655

Latest Threads

Top