473,756 Members | 6,482 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7063
"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***********@p hysik.fu-berlin.de
\______________ ____________ http://www.toerring.de
Nov 14 '05 #3
In article <41************ ****@news.indiv idual.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.l earn.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.l earn.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***********@p hysik.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.t alkaboutprogram ming.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
5246
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
2824
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 Control register 0x1008 Input register 0x100C Output register.
9
2624
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 6.19 (http://www.eskimo.com/~scs/C-faq/q6.19.html). Thanks.
3
2545
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 or unions, these rules apply recursively to the subaggregates or contained unions. If the initializer of a subaggregate or contained union begins with a left brace, the initializers enclosed by that brace and its matching right brace initialize the...
3
11474
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 this a managed struct by adding either the value struct or ref struct I received a compile error stating that mixed types are not supported. I understand this, but I don't know how to create a char array in
3
1358
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 it guaranteed to be contiguous everytime? What If I tried to go out of range but in a knowingly safe manner like the
6
34776
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 elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have...
152
9879
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 = { { 3.14 }, { 42.6 } }; f((double *)array, sizeof array / sizeof **array); return 0;
18
4599
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 far as 320 M. So, myArray is a big as I can get. In theory a 32 bit int is 2.1 billion, but in practice, you cannot fill an array having anywhere near that number of elements. I suspect that it's because every element of an array takes up space...
0
9462
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10046
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9886
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8723
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7259
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5155
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5318
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3817
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3369
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.