7

Suppose I have the following array:

int list[3]={2,8,9};
printf("%p,%p,%p",(void*)&list[0],(void*)&list[1],(void*)&list[2]);

Is it always guaranteed that &list[0]<&list[1]<&list[2] ?

I had assumed it to be a hard and fast rule while using C, but now have to very sure about it as an OP just asked me about it when I answered his question about endianness

Little endian or Big endian

What gave me second thoughts is the stacks can grow up or down issue.I am not very sure about that so your rigorous answers are appreciated.Thanks.

9
  • 1
    &(list[n]) is I think per standard equivalent to list+n. the array index notation in C is not more than a shorthand for adding values to a pointer. The memory is reserved in a way that ensures that + works
    – wirrbel
    May 6, 2013 at 11:00
  • Are you asking about how things appear in C or how they actually are in memory? May 6, 2013 at 11:06
  • @EricPostpischil Give me your views about both in a nutshell,through a comment.Though I am more interested about actual memory. May 6, 2013 at 11:23
  • Arrays are consecutive and ascending inside the C model. The standard imposes no requirements on physical implementation. May 6, 2013 at 11:25
  • You should specify one or the other, so that answers match the question. If you want to know about both, use two questions. May 6, 2013 at 11:27

3 Answers 3

9

Yes, it's guaranteed that &list[0]<&list[1] and &list[1]<&list[2]. When pointers to elements of the same array are compared, the pointer to the element with the larger subscript will be considered to have larger value. This is specified in C99 6.5.8@5:

pointers to array elements with larger subscript values compare greater than pointers to elements of the same array with lower subscript values

However, it is not guaranteed that the values printed by printf with %p will also follow the same ordering - these values are implementation-defined.

1
  • Does your answer contradict Eric Postpischil's?Please give a little more details, both of you. May 6, 2013 at 10:59
5

From the C standard ("Section 6.2.5 Types"):

...An array type describes a contiguously allocated nonempty set of objects...

Arrays will be allocated contiguously in "memory".

What Eric and Interjay are saying, which is something I didn't consider when I initially wrote this so thank you Eric and Interjay, is that this only applies to the virtual memory addresses.

Your machine and OS most likely use a memory management unit (MMU) which creates a virtual address space (where you are working) and maps this onto physical memory in chunk sized blocks (pages).

So what Eric and Interjay are saying is that although the virtual addresses will be contiguous, the chunks of physical memory they map to may be at different addresses.

 Virtual               Physical
+----------+           +----------+
|          |           |
| VMA pg 1 |---------->| PMA 88 (VMA1)
|          |           |
+----------+           +----------+
|          |\           ...
| VMA pg 2 | \          ...
|          |  \         ...
+----------+   \        ...
             \  \       ...  big gap in physical
              \  \      ...  memory
               \  \     ...
                \  \    ...
                 \  >--+----------+
                  \    |
                   \   | PMA 999 (VMA2)
                    \  |
                     >-+----------+

So, for small arrays (smaller than the page size), this may be true for both VMA and PMA addresses, although most likely PMA != VMA. For arrays larger than the page size, although VMA looks contiguous, PMA may well be disjoint and out of order, as the above diagram tries to show...

Also, I think Interjay and Eric are going a step further and saying that any C address, although contiguous in the C model, might be anywhere in memory. Although this is unlikely as most OS's implement some kind of paging to get a virtual to physical mapping, it can technically be the case I think... this was good to learn to consider, so thanks chaps :)

0
4

If you are asking about how memory appears inside the C model, then arrays appear to be contiguous in C code, and the C expression &list[0] < &list[1] is true.

If you are asking about how actual memory appears inside a C implementation, the C standard does not require any particular arrangement of arrays in memory. Most C implementations use consecutive ascending virtual memory for arrays, but descending addresses would be a simple variation. And, at the level of physical memory, arrays are not generally consecutive, because the map from virtual memory to physical memory is determined by the operating system based on whatever it has available and may even change during execution of a process.

Additionally, there is no guarantee that the strings printed by %p are memory addresses.

6
  • Only that the elements are to be stored in consecutive locations eh? May 6, 2013 at 10:56
  • Doesn't matter if higher to lower or lower to higher addresses? May 6, 2013 at 10:56
  • @Rüppell'sVulture: No, the C standard does not guarantee that array elements be stored consecutively. It only requires that pointer increments within the C model refer to successive array elements. The map from C pointers to machine addresses is up to the C implementation. May 6, 2013 at 10:58
  • @Jimbo Can you please post your rebuttal of Eric's answer in a separate answer? May 6, 2013 at 11:00
  • 2
    @Jimbo: All C behavior is described in terms of an abstract machine. That machine may be implemented any way the C implementation desires. Array addresses must appear to be consecutive in the C model, but the C standard does not impose a requirement on physical storage. In fact, most implementations use consecutive virtual storage, but the underlying physical memory is assigned by the operating system and is not entirely consecutive. May 6, 2013 at 11:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.