By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,617 Members | 1,172 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,617 IT Pros & Developers. It's quick & easy.

Are array members guaranteed to be contiguous in physical memory?

P: n/a
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

Nov 14 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
"Olumide" <50***@web.de> 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
Nov 14 '05 #2

P: n/a
Richard Bos <rl*@hoekstra-uitgeverij.nl> wrote:
"Olumide" <50***@web.de> 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 ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #3

P: n/a
In article <41****************@news.individual.net>,
Richard Bos <rl*@hoekstra-uitgeverij.nl> 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/
Nov 14 '05 #4

P: n/a
Richard Bos wrote:
"Olumide" <50***@web.de> 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

Nov 14 '05 #5

P: n/a
On Mon, 29 Nov 2004 15:21:41 GMT, rl*@hoekstra-uitgeverij.nl (Richard
Bos) wrote in comp.lang.c:
"Olumide" <50***@web.de> 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/~a...FAQ-acllc.html
Nov 14 '05 #6

P: n/a
On Mon, 29 Nov 2004 10:10:30 -0500, "Olumide" <50***@web.de> 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/~a...FAQ-acllc.html
Nov 14 '05 #7

P: n/a
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.
Nov 14 '05 #8

P: n/a
Je***********@physik.fu-berlin.de wrote:
Richard Bos <rl*@hoekstra-uitgeverij.nl> wrote:
"Olumide" <50***@web.de> 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
Nov 14 '05 #9

P: n/a
In <30******************************@localhost.talkab outprogramming.com> "Olumide" <50***@web.de> 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: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.