"BartC" <> writes:
> "Keith Thompson" <kst-> wrote in message
> news:...
>> "BartC" <> writes:
>>> "Richard Damon" <> wrote in message
>>> news:k4qm0b$jr0$...
>>>> On 10/6/12 5:30 AM, Nick Keighley wrote:
>>>>> As someone remarked this business with "undefined behaviour" is true
>>>>> of pretty much all programming languages (I'm not convinced Godel has
>>>>> anything to contribute to this). To some extent C stresses it more,
>>>>> this is partly because C runs nearly everywhere and has huge numbers
>>>>> of implementations.
>>>
>>>> If we removed pointers into arrays (and passing
>>>> arrays with unspecified bounds), then the compiler could easily add code
>>>> to check the subscripts to the array and trap on error conditions. If we
>>>> want to support pointers into arrays, then these pointers could also be
>>>> made "fatter" to include the bounds of the object they point to (and for
>>>> multidimensional arrays, the bounds for each of the larger arrays the
>>>> array is part of).
>>>
>>> Arrays can have any numbers of dimensions, so would be highly impractical
>>> for any of a thousand possible pointers into an array for each to
>>> duplicate
>>> it's half-dozen or dozen dimensions. You would likely also need different
>>> pointers for each of the sub-dimensions.
>>
>> C's multidimensional arrays are nothing more or less than arrays of
>> arrays. Whatever mechanism existed for 1D arrays would automatically
>> apply to all higher dimensions.
>
> It's more complicated than that.
No, on the C language level it really is exactly that simple.
> You have simple arrays like this:
>
> int A[5][4][3];
Right.
> and more dynamic ones like this:
>
> int ***B,***C;
Those aren't arrays, they're pointers.
> Which might be set up to have dimensions [7][2][4], and [6][6][6].
You can use multi-level pointers like that to create data structures
that behave like dynamic multi-dimensional arrays. To do so, you
have to explicitly allocate memory for each row, and for each row
of pointers to rows, and so on. Each allocation (presumably a call
to malloc() would have to create a properly initialized fat pointer.
[snip]
The current rules of the language permit an implementation to
make all pointers fat, with information propagating through object
definitions, allocations, assignments, and so forth, so that bounds
checks can be made to fail in circumstances where the behavior
is undefined.
--
Keith Thompson (The_Other_Keith)
kst- <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"