How to assign and delete objects without new and delete operator

Hi
I have some ASN structure to program in C++
Shown Below:
s1:Array[]S1 // this array can have 1 to 7 elements
struct S1
{
id: INTEGER
s2:Array[]S2 // this array can have 1 to 20 elements
}
struct S2
{
a:INTEGER
b:OCTETSTRING(4)
}


The problem is that as I am using Embedded C++ and my processor doesnt have heap so I cant use new and delete operator to create variable length of objects
otherwise I was thinking to use Composite or some more relevant pattern.

I dont want to go through lots of looping mechanism
Is there any other way I can create my array s1 for all different combination of s2 (e.g number elements in s2 can be 1 to 20)

Thanks


You could create your own array type, that does not allocate memory dynamically but instead it has a fixed limit for its size. Something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
template <typename T, size_t N>
class my_array_t
{
public:
   my_array_t();

   void push_back(const T &);
   void pop_back();

   T * begin();
   T * end();

   size_t size() const;

   T & operator[](size_t);

private:
   T      data[N];
   size_t nelements;
};

Of course you can expand/reduce list of methods depending on your needs.

One problem through with the above design is that T constructors are called for all N objects at the time of creating my_array_t object. Similarly, T destructors are called for all N objects at the time of destroying my_array_t object. Also, T must have a default constructor. This is different than in case of std::vector.
Topic archived. No new replies allowed.