Contiguous Arrays
Badri Lokanathan
badri at valhalla.ee.rochester.edu
Wed Feb 22 08:59:27 AEST 1989
> In article <2508 at ssc-vax.UUCP>, dmg at ssc-vax.UUCP (David Geary) writes:
> > Are ALL arrays in C contiguious? If I declare an array:
> >
> > int x[10];
> >
> > am I GUARANTEED that x[0] through x[9] will occupy contiguous memory,
> > or is the compiler free to scatter the elements around in memory?
In article <8943 at alice.UUCP>, ark at alice.UUCP (Andrew Koenig) writes:
> You are guaranteed that if you say
>
> int *p;
>
> p = &x[k]; /* 0 <= k < 9 */
> p++;
>
> then p now points to x[k+1]. It is possible to execute `p++' in
> a separately compiled routine.
On a similar note, just yesterday a friend and I were discussing
tricks to access arrays with different ranges.
For instance, if the array was to be x[101] to x[110] instead of
x[0] to x[9], what is the easiest way to do it?
For automatic arrays one could do
int space[10], *x;
x = &space[0] - 101;
For malloc'd arrays, one could do something like
int *make_array(init_index,final_index)
int init_index, final_index;
{
int *foo;
foo = (int *) malloc((unsigned) (final_index-initial_index+1));
return (foo - init_index);
}
Kind of ugly; only a pascal programmer would think of such grossness (:-)
In any case, I am hard pressed to think of reasons for wanting
to store an automatic array in non-contiguous space.
--
"I care about my fellow man {) badri at ee.rochester.edu
Being taken for a ride, //\\ {ames,cmcl2,columbia,cornell,
I care that things start changing ///\\\ garp,harvard,ll-xn,rutgers}!
But there's no one on my side."-UB40 _||_ rochester!ur-valhalla!badri
More information about the Comp.lang.c
mailing list