Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #90735 > unrolled thread
| Started by | "David T. Ashley" <dashley@gmail.com> |
|---|---|
| First post | 2016-07-21 11:39 -0700 |
| Last post | 2016-07-21 20:07 -0700 |
| Articles | 4 — 4 participants |
Back to article view | Back to comp.lang.c
Detecting Incomplete Array Initialization at Compile Time? "David T. Ashley" <dashley@gmail.com> - 2016-07-21 11:39 -0700
Re: Detecting Incomplete Array Initialization at Compile Time? Ken Brody <kenbrody@spamcop.net> - 2016-07-21 14:47 -0400
Re: Detecting Incomplete Array Initialization at Compile Time? Keith Thompson <kst-u@mib.org> - 2016-07-21 12:42 -0700
Re: Detecting Incomplete Array Initialization at Compile Time? Tim Rentsch <txr@alumni.caltech.edu> - 2016-07-21 20:07 -0700
| From | "David T. Ashley" <dashley@gmail.com> |
|---|---|
| Date | 2016-07-21 11:39 -0700 |
| Subject | Detecting Incomplete Array Initialization at Compile Time? |
| Message-ID | <58720375-dff5-4abf-9475-4080c0feb740@googlegroups.com> |
Found a bug related to incomplete array initialization yesterday. The form of the bug was:
enum
{
Apples,
Oranges,
Grapefruits,
MaxElements
};
int myarray[MaxElements] =
{
429,
258
//Oops, missing initializer--not intended.
//C-language defaults to 0.
};
Question: Is there any way to detect incomplete initialization at compile-time? (Any obscure feature of the language I'm not aware of?)
Caveat: I've oversimplified the problem for the purposes of example. Also, this occurred in an embedded system, and doing anything at runtime is off the table.
[toc] | [next] | [standalone]
| From | Ken Brody <kenbrody@spamcop.net> |
|---|---|
| Date | 2016-07-21 14:47 -0400 |
| Message-ID | <nmr5cc$fkg$1@dont-email.me> |
| In reply to | #90735 |
On 7/21/2016 2:39 PM, David T. Ashley wrote:
> Found a bug related to incomplete array initialization yesterday. The form of the bug was:
>
> enum
> {
> Apples,
> Oranges,
> Grapefruits,
> MaxElements
> };
>
> int myarray[MaxElements] =
> {
> 429,
> 258
> //Oops, missing initializer--not intended.
> //C-language defaults to 0.
> };
>
> Question: Is there any way to detect incomplete initialization at compile-time? (Any obscure feature of the language I'm not aware of?)
>
> Caveat: I've oversimplified the problem for the purposes of example. Also, this occurred in an embedded system, and doing anything at runtime is off the table.
>
This might do what you want:
enum
{
Apples,
Oranges,
Grapefruits,
MaxElements
};
int myarray[] = /* Leave off the array size here. */
{
429,
258
//Oops, missing initializer--not intended.
//C-language defaults to 0.
};
static char MyArraySizeCheck[ ( sizeof(myarray)/sizeof(myarray[0]) )
== MaxEmements ? 1 : -1 ];
If the size is "wrong", then the compiler will complain about the "-1" array
size on "MyArraySizeCheck".
--
Kenneth Brody
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2016-07-21 12:42 -0700 |
| Message-ID | <lninvykcbj.fsf@kst-u.example.com> |
| In reply to | #90736 |
Ken Brody <kenbrody@spamcop.net> writes:
> On 7/21/2016 2:39 PM, David T. Ashley wrote:
>> Found a bug related to incomplete array initialization yesterday. The form of the bug was:
>>
>> enum
>> {
>> Apples,
>> Oranges,
>> Grapefruits,
>> MaxElements
>> };
>>
>> int myarray[MaxElements] =
>> {
>> 429,
>> 258
>> //Oops, missing initializer--not intended.
>> //C-language defaults to 0.
>> };
>>
>> Question: Is there any way to detect incomplete initialization at compile-time? (Any obscure feature of the language I'm not aware of?)
>>
>> Caveat: I've oversimplified the problem for the purposes of example. Also, this occurred in an embedded system, and doing anything at runtime is off the table.
>>
>
> This might do what you want:
>
> enum
> {
> Apples,
> Oranges,
> Grapefruits,
> MaxElements
> };
>
> int myarray[] = /* Leave off the array size here. */
> {
> 429,
> 258
> //Oops, missing initializer--not intended.
> //C-language defaults to 0.
> };
>
> static char MyArraySizeCheck[ ( sizeof(myarray)/sizeof(myarray[0]) )
> == MaxEmements ? 1 : -1 ];
>
> If the size is "wrong", then the compiler will complain about the "-1" array
> size on "MyArraySizeCheck".
That's a hack to implement a static assert. (I'm not using the word
"hack" negatively.)
If you have C11 support, or a compiler that implements it as an
extension, you can just use static assert:
_Static_assert(sizeof myarray / sizeof myarray[0] == MaxElements,
"Extra or missing elements");
(I'd suggest using #if and #error, but sizeof can't be evaluated in
preprocessor expressions.)
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
[toc] | [prev] | [next] | [standalone]
| From | Tim Rentsch <txr@alumni.caltech.edu> |
|---|---|
| Date | 2016-07-21 20:07 -0700 |
| Message-ID | <kfn60rytlo8.fsf@x-alumni2.alumni.caltech.edu> |
| In reply to | #90735 |
"David T. Ashley" <dashley@gmail.com> writes:
> Found a bug related to incomplete array initialization yesterday.
> {} The form of the bug was:
>
> enum
> {
> Apples,
> Oranges,
> Grapefruits,
> MaxElements
> };
>
> int myarray[MaxElements] =
> {
> 429,
> 258
> //Oops, missing initializer--not intended.
> //C-language defaults to 0.
> };
>
> Question: Is there any way to detect incomplete initialization at
> compile-time? (Any obscure feature of the language I'm not aware
> of?)
>
> Caveat: I've oversimplified the problem for the purposes of example.
> Also, this occurred in an embedded system, and doing anything at
> runtime is off the table.
If the code is being (or can be) compiled under C99 or later,
there is a way that's a little bit clunky but probably not so bad
as to be unusable. First assume there is a STATIC_ASSERT() macro
of a form like those seen as examples from time to time (eg,
using an array bound that is either 1 or -1 depending). Define
the set of initial array values as a macro, eg (disclaimer:
all the code below just typed in by hand, not compiled)
#define myarray_INITIALIZER { \
429, \
258 \
}
Next define the array using the initial values macro to do the
initialization:
int myarray[MaxElements] = myarray_INITIALIZER;
Next do a static assertion to check the initializer count, using
sizeof with a compound literal to see how many there are:
STATIC_ASSERT(
sizeof myarray == sizeof ( (int[]) myarray_INITIALIZER )
);
This technique gives a compile-time check with no run-time cost.
It does require the element type to be supplied a second time,
but that seems unlikely to cause any serious problems.
If the pattern seems burdensome, probably a macro can be defined
that both defines the array and does the size check, without too
much difficulty. Maybe a PITA to write, but once written should
be usable enough to be worth the effort.
I know this is just a rough sketch, hopefully the details
are simple enough so they can be filled in with no problem.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c
csiph-web