Re: Initializing complex, const, variably-sized structures at compile
time
On Dec 23, 12:26 pm, Michael DOUBEZ <michael.dou...@free.fr> wrote:
James Kanze a =E9crit :
On Dec 23, 3:17 am, spacewre...@gmail.com wrote:
I'm working on a USB device framework for a microcontroller.
USB requires manipulating many descriptor structures that are
variably- sized. For example:
struct StringDescriptor {
unsigned len;
wchar_t data[0];
}
That's not a legal definition, neither in C nor in C++.
(That's simpler than a real USB StringDescriptor, but it shows
the essential feature, a variably-sized array of wchar_t's
that contain the string data.)
In fact, what you want is a flexible array member: an
incomplete array type as the last element (in which case,
the final declaration should be "wchar_t data[];"). That's
only supported in C99, not C++ nor C90.
Some compilers may allow it as an extension. (I believe
that some compilers also allow the 0 sized array in this
case, again as an extension.)
I know IAR and ARM tools do support it. I believe it is quite
common. Perhaps it is part of the embedded-C++ specs, I have
never checked.
The only compiler I have that accepts it with my usual options
is VC++. G++ will accept it as well, IF you forget to specify
-std=c++98. Never the less, the C standard explicitly forbids
it (and C++ just copies C here).
I would like to be able to declare these at compile time,
and place them in Flash if appropriate:
StringDescriptor might_be_modified( "Some String Value" );
const StringDescriptor cannot_be_modified( "Fixed String Value" );
Is there a clean way to declare StringDescriptor so that
the declarations are simple and don't involve malloc(),
new() or a run- time constructor invocation?
struct StringDescriptor
{
unsigned length ;
wchar_t* data ;
} ;
wchar_t const string_cannot_be_modifed[]
= L"Fixed String Value" ;
StringDescriptor const
cannot_be_modified =
{
sizeof( string_cannot_be_modifed ) - 1,
string_cannot_be_modifed
} ;
As I understand the OP problem, he wants to put them in RO
area and use them directly to answer some request. What he is
actually describing is the answer message (or part of it) and
the layout wanted is the serialized version of the data.
You mean that he wants to be able to do a simple write of the
bytes, with no formatting? That wouldn't be very clean to begin
with. (On the other hand, in some embedded environments, it
might be justified.) The only real way to do that is with a
char const[]; it wouldn't be too difficult to generate them
automatically.
All the code examples I've seen just declare an array of
bytes, which has no type safety and poor readability.
One can argue about readability; for something like the
above, I'll usually use a simple script to generate it,
rather than write it all out by hand.
IMHO, this is the few case when a macro is justified. This is
what we use when describing the layout of the configuration
space in flash on our system.
Nothing against a macro in this case either, but I think that
automatic generation will work better, since it can count the
characters for you (although come to think of it, you could
probably do this with a macro as well, as long as the macro was
only used with string literals).
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34