flexible array in a struct, or something similar

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

In C++ I want to statically create an array that contains variable arrays
inside of it. Or something similar.

Essentially I want to do something like this:
{
{"name1", {a, b, c} },
{"name2", {b, c} },
{"name2", {d} }
}
I want an array of names, and with each name I want to associate an list of
characteristics., of unknown size. But this will all be known at compile
time, so it is a static list.

Or the equivalent. It seems to be that this should be possible with the
compiler.
Worst case scenario I can always do:
int *foo[] = {a, b, c};
{"name1", foo}, etc...

But that seems like too much work on my part. The compiler should be able to
do this work.
If I do the obvious and declare flexible arrays I get a warning about a
nonstandard extension. Then an error "arrays of objects containing zero-size
arrays are illegal"

Thanks
 
This is very similar to the approach I am using.

But I am curious, why do you advise to not use this solution? Other than it
will waste a few hundred bytes of memeory at most. Since there is no viable
alternative solution, I am curious why this one is bad?

Thanks

Vladimir Nesterovsky said:
In C++ I want to statically create an array that contains variable arrays
inside of it. Or something similar.

Essentially I want to do something like this:
{
{"name1", {a, b, c} },
{"name2", {b, c} },
{"name2", {d} }
}
I want an array of names, and with each name I want to associate an list
of
characteristics., of unknown size. But this will all be known at compile
time, so it is a static list.

A following will work. I advice use not this solution, however.

struct Record
{
static const int max_values = 10;

char *name;
int values[max_values];
};

int main(int argc, char* argv[])
{
Record records[] =
{
{"name1", {1, 2, 3} },
{"name2", {2, 3} },
{"name2", {4} }
};

return 0;
}

P.S. Next version of C++ will allow to define such consturcts much more
efficiently. See std::
initializer_list for details.
 
In C++ I want to statically create an array that contains variable arrays
inside of it. Or something similar.

Essentially I want to do something like this:
{
{"name1", {a, b, c} },
{"name2", {b, c} },
{"name2", {d} }
}
I want an array of names, and with each name I want to associate an list
of
characteristics., of unknown size. But this will all be known at compile
time, so it is a static list.

A following will work. I advice use not this solution, however.

struct Record
{
static const int max_values = 10;

char *name;
int values[max_values];
};

int main(int argc, char* argv[])
{
Record records[] =
{
{"name1", {1, 2, 3} },
{"name2", {2, 3} },
{"name2", {4} }
};

return 0;
}

P.S. Next version of C++ will allow to define such consturcts much more
efficiently. See std::
initializer_list for details.
 
This is very similar to the approach I am using.
But I am curious, why do you advise to not use this solution? Other than
it
will waste a few hundred bytes of memeory at most. Since there is no
viable
alternative solution, I am curious why this one is bad?

It's matter of taste. Not always syntactic sugar justifies itself.
E.g.: RAII and smart pointers are good, iostreams are bad.
 
Back
Top