[Haskell-cafe] Specify array or list size?
Thomas Davie
tom.davie at gmail.com
Sat May 7 15:20:15 EDT 2005
On May 7, 2005, at 8:07 PM, Marcin 'Qrczak' Kowalczyk wrote:
> Thomas Davie <tom.davie at gmail.com> writes:
>
>
>> I'm not familiar with your C++ example (not being familiar with C++),
>> but I think that it's a bit of a stretch of the imagination to say
>> that C "introduces a variable of type "array of 50 ints"", the fact
>> that this is now an array of 50 integers is never checked at any
>> point in the compilation or run, and I'm not sure it can be even if
>> K&R had wanted to.
>>
>
> The size is taken into account when such array type is an element of
> another array, and by sizeof.
>
> int (*p)[50]; /* p may legally point only to arrays of 50 ints each */
> ++p; /* p is assumed to point into an array, and is moved by one
> element, i.e. by 50 ints */
I'm not sure what you're trying to prove by saying that... There is
still no type information that says that the contents of p are an
array of 50 elements... I can still attempt to access element 51 and
get a runtime memory error. The type of p is still int**, not
"pointer to array of 50 ints"
>> As an example:
>>
>> int bobsArray[5];
>> bobsArray[6] = 23;
>>
>> is not badly typed - it is merely a badly broken program.
>>
>
> Because the array size is not taken into account by indexing. But it's
> a part of the type. These issues are independent, for example in C#
> both are the opposite.
I don't think it is part of the type... Does the compiler ever know
any more about the type of bobsArray other than it's a pointer to an
integer? I think that the above code can be directly translated to:
int *bobsArray;
bobsArray = (int *)malloc(5 * sizeof(int));
bobsArray[6] = 23
Which stores exactly the same about on type information.
Bob
More information about the Haskell-Cafe
mailing list