Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #2945

Re: multidimensional heap arrays

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: multidimensional heap arrays
Date 2011-04-26 17:38 -0700
Organization None to speak of
Message-ID <lnipu0y1dr.fsf@nuthaus.mib.org> (permalink)
References (7 earlier) <91oq24Fq9bU9@mid.individual.net> <0.967ba25c6450811178a3.20110426224949BST.87liywd6o2.fsf@bsb.me.uk> <91ouo6Fq9bU12@mid.individual.net> <0.bae52d41338584cfc66d.20110427003859BST.87aafcd1m4.fsf@bsb.me.uk> <91p3udFq9bU13@mid.individual.net>

Show all headers | View raw


Ian Collins <ian-news@hotmail.com> writes:
> On 04/27/11 11:38 AM, Ben Bacarisse wrote:
>> Ian Collins<ian-news@hotmail.com>  writes:
>>> On 04/27/11 09:49 AM, Ben Bacarisse wrote:
>> <snip>
>>>> In addition, the change is very sight.  A program that behaves
>>>> differently were a VLA to become a plain arrays is already something of
>>>> a monster.  I can come up with such programs, but they are all very
>>>> contrived.  There may be a natural example, but it seems unlikely.
>>>
>>> I was trying to come up with one and the first example I conjured up
>>> surprised me:
>>>
>>> typedef char C[16];
>>>
>>> void f( C* c ) {}
>>>
>>> int main(void)
>>> {
>>>    char c[16];
>>>
>>>    f(&c );
>>>
>>>    const unsigned n = 15;
>>>
>>>    char cc[n];
>>>
>>>    f(&cc );
>>> }
>>>
>>> Compiles fine as c99 (which was the surprise),
>>
>> Hmm...  At first glance it looks like a diagnostic is required.  6.7.5.2
>> p6 says:
>>
>>    "For two array types to be compatible, both shall have compatible
>>    element types, and if both size specifiers are present, and are
>>    integer constant expressions, then both size specifiers shall have the
>>    same constant value. If the two array types are used in a context
>>    which requires them to be compatible, it is undefined behavior if the
>>    two size specifiers evaluate to unequal values."
>>
>> so are '&cc' and the parameter 'c' pointers to compatible types?  This
>> paragraph does not say.  Maybe it is specified somewhere else, but I
>> can't find it.
>
> Me neither, that's what surprised me.
>
>> I think the intent of the last sentence is to make the program's
>> behaviour is undefined, but that would imply the need for a diagnostic.
>>
>>> but fails as C++ (type
>>> mismatch).  What did surprise me was the VLA type matching even though
>>> the size differed.  Changing the declaration of cc to cc[15] did
>>> produce the expected compiler warnings.
>>
>> The standard can't mandate a type compatibility test that requires an
>> arbitrary non-constant expression to be evaluated, so in one way it is
>> not surprising that there is no error message.  Unfortunately I can't
>> square that with the requirement to issue a diagnostic when a pointer to
>> an incompatible type is passed to a function.
>
> I agree.  I tried the code with both gcc 4.5.1 (-ansi -Wall -pedantic 
> -std=c99) and Sun c99.
>
> I thought a VLA would always have an incompatible type, given its size 
> isn't known at compile time.  In cases like this, I miss C++'s typeid!

C99 6.7.5.2p6:

    For two array types to be compatible, both shall have compatible
    element types, and if both size specifiers are present, and are
    integer constant expressions, then both size specifiers shall
    have the same constant value. If the two array types are used in
    a context which requires them to be compatible, it is undefined
    behavior if the two size specifiers evaluate to unequal values.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

multidimensional heap arrays Jack Boot <jackboot@mailinator.com> - 2011-04-25 19:36 +0000
  Re: multidimensional heap arrays Francois Grieu <fgrieu@gmail.com> - 2011-04-25 23:16 +0200
  Re: multidimensional heap arrays Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-04-25 22:35 +0100
    Re: multidimensional heap arrays Anand Hariharan <mailto.anand.hariharan@gmail.com> - 2011-04-25 21:19 -0700
      Re: multidimensional heap arrays Francois Grieu <fgrieu@gmail.com> - 2011-04-26 08:19 +0200
        Re: multidimensional heap arrays Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-04-26 12:19 +0100
          Re: multidimensional heap arrays Ian Collins <ian-news@hotmail.com> - 2011-04-26 23:25 +1200
            Re: multidimensional heap arrays Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-04-26 21:06 +0100
              Re: multidimensional heap arrays Ian Collins <ian-news@hotmail.com> - 2011-04-27 08:59 +1200
                Re: multidimensional heap arrays Keith Thompson <kst-u@mib.org> - 2011-04-26 14:39 -0700
                Re: multidimensional heap arrays Ian Collins <ian-news@hotmail.com> - 2011-04-27 10:03 +1200
                Re: multidimensional heap arrays Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-04-26 23:41 +0100
                Re: multidimensional heap arrays Keith Thompson <kst-u@mib.org> - 2011-04-26 16:33 -0700
                Re: multidimensional heap arrays Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-04-27 04:05 +0100
                Re: multidimensional heap arrays Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-04-26 22:49 +0100
                Re: multidimensional heap arrays Ian Collins <ian-news@hotmail.com> - 2011-04-27 10:19 +1200
                Re: multidimensional heap arrays Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-04-27 00:38 +0100
                Re: multidimensional heap arrays Ian Collins <ian-news@hotmail.com> - 2011-04-27 11:48 +1200
                Re: multidimensional heap arrays Keith Thompson <kst-u@mib.org> - 2011-04-26 17:38 -0700
                Re: multidimensional heap arrays Ian Collins <ian-news@hotmail.com> - 2011-04-27 13:08 +1200
                Re: multidimensional heap arrays Tim Rentsch <txr@alumni.caltech.edu> - 2011-04-26 18:31 -0700
                Re: multidimensional heap arrays Ian Collins <ian-news@hotmail.com> - 2011-04-27 14:01 +1200
                Re: multidimensional heap arrays Tim Rentsch <txr@alumni.caltech.edu> - 2011-04-27 00:04 -0700
                Re: multidimensional heap arrays Ian Collins <ian-news@hotmail.com> - 2011-04-27 19:58 +1200
                Re: multidimensional heap arrays Ian Collins <ian-news@hotmail.com> - 2011-04-27 20:03 +1200
                Re: multidimensional heap arrays Keith Thompson <kst-u@mib.org> - 2011-04-27 00:03 -0700
                Re: multidimensional heap arrays Tim Rentsch <txr@alumni.caltech.edu> - 2011-04-26 18:20 -0700
                Re: multidimensional heap arrays Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-04-27 03:59 +0100
                Re: multidimensional heap arrays Tim Rentsch <txr@alumni.caltech.edu> - 2011-04-27 00:24 -0700
    Re: multidimensional heap arrays "io_x" <a@b.c.invalid> - 2011-04-26 09:42 +0200
  Re: multidimensional heap arrays Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-04-25 20:50 -0400
  Re: multidimensional heap arrays James Kuyper <jameskuyper@verizon.net> - 2011-04-25 22:57 -0400

csiph-web