473,396 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Are array members guaranteed to be contiguous in physical memory?

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
9 7035
"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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Gary Kuehn | last post by:
Is Reserve guaranteed to allocate contiguous memory? How safe is the following: vector<char> vbuff; int sz = numeric_limits<short int>::max();
4
by: Thomas Matthews | last post by:
Hi, I'm writing code for an embedded system. In the system a Timer has 4 memory mapped registers of 32-bit lengths in contiguous locations: Timer 0: 0x1000 Configuration register 0x1004...
9
by: buda | last post by:
Hi, I've been wondering for a while now (and always forgot to ask :) what is the exact quote from the Standard that forbids the use of (&array) (when x >= number_of_columns) as stated in the FAQ...
3
by: Eric Laberge | last post by:
Aloha! I've been reading the standard (May '05 draft, actually) and stumbled across this: 6.7.1 Initialization §20 "If the aggregate or union contains elements or members that are aggregates...
3
by: GrkEngineer | last post by:
I recently had to use someone's struct from a native app to receive data over Udp. The struct has a array member which looked like this: struct sensorHdr{ char sName; }; When I tried to make...
3
by: Jesika | last post by:
Hi, I have a cumbersome question: Given the following declaration: string varray = new string; (15 blocks of memory) Does it allocate a contiguous blocks of memory or not? If it does, is...
6
by: Kannan | last post by:
Hi, I have question about character array initialization. In section 6.7.8 paragraph number 21, it's given that "If there are fewer initializers in a brace-enclosed list than there are...
152
by: vippstar | last post by:
The subject might be misleading. Regardless, is this code valid: #include <stdio.h> void f(double *p, size_t size) { while(size--) printf("%f\n", *p++); } int main(void) { double array = { {...
18
by: raylopez99 | last post by:
The maximum int for an array on my machine (a Pentium IV with 2 GB RAM) is < 330 Million...before you get an "out of memory" exception. I simply filled an array of this size with ints...I got as...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.