Groups | Search | Server Info | Login | Register


Groups > comp.lang.c > #396719

Re: Loops (was Re: do { quit; } else { })

From Tim Rentsch <tr.17687@z991.linuxsc.com>
Newsgroups comp.lang.c
Subject Re: Loops (was Re: do { quit; } else { })
Date 2026-03-01 21:55 -0800
Organization A noiseless patient Spider
Message-ID <86cy1mg4h5.fsf@linuxsc.com> (permalink)
References (16 earlier) <875xi4cevz.fsf@nosuchdomain.example.com> <86ecwsvunb.fsf@linuxsc.com> <87sel7c3y6.fsf@nosuchdomain.example.com> <865xi3x22i.fsf@linuxsc.com> <87h61nbvlo.fsf@nosuchdomain.example.com>

Show all headers | View raw


Keith Thompson <Keith.S.Thompson+u@gmail.com> writes:

> Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
>
>> Keith Thompson <Keith.S.Thompson+u@gmail.com> writes:
>>
>>> Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
>>>
>>>> Keith Thompson <Keith.S.Thompson+u@gmail.com> writes:
>>>>
>>>>> Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
>>>>>
>>>>>> Keith Thompson <Keith.S.Thompson+u@gmail.com> writes:
>>>>>
>>>>> [...]
>>>>>
>>>>>>> My personal interpretation is that this:
>>>>>>>
>>>>>>> void func(int arr[static 5]) {
>>>>>>> }
>>>>>>>
>>>>>>> int main(void) {
>>>>>>>     int arr[10];
>>>>>>>     func(arr+5);    // OK
>>>>>>>     // func(arr+6); // UB
>>>>>>> }
>>>>>>>
>>>>>>> is valid, because, for example, the last 5 elements of a 10-element
>>>>>>> array object can be treated as a 5-element array object.  gcc seems
>>>>>>> to agree, based on the fact that it warns about func(arr+6) but
>>>>>>> not about func(arr+5).
>>>>>>>
>>>>>>> This is a fundamental part of my mental model of C, but in a few
>>>>>>> minutes of searching I wasn't able to find explicit wording in the
>>>>>>> standard that supports it.
>>>>>>
>>>>>> In N1570, 6.7.6.3 p7.
>>>>>
>>>>> Did you mean to imply that that paragraph supports (or refutes) my
>>>>> statement?  [...]
>>>>
>>>> No.  I posted the reference to say that the cited paragraph supports
>>>> the conclusion that 'func(arr+6)' is undefined behavior.
>>>
>>> I wish you had said so in the first place.  Of course func(arr+6) has
>>> undefined behavior.  Did anyone in this thread say or imply otherwise?
>>
>> In my view the same reasoning about the meaning applies to both
>> cases, so there is no reason to talk about them separately.
>
> Again, of course the behavior of func(arr+6) is undefined.  The behavior
> of func(arr+5) is less clear *to me*.
>
>>>>> """
>>>>> A declaration of a parameter as ??array of _type_?? shall
>>>>> be adjusted to ??qualified pointer to _type_??, where the
>>>>> type qualifiers (if any) are those specified within the [ and ]
>>>>> of the array type derivation.  If the keyword static also appears
>>>>> within the [ and ] of the array type derivation, then for each call
>>>>> to the function, the value of the corresponding actual argument
>>>>> shall provide access to the first element of an array with at least
>>>>> as many elements as specified by the size expression.
>>>>> """
>>>>>
>>>>> The question is whether, for example, the last 5 elements of a
>>>>> 10-element array object can be treated as a 5-element array object.
>>>>> If someone can cite wording in the standard that answers that
>>>>> question, I'd appreciate it.  (I'll be happier if the answer is yes.)
>>>>
>>>> To me it seems obvious that 6.7.6.3 p7 is meant to cover the
>>>> case of 'func(arr+6)' as being undefined behavior.
>>>
>>> But that's not the question I was addressing.  My question is whether
>>> func(arr+5) has defined behavior, based on whether or not a+5 points to
>>> the *first element* of an array.
>>
>> To me it seems obvious that 6.7.6.3 p7 is meant to cover the
>> case of 'func(arr+5)' as satisfying the "shall" requirement,
>> for the same reasons that it is meant to cover the case of
>> 'func(arr+6)' as being undefined behavior.
>
> It does so only if the argument arr+5 provides access to "the first
> element of an array".  You seem to think that it's obvious that it does
> so, based on the disinction you make between "array" and "array object".

