Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Static initialization of an array of Structs (each of which has an array member)

1 view
Skip to first unread message

mike.arsenault

unread,
Nov 24, 2009, 5:25:04 PM11/24/09
to
I'm not sure how to explain this problem other that posting the code
that I'm attempting to compile, so here it is:

struct innerStruct
{
int m_RelOID;
int m_ParentClassOID;
int m_InverseRelOID;
int m_InverseParentClassOID;
};

struct outerStruct
{
int m_HashIndex;
struct innerStruct m_InnerArray[];
}
s_OuterStruct[] =
{
0, {
{1, 2, 3, 4},
{5, 6, 7, 8}
},
1, {
{11, 12, 13, 14},
{15, 16, 17, 18},
{25, 26, 27, 28},
}
};

's_OuterStruct' is an array of structs - I'm trying to initialize an
array with 2 elements in it.
The first element has a '0' with an 'inner' array of 2 elements
(of type 'innerStruct').
The second element has a '1' with an 'inner' array of 3 elements
(of type 'innerStruct').

I'm having a hard time coming up with the proper syntax to initialize
this 'array of structs, each of which is composed of a number and
another array of structs'.

Keep in mind that the inner 'array of structs' won't have the same
number of elements, as in the above example.

Is what I'm trying to do even possible with static initialization??

Thanks
Mike

Andrey Tarasevich

unread,
Nov 24, 2009, 5:37:37 PM11/24/09
to
mike.arsenault wrote:
>
> Is what I'm trying to do even possible with static initialization??
>

No. C++ language has no such feature. The declaration of your
'outerStruct' is already broken

> struct outerStruct
> {
> int m_HashIndex;
> struct innerStruct m_InnerArray[];
> }
> s_OuterStruct[] =

because in C++ you can't use an array of unspecified size as a class member.

Using a '[]' array as a trailing member of a struct is allowed in C99
version of C language. However, it is intent is to legalize "struct
hack", i.e. support run-time sized arrays as trailing members of a
*dynamically allocated* struct objects. It can't be used with
non-dynamic objects and aggregate initializers, as you are trying to use it.

Finally, having said the above, what you are trying to do is actually
supported as a C language extension in GCC compiler. Nevertheless, it is
not legal C and it is not legal C++.

--
Best regards,
Andrey Tarasevich

Paavo Helde

unread,
Nov 24, 2009, 5:48:28 PM11/24/09
to
"mike.arsenault" <mike.ar...@ivara.com> wrote in news:c7e45c84-6220-
4e97-96af-4...@m13g2000vbf.googlegroups.com:

> I'm not sure how to explain this problem other that posting the code
> that I'm attempting to compile, so here it is:
>
> struct innerStruct
> {
> int m_RelOID;
> int m_ParentClassOID;
> int m_InverseRelOID;
> int m_InverseParentClassOID;
> };
>
> struct outerStruct
> {
> int m_HashIndex;
> struct innerStruct m_InnerArray[];
> }

C++ wants to have a fixed sizeof for each type. Here, the sizeof
(outerStruct) is fixed after parsing its definition. You cannot enlarge
it later, and you cannot put data in the struct which does not fit there,
neither can you have a type with varying size, and neither can you have
an array with elements having different sizes. So it's not just
initialization what does not work here. You have to use either
std::vector or dynamic memory.

hth
Paavo

Vidar Hasfjord

unread,
Nov 25, 2009, 1:24:40 AM11/25/09
to
On Nov 24, 10:25 pm, "mike.arsenault" <mike.arsena...@ivara.com>
wrote:

To use static initalization you will have to fix the size of the
member array in outerStruct:

struct outerStruct
{
int m_HashIndex;
enum {MaxArraySize = 3};
innerStruct m_InnerArray [MaxArraySize];
}

Regards,
Vidar Hasfjord

Saeed Amrollahi

unread,
Nov 25, 2009, 3:22:36 AM11/25/09
to

Hi Mike
As others mentioned because each object in an array must contain at
least one element
and compiler assumes the size of m_InnerArray is zero, so such error
occured.
As you know there is big diiference between array and vector: the size
of vector can be zero.
Because the C++0x is approaching, may be you like to know, using
inilializer lists
you can write your code much better:
struct outerStruct
{
int m_HashIndex;
vector<innerStruct> v; // empty vector
};

vector<outerStruct> vec = {


{ 0,
{ {1, 2, 3, 4},
{5, 6, 7, 8} }
},
{ 1,
{ {11, 12, 13, 14},
{15, 16, 17, 18},
{25, 26, 27, 28},
}
}
};

As you see, the above code will be more elegant and conform to sprit
of C++.

Regards,
-- Saeed Amrollahi

mike.arsenault

unread,
Nov 25, 2009, 10:06:49 AM11/25/09
to
Thanks for all the answers - There are other ways that I can set up
the structure that I want - it just would've been nice to do it all
with initialization syntax.

Mike

0 new messages