On 08/05/2011 05:19 PM, Paul Eggert wrote:
On 08/05/2011 03:33 PM, Bruno Haible wrote:
int a = PATH_MAX;

Shouldn't this be:

static int a[PATH_MAX];
Maybe. Or possibly:
   enum { a = PATH_MAX };

Are there cases when these three declarations don't all succeed and don't
all fail?

Oh yes.  :-)

"int a = PATH_MAX;" would allow a float PATH_MAX, which presumably
you'd rather reject.  "static int a[PATH_MAX];" might warn you about
an array too large if PATH_MAX == SIZE_MAX; I'd make it "static char
a[PATH_MAX];".  "enum { a = PATH_MAX };" won't work if PATH_MAX
exceeds INT_MAX, and also it doesn't check that PATH_MAX is positive;
these problems are both fixable, but since the intended use of
PATH_MAX is likely array sizes, the array's probably your best bet.

What if we go with the array approach, but without risking overallocation problems? Would this work for a single-byte array:

static char a[PATH_MAX / PATH_MAX];

or is it worth considering the constant context in bit field width:

struct s {
  int a : PATH_MAX / PATH_MAX;
} s1;

--
Eric Blake   ebl...@redhat.com    +1-801-349-2682
Libvirt virtualization library http://libvirt.org

Reply via email to