Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Shao Miller <sha0.miller@gmail.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: [MUDFLAP] Is sizeof(ARRAY[0]) equivalent to sizeof(*ARRAY) ? |
| Date | 2013-01-11 19:30 -0500 |
| Organization | A noiseless patient Spider |
| Message-ID | <kcqar7$asb$1@dont-email.me> (permalink) |
| References | (1 earlier) <0b6510c5-c860-41e8-80e6-9856f6145d7f@googlegroups.com> <kcmfql$bkc$1@dont-email.me> <lna9sflmjj.fsf@nuthaus.mib.org> <kcpj52$lv9$1@dont-email.me> <lntxqnjs1a.fsf@nuthaus.mib.org> |
On 1/11/2013 17:04, Keith Thompson wrote:
> Shao Miller <sha0.miller@gmail.com> writes:
>> On 1/11/2013 11:19, Keith Thompson wrote:
>>> Shao Miller <sha0.miller@gmail.com> writes:
>>> [...]
>>>> MUDFLAP can diagnose stuff that's permitted by C90, such as warning you
>>>> that an index used for iteration has gone too far. Usually you want to
>>>> index one-past an array, at most. Do the 'for' loops have any problems?
>>>
>>> As I'm sure you know, it's legal to construct a pointer one element
>>> past the end of an array, but not to dererence it. Constructing a
>>> pointer before the beginning of an array or more than one element
>>> past the end of it, or deferencing a pointer one past the end of it,
>>> has undefined behavior. I'm not aware of any relevant changes in
>>> this area from C90 to C99 to C11.
>>>
>>> Are you saying that Mudflap complains about stuff that's legal *and
>>> has defined behavior* in C90? If it complained about something
>>> like this:
>>>
>>> int arr[N];
>>> int *ptr;
>>> for (ptr = arr; ptr < arr + N; ptr ++) {
>>> /* ... */
>>> }
>>>
>>> that would IMHO be a serious bug in Mudflap.
>>>
>>> (I haven't analyzed the posted code, but from the discussion I don't
>>> think that's what's going on.)
>>
>> Nah, I tried to be careful and chose to type "index" instead of "point".
>> The code I was criticizing used both an integer index as well as a
>> pointer. Their pointer would never point more than one past the array,
>> but their integer would index two past the array. That seems to be
>> permitted by C90.
>
> It's permitted (in all versions of the language) for a object
> of an integer type to have any value in the range of that type.
> That has nothing at all to do with arrays; there's no association
> (that a compiler can detect) between a given integer value and
> indices into some array object.
>
But that's not so. A compiler can plainly see an integer object being
compared with the bounds of an array, as does the code I was
criticizing. For example:
for (i = 0; i < sizeof arr / sizeof arr[0]; i += 10)
If the count of 'arr' is 4, this alone is sufficient cause to tell the
programmer to have their head checked, with no pointers in sight.
> That is, unless you actually use the integer value as an index into
> a particular array. In that case, exactly the same rules apply as
> for pointers, because the indexing operation is defined in terms
> of pointer arithmetic.
>
> Concretely:
>
> int arr[10] = { 0 };
> int i;
> for (i = 0; i < 10; i += 3) {
> arr[i] = 42;
> }
>
> In successive iterations of the loop, i takes on the values 0, 3,
> 6, and 9. *After* the loop, i==12, which is a perfectly valid value
> for an object of type int. Evaluating `arr[i]` after the loop would
> have undefined behavior, but the program doesn't do that so there's
> no problem. There could be problems if i iterates past INT_MAX,
> but it doesn't do that either.
>
This example is a good one, and rings a bell as being along the lines of
the bonus diagnostic I mentioned.
If I recall correctly, the idea is that whether or not one is using an
integer index or a pointer itself for iteration is hardly any different
to the compiler, so noting that an index goes "out of bounds" is just as
easy as noting that a pointer goes out of bounds, so it's pretty
straightforward to provide a bonus diagnostic, even though the program
might not suffer from undefined behaviour.
>> I seem to recall some bit about this situation being easily detectable
>> and reportable as a bonus diagnostic, perhaps in the hopes of alerting
>> the programmer that there's a better way to write their loop without
>> stepping out of bounds, conceptually.
>
> What would such a diagnostic look like?
>
> Warning: `i' exceeds the bounds of `arr', so evaluating `arr[i]'
> would have undefined behavior, but that never happens so I'm not
> sure what I'm warning you about.
>
Let me try to dig it up and post again...
> A similar snippet using pointers *would* cause undefined behavior,
> and would be worthy of a warning (though I can't quite persuade
> gcc to generate one):
>
> int arr[10] = { 0 };
> int *ptr;
> for (ptr = arr; ptr < arr + 10; ptr += 3) {
> *ptr = 42;
> }
> return 0;
>
> because it actually constructs the invalid pointer value.
>
> Or am I missing something?
>
Not that I know of. :) It's just helpful, because it's usually right...
The loop is usually clearer if it's reworked to avoid the diagnostic.
--
- Shao Miller
--
"Thank you for the kind words; those are the kind of words I like to hear.
Cheerily," -- Richard Harter
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
[MUDFLAP] Is sizeof(ARRAY[0]) equivalent to sizeof(*ARRAY) ? m.labanowicz@gmail.com - 2013-01-09 07:26 -0800
Re: [MUDFLAP] Is sizeof(ARRAY[0]) equivalent to sizeof(*ARRAY) ? James Kuyper <jameskuyper@verizon.net> - 2013-01-09 11:30 -0500
Re: [MUDFLAP] Is sizeof(ARRAY[0]) equivalent to sizeof(*ARRAY) ? Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-01-09 11:58 -0500
Re: [MUDFLAP] Is sizeof(ARRAY[0]) equivalent to sizeof(*ARRAY) ? Robert Wessel <robertwessel2@yahoo.com> - 2013-01-09 12:27 -0600
Re: [MUDFLAP] Is sizeof(ARRAY[0]) equivalent to sizeof(*ARRAY) ? Keith Thompson <kst-u@mib.org> - 2013-01-09 11:22 -0800
Re: [MUDFLAP] Is sizeof(ARRAY[0]) equivalent to sizeof(*ARRAY) ? Shao Miller <sha0.miller@gmail.com> - 2013-01-09 14:39 -0500
Re: [MUDFLAP] Is sizeof(ARRAY[0]) equivalent to sizeof(*ARRAY) ? m.labanowicz@gmail.com - 2013-01-10 02:40 -0800
Re: [MUDFLAP] Is sizeof(ARRAY[0]) equivalent to sizeof(*ARRAY) ? Shao Miller <sha0.miller@gmail.com> - 2013-01-10 08:31 -0500
Re: [MUDFLAP] Is sizeof(ARRAY[0]) equivalent to sizeof(*ARRAY) ? Shao Miller <sha0.miller@gmail.com> - 2013-01-10 08:49 -0500
Re: [MUDFLAP] Is sizeof(ARRAY[0]) equivalent to sizeof(*ARRAY) ? m.labanowicz@gmail.com - 2013-01-10 06:48 -0800
Re: [MUDFLAP] Is sizeof(ARRAY[0]) equivalent to sizeof(*ARRAY) ? Shao Miller <sha0.miller@gmail.com> - 2013-01-10 10:33 -0500
Re: [MUDFLAP] Is sizeof(ARRAY[0]) equivalent to sizeof(*ARRAY) ? Keith Thompson <kst-u@mib.org> - 2013-01-11 08:19 -0800
Re: [MUDFLAP] Is sizeof(ARRAY[0]) equivalent to sizeof(*ARRAY) ? Shao Miller <sha0.miller@gmail.com> - 2013-01-11 12:46 -0500
Re: [MUDFLAP] Is sizeof(ARRAY[0]) equivalent to sizeof(*ARRAY) ? Keith Thompson <kst-u@mib.org> - 2013-01-11 14:04 -0800
Re: [MUDFLAP] Is sizeof(ARRAY[0]) equivalent to sizeof(*ARRAY) ? glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2013-01-11 22:42 +0000
Re: [MUDFLAP] Is sizeof(ARRAY[0]) equivalent to sizeof(*ARRAY) ? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2013-01-12 01:44 +0000
Re: [MUDFLAP] Is sizeof(ARRAY[0]) equivalent to sizeof(*ARRAY) ? Shao Miller <sha0.miller@gmail.com> - 2013-01-11 19:30 -0500
Re: [MUDFLAP] Is sizeof(ARRAY[0]) equivalent to sizeof(*ARRAY) ? Shao Miller <sha0.miller@gmail.com> - 2013-01-11 20:11 -0500
Re: [MUDFLAP] Is sizeof(ARRAY[0]) equivalent to sizeof(*ARRAY) ? glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2013-01-12 01:17 +0000
Re: [MUDFLAP] Is sizeof(ARRAY[0]) equivalent to sizeof(*ARRAY) ? Shao Miller <sha0.miller@gmail.com> - 2013-01-11 20:46 -0500
Re: [MUDFLAP] Is sizeof(ARRAY[0]) equivalent to sizeof(*ARRAY) ? glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2013-01-12 03:14 +0000
Re: [MUDFLAP] Is sizeof(ARRAY[0]) equivalent to sizeof(*ARRAY) ? Keith Thompson <kst-u@mib.org> - 2013-01-12 14:17 -0800
Re: [MUDFLAP] Is sizeof(ARRAY[0]) equivalent to sizeof(*ARRAY) ? Shao Miller <sha0.miller@gmail.com> - 2013-01-12 18:02 -0500
Re: [MUDFLAP] Is sizeof(ARRAY[0]) equivalent to sizeof(*ARRAY) ? Keith Thompson <kst-u@mib.org> - 2013-01-12 15:13 -0800
Re: [MUDFLAP] Is sizeof(ARRAY[0]) equivalent to sizeof(*ARRAY) ? Shao Miller <sha0.miller@gmail.com> - 2013-01-13 16:36 -0500
Re: [MUDFLAP] Is sizeof(ARRAY[0]) equivalent to sizeof(*ARRAY) ? Keith Thompson <kst-u@mib.org> - 2013-01-13 15:39 -0800
Re: [MUDFLAP] Is sizeof(ARRAY[0]) equivalent to sizeof(*ARRAY) ? Shao Miller <sha0.miller@gmail.com> - 2013-01-13 19:17 -0500
Re: [MUDFLAP] Is sizeof(ARRAY[0]) equivalent to sizeof(*ARRAY) ? Keith Thompson <kst-u@mib.org> - 2013-01-13 16:57 -0800
Re: [MUDFLAP] Is sizeof(ARRAY[0]) equivalent to sizeof(*ARRAY) ? Shao Miller <sha0.miller@gmail.com> - 2013-01-13 20:50 -0500
Re: [MUDFLAP] Is sizeof(ARRAY[0]) equivalent to sizeof(*ARRAY) ? Keith Thompson <kst-u@mib.org> - 2013-01-13 19:08 -0800
Re: [MUDFLAP] Is sizeof(ARRAY[0]) equivalent to sizeof(*ARRAY) ? Shao Miller <sha0.miller@gmail.com> - 2013-01-14 00:00 -0500
Re: [MUDFLAP] Is sizeof(ARRAY[0]) equivalent to sizeof(*ARRAY) ? glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2013-01-14 21:21 +0000
Re: [MUDFLAP] Is sizeof(ARRAY[0]) equivalent to sizeof(*ARRAY) ? Shao Miller <sha0.miller@gmail.com> - 2013-01-14 17:50 -0500
Re: [MUDFLAP] Is sizeof(ARRAY[0]) equivalent to sizeof(*ARRAY) ? glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2013-01-14 01:23 +0000
Re: [MUDFLAP] Is sizeof(ARRAY[0]) equivalent to sizeof(*ARRAY) ? Shao Miller <sha0.miller@gmail.com> - 2013-01-13 21:04 -0500
Re: [MUDFLAP] Is sizeof(ARRAY[0]) equivalent to sizeof(*ARRAY) ? Keith Thompson <kst-u@mib.org> - 2013-01-13 19:13 -0800
Re: [MUDFLAP] Is sizeof(ARRAY[0]) equivalent to sizeof(*ARRAY) ? glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2013-01-14 21:37 +0000
Re: [MUDFLAP] Is sizeof(ARRAY[0]) equivalent to sizeof(*ARRAY) ? Keith Thompson <kst-u@mib.org> - 2013-01-11 18:28 -0800
Re: [MUDFLAP] Is sizeof(ARRAY[0]) equivalent to sizeof(*ARRAY) ? Keith Thompson <kst-u@mib.org> - 2013-01-11 18:40 -0800
Re: [MUDFLAP] Is sizeof(ARRAY[0]) equivalent to sizeof(*ARRAY) ? Shao Miller <sha0.miller@gmail.com> - 2013-01-12 04:30 -0500
Re: [MUDFLAP] Is sizeof(ARRAY[0]) equivalent to sizeof(*ARRAY) ? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2013-01-10 14:37 +0000
Re: [MUDFLAP] Is sizeof(ARRAY[0]) equivalent to sizeof(*ARRAY) ? m.labanowicz@gmail.com - 2013-01-10 06:51 -0800
Re: [MUDFLAP] Is sizeof(ARRAY[0]) equivalent to sizeof(*ARRAY) ? m.labanowicz@gmail.com - 2013-01-10 07:41 -0800
Re: [MUDFLAP] Is sizeof(ARRAY[0]) equivalent to sizeof(*ARRAY) ? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2013-01-10 16:55 +0000
Re: [MUDFLAP] Is sizeof(ARRAY[0]) equivalent to sizeof(*ARRAY) ? Shao Miller <sha0.miller@gmail.com> - 2013-01-10 17:41 -0500
Re: [MUDFLAP] Is sizeof(ARRAY[0]) equivalent to sizeof(*ARRAY) ? m.labanowicz@gmail.com - 2013-01-11 03:03 -0800
Re: [MUDFLAP] Is sizeof(ARRAY[0]) equivalent to sizeof(*ARRAY) ? m.labanowicz@gmail.com - 2013-01-11 04:14 -0800
Re: [MUDFLAP] Is sizeof(ARRAY[0]) equivalent to sizeof(*ARRAY) ? James Kuyper <jameskuyper@verizon.net> - 2013-01-11 08:32 -0500
csiph-web