Path: utzoo!utgpu!utstat!jarvis.csri.toronto.edu!mailrus!cornell!rochester!uhura.cc.rochester.edu!ur-valhalla!badri From: badri@valhalla.ee.rochester.edu (Badri Lokanathan) Newsgroups: comp.lang.c Subject: Re: Contiguous Arrays Summary: Another curiosity Message-ID: <1828@valhalla.ee.rochester.edu> Date: 21 Feb 89 22:59:27 GMT References: <2508@ssc-vax.UUCP> <8943@alice.UUCP> Organization: UR Dept. of Electrical Engg, Rochester NY 14627 Lines: 47 > In article <2508@ssc-vax.UUCP>, dmg@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@alice.UUCP>, ark@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@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