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


Groups > comp.lang.c > #2941

Re: multidimensional heap arrays

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: multidimensional heap arrays
Date 2011-04-26 16:33 -0700
Organization None to speak of
Message-ID <lntydky4dp.fsf@nuthaus.mib.org> (permalink)
References (5 earlier) <91noctFq9bU7@mid.individual.net> <0.bece330a32ff7f1d21e3.20110426210623BST.87r58odbgg.fsf@bsb.me.uk> <91oq24Fq9bU9@mid.individual.net> <lny62wy9ns.fsf@nuthaus.mib.org> <0.03f3b69bd092863fec1b.20110426234112BST.87fwp4d4af.fsf@bsb.me.uk>

Show all headers | View raw


Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
> Keith Thompson <kst-u@mib.org> writes:
>> Ian Collins <ian-news@hotmail.com> writes:
> <snip>
>>> Would it be acceptable for a language change to change (all be it 
>>> subtly) the behaviour of existing code?
>>
>> What behavior would change?
>>
>> Currently, arr is a VLA.  If the rules were changed to make ncol a
>> constant expression, as it is in C++, arr would be an ordinary array
>> object, not a VLA.  As far as I can tell, this would merely make some
>> things legal and well-defined that are currently either constraint
>> violations or undefined behavior.  I can't think of any cases were valid
>> code would either become invalid or change its (currently defined)
>> behavior.  Can you?
>
> I think I can, but only in the most obscure way.  For example:
>
>   const int n = 10;
>   int vla[n];
>   int (*vlap)[n] = &vla;
>   int i = 0;
>   sizeof vlap[i++];
>
> must make i == 1 if vla is a VLA and must leave it at zero otherwise.
>
> For simplicity I've snipped the discussion that got us here so I feel I
> should re-state that I don't think this matters much (to put it mildly).

Interesting case.  I think it illustrates a problem with C99's definition
of sizeof.  C99 6.5.3.4p2 says:

    If the type of the operand is a variable length array type, the
    operand is evaluated; otherwise, the operand is not evaluated
    and the result is an integer constant.

I think this both causes things to be evaluated that needn't be, and
doesn't cause things to be evaluated that should be, though it probably
matters only in obscure cases.

In the simplest case of applying sizeof to a VLA:

    int r = rand() % 10 + 1;
    int vla[r];
    sizeof vla;

I'm not sure what it even means here to evaluate ``vla''.  Certainly it
has to evaluate *something*, but that something is probably an anonymous
variable.  (In particular, it's not r, since r can be modified without
affecting the VLA.)

In this contrived example:

    int i;
    void *p;
    size_t s = sizeof (i = 42, (int(*)[i])p);

the argument to sizeof is not evaluated, even though the evaluation
of ``i = 42'' is needed for the type ``int(*)[i]'' to be valid.
On the other hand, in this case what's really being evaluated is
the size of a pointer, which probably (certainly?) doesn't actually
depend on the value of i.

Of course sizeof can also take a parenthesized type name as its
argument:

    sizeof(int[rand() % 10 + 1]);

This is "evaluating" a type name.  It's not at all clear what
that means, but presumably it includes evaluating the expression
``rand() % 10 + 1''.

Would this work?

    If the operand is an expression, it is not evaluated.  If the
    operand is a parenthesized type name, each full expression that
    is part of the type name is evaluated.

A VLA is the only case I can think of where a type name can include a
non-constant expression.

Hmm.  In ``sizeof (int[42])'' is the expression ``42'' "evaluated"?  C99
6.6.2 implies that it is ("A constant expression can be evaluated during
translation rather than runtime"), but 6.5.3.4 kinda sorta implies that it
isn't.  Does the description of sizeof need to distinguish between
translation-time and execution-time evaluation?

-- 
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