Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Ken Brody <kenbrody@spamcop.net> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Detecting Incomplete Array Initialization at Compile Time? |
| Date | 2016-07-21 14:47 -0400 |
| Organization | A noiseless patient Spider |
| Message-ID | <nmr5cc$fkg$1@dont-email.me> (permalink) |
| References | <58720375-dff5-4abf-9475-4080c0feb740@googlegroups.com> |
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
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
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
csiph-web