The "based on ..." part of this sentence isn't right.  My view here
depends on how array is defined, not on any distinction between
"array" and "array object".

>>>> Note that 6.7.6.3 p7 doesn't say "array object", it says just
>>>> "array".  I believe the choice of wording is neither an accident nor
>>>> an oversight.
>>>
>>> Then please explain what you see as the difference.  Wording in the
>>> standard to support the distinction would be welcome.
>>>
>>> Given `int arr[10];`, do the last 5 elements of arr constitute an
>>> "array"?   Do they constitute an "array object"?  And the same
>>> questions for arr as a whole.
>
> I note that you haven't answered the above questions.  They were not
> rhetorical.  I asked them because I thought that answers to those
> specific questions would help me to understand the distinction that you
> make between "array" and "array object".

My answer to the first two questions is no, and I don't know what
question(s) you mean to ask by the last sentence.  I know that
doesn't help you, and that's why I didn't answer.

>> The meanings follow from a plain understanding of the English
>> language.
>
> I disagree.  Perhaps my understanding of certain combinations of English
> words differs from yours.
>
>> Consider the following example:
>>
>>    typedef struct { int s; float f; } T;
>>
>>    extern void foo( unsigned char blah[ static sizeof(T)-1 ] );
>>
>>    void
>>    bas( T *it ){
>>       foo( (unsigned char *)it + 1 );
>>    }
>>
>> There is no array object.  But surely there is an array (or at
>> least an array is indicatated, and an array is present if 'it' is
>> a valid pointer).  This example satisfies the "shall" requirement
>> in 6.7.6.3 p7, despite there being no array object in sight.
>
> Is there no "region of data storage in the execution environment, the
> contents of which can represent values"?  Cannot that region represent
> a value of type unsigned char[sizeof(T)-1]?  Is a region of data
> storage that can represent values of array type not an array object?
> Again, none of these questions are rhetorical.

Arrays do not, as a whole, have values.  Elements might have values,
but arrays as a whole do not.

> Can you define, preferably in something approaching standardese, what
> you mean by "array" and by "array object", and in particular how they
> differ?

The difference is contextual.  A declaration such as 'int a[5];', at
file scope, declares and defines an array object.  The value of a
pointer returned by malloc() may be used to access an array of
elements, but there is nothing that says the region of memory
allocated by malloc() is an array object, only that it may be
accessed as an array.

> I believe that, in my example above, arr+5 *does* "provide access
> to an array" with at least 5 elements.  (I also believe that that
> "array" is an "array object".)  My difficulty is in demonstrating
> this based on the normative wording in the standard.  *Maybe*
> if you could explain the distinction you make between "array" and
> "array object" it would help.

Suppose we have a file scope declaration

   int foo[10];

and an expression

   (foo+5)[-3];

the behavior of the [] operation is described by the implied +
operation in the section for Additive operators, with P being
the subexpression (foo+5).  The region of memory corresponding
to the fifth through ninth elements of foo surely is an array,
but it is not the "array object" referred to by that paragraph
in the C standard.  This point shows why it is imporant to
distinguish between "array" and "array object".  All array
objects are arrays, but not all arrays are array objects, as
those terms are used in the C standard.

For what it's worth I agree the phrasing used is sometimes
sloppy, but that doesn't change the conclusion that the two
phrases may not be used interchangeably.

Back to comp.lang.c | Previous | NextNext in thread | Find similar


Thread

Re: Loops (was Re: do { quit; } else { }) Tim Rentsch <tr.17687@z991.linuxsc.com> - 2026-03-01 21:55 -0800
  Re: Loops (was Re: do { quit; } else { }) Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2026-03-01 23:37 -0800

csiph-web