Are array members guaranteed to be contiguous in physical memory?

O

Olumide

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
 
R

Richard Bos

Olumide said:
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
 
J

Jens.Toerring

Not if you think they have anything to do with arrays in C, you don't.
They're irrelevant in this newsgroup.
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
 
G

Goran Larsson

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

Yes. The Standard demands it.[/QUOTE]

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

CBFalconer

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


Yes. The Standard demands it.


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

Jack Klein

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


Yes. The Standard demands it.


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

Jack Klein

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

E. Robert Tisdale

Olumide said:
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.
 
R

Richard Bos

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
 
D

Dan Pop

In said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top