bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: obstack_printf


From: Jim Meyering
Subject: Re: obstack_printf
Date: Sat, 14 Jun 2008 10:04:02 +0200

Bruno Haible <address@hidden> wrote:
...
> 1) This C code
>
>     const size_t cutoff = 1024;
>     char buf[cutoff];
>
> is understood only by gcc, Tru64 cc, IRIX cc. It fails to compile on other C
> compilers:
>
> Solaris, Sun C 5.0:
> cc -O -DHAVE_CONFIG_H -I. -I..      -g -c obstack_printf.c
> "obstack_printf.c", line 62: integral constant expression expected
>
> HP-UX, "cc -Ae":
> cc: "obstack_printf.c", line 62: error 1502: Array size must be a constant 
> expression.
>
> AIX, "xlc -D_ALL_SOURCE":
> "obstack_printf.c", line 62.12: 1506-195 (S) Integral constant expression 
> with a value greater than zero is required.

Hi Bruno,

Thanks for the list.
Pádraig and I were discussing that in the context of coreutils'
new truncate.c.  I'd like to be able to use the attribute
(though not for cases like the above, where enum is fine)
to mark variables as write-once.  However, I will do that
only if I can automate the check for compliance that they've
all been removed after applying a c99-to-c89 patch like I
already do with -Wdeclaration-after-statement.

Do any of you know of a gcc option to warn about
"const-applied-to-scalar-type"?

> The fix is to define the array size either as a macro or as an enum value.
...
> *** lib/obstack_printf.c.orig 2008-06-14 09:05:52.000000000 +0200
> --- lib/obstack_printf.c      2008-06-14 08:43:36.000000000 +0200
> ***************
> *** 58,73 ****
>        stack-allocated buffer and copy, to reduce the likelihood of a
>        small-size malloc.  Otherwise, print directly into the
>        obstack.  */
> !   const size_t cutoff = 1024;
> !   char buf[cutoff];
...
> --- 58,73 ----
> ! #define CUTOFF 1024
> !   char buf[CUTOFF];

Why not use an enum, as you suggested above?

  enum { CUTOFF = 1024 };
  char buf[CUTOFF];

Then you can copy/paste expressions involving "CUTOFF" into
debuggers that don't know about CPP-defined symbols.

Besides, code without cpp directives just looks better ;-)




reply via email to

[Prev in Thread] Current Thread [Next in Thread]