Embedded C ++: initializing an element of an array of structure within a class, size omitted

Welcome and thank you in advance for any help on the following subject.

Edit: I forgot to add that this is an embedded system without access to STL features. We apologize for leaving this important information. This is my first C ++ encoding, so I forgot to mention the obvious. I returned to add this fact, and this question has already received some answers. Thanks to everyone for such a quick reply!

I am trying to initialize a member of an array of structure, which in turn is a public member of a C ++ class. The size of the array is omitted in the structure. Here is an example:

// ClassA.h Class A { public: struct StructA { StructB structs[]; }; struct StructB { // stuff }; ClassA(); //etc }; // ClassB.h ClassB: public ClassA { public: StructA structA; ClassB() // etc }; // ClassB.cpp // What do I do here? 

I tried:

StructA ClassB::structA.structs[] = { first, second, third }

no luck. I'm relatively new to C ++, so that put me on my guard. I can add size to an array element in StructA, but I would prefer there is a legitimate way to handle this.

Thanks!

+4
source share
4 answers

In general, it is difficult to create something that has a variable size in C or C ++ without resorting to dynamic allocation, which should be avoided when using embedded systems.

I would suggest that you create a simple array of things and place a pointer to this inside your class. You can put an array in a global scope, and the pointer does not need to know its size.

Also, just putting something in a class does not make it global. You must either make it a static member (which means that there will be only one variable, no matter how many objects of this particular class are), or initialize it in the constructor.

For instance:

 class A { public: struct B { char const * str; }; // Constructor A(A::B * beePointer) : mB(beePointer) { } // Data members B * mB; }; // Global array A::B my_Bees[] = {"first", "second", "third"}; A my_a(my_Bees); 

Of course, in this solution you cannot add more elements to the end of the array, since it is allocated once and for all.

+1
source

Arrays without size are invalid C ++; they are a feature on C99 where they need some malloc magic. If you try to compile code using this function with g++ -pedantic , you will see that it emits:

 $ g++ -Wall -pedantic test.cc test.cc:5: error: ISO C++ forbids zero-size array 'foo' 

I suggest using std::vector instead, which has the same function as non-standard arrays in C99, but is much safer:

 std::vector<StructB> structs; 

then add items with push_back .

+7
source

Since you are still coding C ++ code, why not use:

std::vector<structA> mStruct; ...
mStruct.push_back(first); ...

std :: vectors are quiet convenient compared to the c-array and less error prone.

+2
source

In C ++, you cannot use non-standard arrays. Since you do not have access to vector :

  • If you know the maximum number of elements that you will never need, just put your array in this value.
  • If you do not know the maximum number of elements that you will never need, you will have to dynamically manage your memory in some way. You can try new / delete by default, but you may need to create your own memory pool if the default values ​​are not efficient enough.
  • If the very last line of your message indicates that you only need one copy of the array in the entire program, and that you know what values ​​are initialized at compile time, you can use a static class member. Your sample code had several problems, so it was hard for me to guess what you wanted.

An example of how static will look:

 // ClassA.h class ClassA { public: struct StructB { // stuff }; struct StructA { static StructB structs[]; }; ClassA(); //etc }; // ClassB.h class ClassB: public ClassA { public: StructA structA; ClassB() { } // etc }; // ClassB.cpp ClassA::StructB first, second, third; // What do I do here? ClassA::StructB ClassA::StructA::structs[] = { first, second, third }; int main() { return 0; } 
+1
source

Source: https://habr.com/ru/post/1345878/


All Articles