Are array members guaranteed to be contiguous in physical memory?

Discussion in 'C Programming' started by Olumide, Nov 29, 2004.

  1. Olumide

    Olumide Guest

    Thats the question.

    I know about virtual memory, and the MMU. I just wonder if array members
    guaranteed to be contiguous in physical memory (and if so, why).

    Thanks,

    Olumide
     
    Olumide, Nov 29, 2004
    #1
    1. Advertising

  2. Olumide

    Richard Bos Guest

    "Olumide" <> wrote:

    > I know about virtual memory, and the MMU.


    Not if you think they have anything to do with arrays in C, you don't.
    They're irrelevant in this newsgroup.

    > I just wonder if array members guaranteed to be contiguous in
    > physical memory


    Yes. The Standard demands it.

    > (and if so, why).


    Probably because the ISO C Committee thought (rightly, IMO), that the
    opposite would be a major pain in the backside.

    Richard
     
    Richard Bos, Nov 29, 2004
    #2
    1. Advertising

  3. Olumide

    -berlin.de Guest

    Richard Bos <> wrote:
    > "Olumide" <> wrote:


    >> I know about virtual memory, and the MMU.


    > Not if you think they have anything to do with arrays in C, you don't.
    > They're irrelevant in this newsgroup.


    >> I just wonder if array members guaranteed to be contiguous in
    >> physical memory


    > Yes. The Standard demands it.


    Sorry, but I am not convinced about the "physical" bit - they are
    contiguous in memory of the programs address space, but how the
    address space is realized by the system the program is running on
    isn't something the standard specifies. So, if the program runs on
    a system that uses a virtual memory system the standard doesn't
    care if the array is contiguous in "physical memory" as long as
    what the program sees is that the array elements are contiguous.
    The array elements could be as well distributed randomly over the
    whole _physical_ memory (or in parts or completely be not even in
    normal memory, having been written to e.g. a swap partition).

    Regards, Jens
    --
    \ Jens Thoms Toerring ___ -berlin.de
    \__________________________ http://www.toerring.de
     
    -berlin.de, Nov 29, 2004
    #3
  4. In article <>,
    Richard Bos <> wrote:

    > > I just wonder if array members guaranteed to be contiguous in
    > > physical memory

    >
    > Yes. The Standard demands it.


    Chapter and verse, please.

    In 6.2.5 clause 20 the Standard guarantees that the array is
    contiguously allocated, but the memory is the memory of the abstract
    machine described in 5.1.2.3, not the physical memory of the hosting
    environment.

    > Probably because the ISO C Committee thought (rightly, IMO), that the
    > opposite would be a major pain in the backside.


    Demanding that array members be contiguous in physical memory must be a
    terrible burden on the compiler runtime environment as it has to have
    knowledge about how to request contiguous physical memory from the
    execution environment. I have never seen this done for ordinary array
    members.

    --
    Göran Larsson http://www.mitt-eget.com/
     
    Goran Larsson, Nov 29, 2004
    #4
  5. Olumide

    CBFalconer Guest

    Richard Bos wrote:
    > "Olumide" <> wrote:
    >
    >> I know about virtual memory, and the MMU.

    >
    > Not if you think they have anything to do with arrays in C, you
    > don't. They're irrelevant in this newsgroup.
    >
    >> I just wonder if array members guaranteed to be contiguous in
    >> physical memory

    >
    > Yes. The Standard demands it.
    >
    >> (and if so, why).

    >
    > Probably because the ISO C Committee thought (rightly, IMO), that
    > the opposite would be a major pain in the backside.


    Illustrating once more that you should not answer off-topic
    questions. There is no such demand, because virtual memory is not
    mentioned in the C standard. For example, one array member could
    be in one physical page, and the next in an entirely different
    page.

    --
    "The most amazing achievement of the computer software industry
    is its continuing cancellation of the steady and staggering
    gains made by the computer hardware industry..." - Petroski
     
    CBFalconer, Nov 29, 2004
    #5
  6. Olumide

    Jack Klein Guest

    On Mon, 29 Nov 2004 15:21:41 GMT, (Richard
    Bos) wrote in comp.lang.c:

    > "Olumide" <> wrote:
    >
    > > I know about virtual memory, and the MMU.

    >
    > Not if you think they have anything to do with arrays in C, you don't.
    > They're irrelevant in this newsgroup.
    >
    > > I just wonder if array members guaranteed to be contiguous in
    > > physical memory

    >
    > Yes. The Standard demands it.
    >
    > > (and if so, why).

    >
    > Probably because the ISO C Committee thought (rightly, IMO), that the
    > opposite would be a major pain in the backside.
    >
    > Richard


    Let's get a little more specific about why your answer is incorrect.
    The concept of "physical memory" does not exist in C.

    Assume an array of 8,192 characters on a CHAR_BIT 8 x86 implementation
    under an OS like Windows, Linux, BSD. Given swap files and the like,
    it is entirely possible that element 4095 and 4096 could be in two
    completely different parts of a swap file, and not in physical memory
    at all, at some times.

    And due to the miraculous operation of x86 page tables, even when both
    halves of the array are in memory, and contiguous logical memory at
    that, the two different pages could be megabytes apart in physical
    memory.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Nov 30, 2004
    #6
  7. Olumide

    Jack Klein Guest

    On Mon, 29 Nov 2004 10:10:30 -0500, "Olumide" <> wrote in
    comp.lang.c:

    > Thats the question.
    >
    > I know about virtual memory, and the MMU. I just wonder if array members
    > guaranteed to be contiguous in physical memory (and if so, why).


    No, so there is no why. C guarantees that elements of an array are
    located at contiguous addresses as seen by the C program. It says
    nothing about physical memory, virtual memory, or even swap files.

    > Thanks,
    >
    > Olumide


    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Nov 30, 2004
    #7
  8. Olumide wrote:

    > That's the question.
    >
    > I know about virtual memory, and the MMU.
    > I just wonder if array members guaranteed to be contiguous
    > in physical memory (and if so, why).


    The ANSI/ISO C standards don't specify
    how physical or virtual memory is organized
    but they do use the notion of a sequence of addresses
    in increments of bytes (the size of a char).
    Yes, the elements of an array are guaranteed
    to be contiguous in this address space
    but, if you understand VM and the MMU,
    you know that the physical location
    in the machine physical address space
    may actually be quite different.
    In the typical implementation,
    the address space specified by the C programming language
    maps neatly onto the virtual memory space
    so you are not led too far astray
    if you take them to be equivalent.
     
    E. Robert Tisdale, Nov 30, 2004
    #8
  9. Olumide

    Richard Bos Guest

    -berlin.de wrote:

    > Richard Bos <> wrote:
    > > "Olumide" <> wrote:

    >
    > >> I know about virtual memory, and the MMU.

    >
    > > Not if you think they have anything to do with arrays in C, you don't.
    > > They're irrelevant in this newsgroup.

    >
    > >> I just wonder if array members guaranteed to be contiguous in
    > >> physical memory

    >
    > > Yes. The Standard demands it.

    >
    > Sorry, but I am not convinced about the "physical" bit


    Right. Completely missed that. Never mind me, I'll be in the corner here
    ingesting more caffeine. Of course _physical_ memory can be written with
    a magic marker on the backs of carrier pigeons for all the Standard
    cares.

    Equally of course, no C program can find out whether it is or not
    without going seriously into undefined behaviour territory.

    Richard
     
    Richard Bos, Nov 30, 2004
    #9
  10. Olumide

    Dan Pop Guest

    In <> "Olumide" <> writes:

    >Thats the question.
    >
    >I know about virtual memory, and the MMU. I just wonder if array members
    >guaranteed to be contiguous in physical memory (and if so, why).


    They are guaranteed to be contiguous in the program memory, i.e. to look
    contiguous to the program. No further guarantees. So, if the
    implementation uses virtual memory, an array occupies a contiguous
    *virtual* memory block, but parts of it need not even exist as physical
    memory.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
    Currently looking for a job in the European Union
     
    Dan Pop, Nov 30, 2004
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Newsgroup - Ann
    Replies:
    0
    Views:
    403
    Newsgroup - Ann
    Aug 15, 2003
  2. panbru

    Contiguous memory blocks

    panbru, Aug 5, 2003, in forum: Python
    Replies:
    0
    Views:
    359
    panbru
    Aug 5, 2003
  3. Gary Kuehn
    Replies:
    2
    Views:
    464
    Gary Kuehn
    Jul 19, 2005
  4. Martin
    Replies:
    5
    Views:
    371
    Martin
    Feb 11, 2008
  5. Replies:
    15
    Views:
    1,178
    Tim Rentsch
    Jul 21, 2012
Loading...

Share This Page