Sujet : Re: technology discussion → does the world need a "new" C ?
De : jameskuyper (at) *nospam* alumni.caltech.edu (James Kuyper)
Groupes : comp.lang.cDate : 08. Jul 2024, 05:28:53
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v6gl83$s72a$1@dont-email.me>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14
User-Agent : Mozilla Thunderbird
On 7/7/24 20:02, Kaz Kylheku wrote:
...
Ritchie's B language had arrays which contained a pointer to their
first element. Via a hack, it was possible to relocate an array.
In C, such a thing is not simply not required; it is ruled out
by the detailed semantic description of arrays.
The entire representation of an array of size N elements of type
T is contained in the memory block that is sizeo(T)*N bytes wide.
If you copy that block, you have a fully functional copy of the array.
No extra pointer needs to be set up with the correct value.
An implementation which took the following code:
int array1[5], array2[5];
memcpy(array2, array1, sizeof array1);
and translated it into machine code that was the equivalent of
int array1[5], array2[5];
int *_p1 = &array1[0], *_p2 = &array2[0];
memcpy(_p2, _p1, sizeof array1);
would not violate any of the requirements you mention. The key point is
that when you copy the contents of an array to a new location, you
wouldn't want to copy the implicit pointer - it would point at the wrong
location. And if the destination is itself declared as an array, it
would already have an implicit pointer that pointed at the correct location.
I see no point in having implicit pointers, but I don't believe that
they are prohibited.
Furthermore, to dynamically allocate an array, you need only
provide sizeof(T)*N bytes of storage, and not a bit more.
There is simply nowhere in the representation of an array where
a pointer could hide that is part of the representation.
Allocated memory is already accessed through a pointer; there would be
no corresponding need to create an implicit one when there's already an
explicit one.