Contiguous Arrays

John Hascall hascall at atanasoff.cs.iastate.edu
Wed Feb 22 13:07:47 AEST 1989


In article <8943 at alice.UUCP> ark at alice.UUCP (Andrew Koenig) writes:
>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:...

>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.

>For that reason it's hard to see how a C implementation could possibly
>do anything but put x in contiguous memory.


     How about:

	 Assume int's are (say) 2 bytes.  Assume further that your
	 machine is stupid, and all accesses must be on an 8-byte boundary.

	 Suppose x[0] ends up at 0x01000, then
		 x[1] ends up at 0x01008, and
		 x[2]            0x01010, etc....

       address (0x01000 base):    0 1 2 3 4 5 6 7 8 9 a b c d e f 0 1 ...
	 x[?]                     0 0             1 1             2 2

    A non-contiguous array, and the compiler can always generate

	    p <- p + 8

    for p++; since it knows about the 8-byte alignment business.

  Or have I forgotten something?
  John Hascall / ISU Comp Center



More information about the Comp.lang.c mailing list