Zero Length Arrays Allowed in C Standard?

John Gardiner Myers jm36+ at andrew.cmu.edu
Fri Dec 8 05:37:37 AEST 1989


gwyn at smoke.BRL.MIL (Doug Gwyn) writes:
> In article <11963 at phoenix.Princeton.EDU> tbrakitz at phoenix.Princeton.EDU (Triantaphyllos B\
> yron Rakitzis) writes:
> -Bill Wells says:
> ->... some systems might take that [1] declaration
> ->seriously and give you an error when you access something beyond
> ->the first element of the string. The kind that immediately comes
> ->to mind is debugging interpreters: these, one hopes, will check
> ->for accessing outside the bounds of an array.
> -Wait, hold on a second. If an implementation of C does bounds checking
> -then it isn't C any more.
> 
> Wrong.
[...]
> Just because you aren't familiar with an implementation like Bill
> described does not mean they don't exist or that they're not valid
> C implementations.

Ok, I'll bite.  I claim that the following program is (modulo typos)
strictly conforming.  Could someone please point out which constraint
I missed?

#include <stdio.h>
main()
{
    struct foo_struct {
	int bar;
	char baz[1];
    } *foo;

    foo = (struct foo_struct *) malloc(sizeof(struct foo_struct)+1);
    foo->baz[1] = 1;  /* error? */
    return 0;
}

Note that it is provable that the char pointer (foo->baz + 1) points
within the object returned by malloc.

-- 
_.John G. Myers		Internet: John.G.Myers at andrew.cmu.edu
(412) 268-2984		LoseNet:  ...!seismo!ihnp4!wiscvm.wisc.edu!give!up



More information about the Comp.lang.c mailing list