home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!haven.umd.edu!darwin.sura.net!zaphod.mps.ohio-state.edu!usc!news.service.uci.edu!unogate!mvb.saic.com!macro32
- From: "Andy, Systems Manager" <UDAA055@ELM.CC.KCL.AC.UK>
- Newsgroups: vmsnet.internals
- Subject: RE: C Language question
- Message-ID: <8543225@MVB.SAIC.COM>
- Date: Thu, 8 Oct 92 12:39 BST
- Organization: Macro32<==>Vmsnet.Internals Gateway
- X-Gateway-Source-Info: Mailing List
- Lines: 80
-
- Nigel Arnot writes:
-
- >Question: in C, if one declares char a[I][J], is there anything in the
- >language standard that guarantees that &a[0][0] is the first of a set of
- >I*J contiguous chars, and that a[i+1][0] is guaranteed to be the same
- >storage location as a[i][J] ).
- >
-
- I suppose that it is conceivable for a compiler to use a mechanism for indexing
- that allows the individual elements of an array to be scattered in virtual
- memory. Assuming this is not the case then I think the following explanation
- holds.
-
- A single `dimensioned' array or vector, such as a[i], is contiguous in memory
- in any language. (Obviously, otherwise indexing couldn't work).
-
- So:
-
- int a[i]
-
- simply states that a is an array of contiguous ints (i of them).
-
- Now if we consider a two dimensional array, such as a[i][j], as a vector where
- each element is itself a vector (as I believe the C standard does, although
- maybe only implictly rather than explicitly), then clearly the same rules apply.
-
- So:
-
- int a[i][j]
-
- could be considered as:
-
- typedef int vector[i];
- vector a[j];
-
- where a is still a vector, but each element is a contiguous array of integers.
-
- Clearly all the elements of a must therefore be contiguous!
-
- This tends to be borne out by most C compilers; I dont think I've ever met one
- that allocates the space non contiguously. That's not proof of course, merely
- the implementor's interpretation of the standard but, I think, an accurate one.
-
-
- >A friend asserts that it is, and says his experience on several systems
- >'proves' this. My belief is that there is nothing that says this will
- >always be the case, and that the only constraint is that every valid pointer
- >a[i] will point to J chars. Which of us is right?
-
-
- C is certainly confusing in this area in that, although pointers and arrays are
- often interchangeable, what you are ACTUALLY allowed to do and how the storage
- is allocated, depends on how you originally define the arrays.
-
- For example:
-
- char c[10]="123456789";
- char *c = "123456789";
-
- Initially, the program sees a similar data structure, but the programmer has
- different constraints on each. For example:
-
- 1. c = &x is invalid for the first but ok for the second.
-
- 2. The first declaration allocates storage in writeable memory and then
- initializes it. The second however sets the data up in read-only
- memory and sets c to point at it.
-
- Nigel's assertion is correct only if the array has been declared slightly
- differently:
-
- typedef char string[j]
- string *a[i]
-
- Where a is defined as an array of pointers to string. Although the syntax is
- similar, the meaning is somewhat different.
-
-
- Andy Harper
- Kings College London, England
-