 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
milochen Guest
|
Posted: Tue Feb 14, 2006 2:06 pm Post subject: Question:What's C memory model meaning in M.H.Austern's book |
|
|
I am a freshman to learn Generic Programming
The book I used is Generic Programming and
the STL written by Matthew H. Austern.
In page 26 of this book, Austern wrote that
"This is a fundamental property of C memory model:
Distinct pointers point to distinct memory locations.",
so p and q are both dereferenceable => (p==q iff *p and *q are the same
object)
I don't know what the mean of
the keyword "C memory model" and "C++ memory model" are.
Could anybody have any suggestion for me to read?
......................(Q1)
(I really want to know what the Austern's book talk about clearly)
Now I try to guess what the "p==q iff *p and *q are the same object"
mean.
But I don't know is it in correct.
I want to care about "A and B are same object" here.
When we say "A and B are the same object" , I guess it' s mean that
the value stroed in the address A is equal to the value stored in the
address B ...(i)
Presuming that [0X000AH .. 0X001AH] is a space
for specail object.
So you can get the data in [0X000AH .. 0X001AH]
through the value in the address A, and so do B.
If I get the data in [0X000AH .. 0X001AH] through
value in the address A and I get the data in
[0X0D0AH .. 0X0D1AH] through value in the address B.
We cannot A and B is same object
when all data in [0X000AH .. 0X001AH] is equal to all data in[0X0D0AH
... 0X0D1AH].
Since I guess that (i) is true...
So you can get the data in [0X000AH .. 0X001AH]
through the value in the address A, and so do B.
If I get the data in [0X000AH .. 0X001AH] through
value in the address A and I get the data in
[0X0D0AH .. 0X0D1AH] through value in the address B.
We cannot A and B is same object
when all data in [0X000AH .. 0X001AH] is equal to all data in[0X0D0AH
... 0X0D1AH].
Since I guess that (i) is true...
But whether I got a correct guess? .....(Q2)
After all, or you could give any hint to understand this(Q3)
/****************+++Thank you very much+++*****************/
--
My WebSite
http://home.kimo.com.tw/mlo_chen
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Nicola Musatti Guest
|
Posted: Tue Feb 14, 2006 5:06 pm Post subject: Re: Question:What's C memory model meaning in M.H.Austern's |
|
|
milochen wrote:
| Quote: | I am a freshman to learn Generic Programming
The book I used is Generic Programming and
the STL written by Matthew H. Austern.
In page 26 of this book, Austern wrote that
"This is a fundamental property of C memory model:
Distinct pointers point to distinct memory locations.",
so p and q are both dereferenceable => (p==q iff *p and *q are the same
object)
I don't know what the mean of
the keyword "C memory model" and "C++ memory model" are.
|
It just referes to how C (or C++) assumes memory works. More
specifically, C has a linear memory model where pointers with different
values point to different memory locations.
A counter example is a segmented memory model where each address is
identified by a segment number and an offset within the segment. If
segments overlap there are multiple ways to refer to each memory
location.
[...]
| Quote: | I want to care about "A and B are same object" here.
When we say "A and B are the same object" , I guess it' s mean that
the value stroed in the address A is equal to the value stored in the
address B ...(i)
|
No, we mean that address A and address B are the same. Note that we are
speaking about A and B being the same, not being equal or equivalent.
Hope this helps.
Cheers,
Nicola Musatti
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Francis Glassborow Guest
|
Posted: Tue Feb 14, 2006 6:06 pm Post subject: Re: Question:What's C memory model meaning in M.H.Austern's |
|
|
In article <1139913826.071250.165930 (AT) o13g2000cwo (DOT) googlegroups.com>,
milochen <milochen.bbs (AT) jupiter (DOT) csie.ntu.edu.tw> writes
| Quote: | Now I try to guess what the "p==q iff *p and *q are the same object"
mean.
But I don't know is it in correct.
|
Basically that statement reads, p compares equal to q if and only if the
objects pointed to by p and q are the same object.
--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
kanze Guest
|
Posted: Wed Feb 15, 2006 2:06 pm Post subject: Re: Question:What's C memory model meaning in M.H.Austern's |
|
|
Nicola Musatti wrote:
| Quote: | milochen wrote:
I am a freshman to learn Generic Programming The book I used
is Generic Programming and the STL written by Matthew H.
Austern.
In page 26 of this book, Austern wrote that "This is a
fundamental property of C memory model: Distinct pointers
point to distinct memory locations.", so p and q are both
dereferenceable => (p==q iff *p and *q are the same object)
I don't know what the mean of the keyword "C memory model"
and "C++ memory model" are.
It just referes to how C (or C++) assumes memory works. More
specifically, C has a linear memory model where pointers with
different values point to different memory locations.
|
The memory model is not necessarily linear; it is only linear
within objects. What the C/C++ memory model does assume is:
-- memory is made up of bytes, and these bytes are individually
addressable, and
-- the implementation of pointers, pointer arithmetic, and
pointer comparisons is such that if two pointers of the same
type compare equal, they point to the same object.
(There's a lot more to it, of course, but that is the essential
with regards to the question.)
What the second point basically means is that if p == q, there
is no way a conforming program can determine whether it used *p
or *q, once the dereferencing has taken place.
| Quote: | A counter example is a segmented memory model where each
address is identified by a segment number and an offset within
the segment.
|
Which is a perfectly legal memory model in C/C++. There have,
in fact, been fully compliant ISO C compilers which used such a
model -- at one point, I'd say that it was the most frequent
case.
| Quote: | If segments overlap there are multiple ways to refer to each
memory location.
|
The issue is complex. The C/C++ memory model only concerns what
you can do in C/C++. An implementation has two solutions to
this problem: it can ensure that all the pointers it generates
for a given object do, in fact, use the same representation, or
it can ensure that comparison returns true, even if the
representations are different. The C and C++ compilers I've
used on segmented memory have done both -- all pointers
generated by the compiler had a unique representation, but the
compiler did comparison so that it worked even on pointers
obtained elsewhere.
| Quote: | [...]
I want to care about "A and B are same object" here. When
we say "A and B are the same object" , I guess it' s mean
that the value stroed in the address A is equal to the value
stored in the address B ...(i)
No, we mean that address A and address B are the same. Note
that we are speaking about A and B being the same, not being
equal or equivalent.
|
The concept is called identity. It's fundamental in OO
programming, and probably important in most other paradigms as
well.
--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Guest
|
Posted: Wed Feb 15, 2006 2:06 pm Post subject: Re: Question:What's C memory model meaning in M.H.Austern's |
|
|
Nicola Musatti wrote:
| Quote: | milochen wrote:
I am a freshman to learn Generic Programming
The book I used is Generic Programming and
the STL written by Matthew H. Austern.
In page 26 of this book, Austern wrote that
"This is a fundamental property of C memory model:
Distinct pointers point to distinct memory locations.",
so p and q are both dereferenceable => (p==q iff *p and *q are the same
object)
I don't know what the mean of
the keyword "C memory model" and "C++ memory model" are.
It just referes to how C (or C++) assumes memory works. More
specifically, C has a linear memory model where pointers with different
values point to different memory locations.
|
It does? New to me. As far as I know, it's not linear (except within
objects).
After all, there is a good reason why std::less<T*> isn't simply
operator<
| Quote: | A counter example is a segmented memory model where each address is
identified by a segment number and an offset within the segment. If
segments overlap there are multiple ways to refer to each memory
location.
|
Yes, but this is legal C as well.
Michiel.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Thant Tessman Guest
|
Posted: Wed Feb 15, 2006 6:06 pm Post subject: Re: Question:What's C memory model meaning in M.H.Austern's |
|
|
Francis Glassborow wrote:
| Quote: | In article <1139913826.071250.165930 (AT) o13g2000cwo (DOT) googlegroups.com>,
milochen <milochen.bbs (AT) jupiter (DOT) csie.ntu.edu.tw> writes
Now I try to guess what the "p==q iff *p and *q are the same object"
mean.
But I don't know is it in correct.
Basically that statement reads, p compares equal to q if and only if the
objects pointed to by p and q are the same object.
|
....which, in C++, really means that if you modify the contents of the
object via p, those modifications will be visible through q (and vice
versa).
Nearby, James Kanze writes:
| Quote: | The concept is called identity. It's fundamental in OO
programming, and probably important in most other paradigms as
well.
|
The concept of identity is only relevant when destructive update is
involved. That is, unless you deliberately rely on the ability to modify
the contents of the object in place, and see those modifications through
another pointer or reference, it is irrelevant whether you're dealing
with the original or a copy. So the notion of identity is indeed
fundamental to what most people regard as OO programming, but at the
same time many consider it something to be avoided when possible, and of
course "pure" functional programming bans the notion outright. Even C++
affords many opportunities to avoid destructive update, and this (at
least in my experience) usually leads to cleaner, more robust code.
-thant
--
The number one rule of hunting is: Never, ever, shoot a lawyer. -- C.J.
Watson
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Nicola Musatti Guest
|
Posted: Wed Feb 15, 2006 7:06 pm Post subject: Re: Question:What's C memory model meaning in M.H.Austern's |
|
|
kanze wrote:
| Quote: | Nicola Musatti wrote:
[...]
It just referes to how C (or C++) assumes memory works. More
specifically, C has a linear memory model where pointers with
different values point to different memory locations.
The memory model is not necessarily linear; it is only linear
within objects. What the C/C++ memory model does assume is:
|
You're right of course. Always working on the same platform may harm
one's thought processes :-)
[...]
| Quote: | No, we mean that address A and address B are the same. Note
that we are speaking about A and B being the same, not being
equal or equivalent.
The concept is called identity. It's fundamental in OO
programming, and probably important in most other paradigms as
well.
|
I'm aware of that, but I'm not sure that the exact meaning of the terms
"identity" and "equivalence", not to mention "equality", are well known
to those who are not native speakers of latin influenced languages (and
also to many that are if you consider that "equal" is often translated
to "uguale" in italian, when the latter term is closer to "the same").
Cheers,
Nicola Musatti
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
kanze Guest
|
Posted: Sat Feb 18, 2006 1:06 am Post subject: Re: Question:What's C memory model meaning in M.H.Austern's |
|
|
Nicola Musatti wrote:
| Quote: | kanze wrote:
Nicola Musatti wrote:
[...]
No, we mean that address A and address B are the same.
Note that we are speaking about A and B being the same,
not being equal or equivalent.
The concept is called identity. It's fundamental in OO
programming, and probably important in most other paradigms
as well.
I'm aware of that, but I'm not sure that the exact meaning of
the terms "identity" and "equivalence", not to mention
"equality", are well known to those who are not native
speakers of latin influenced languages (and also to many that
are if you consider that "equal" is often translated to
"uguale" in italian, when the latter term is closer to "the
same").
|
I think the difference is one of technical language vs. general
language. Terms like "identity", "equivalence" and "equality"
may be used with some degree of ambiguity in the everyday
language, but they have a very precise meaning in our particular
technical context.
As for translating "equal" with "uguale", if "equal" is the
mathematical (technical) concept, what is the translation, if
not "ugale"? My technical Italian is pretty bad, but the fact
that the word has other meanings in other contexts doen't
necessarily exclude it from being the correct technical word.
(Note that even in English... when I see "a = b" in a C++
program, I read "a equals b", although that is not at all the
general meaning of "equals".)
--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Guest
|
Posted: Sat Feb 18, 2006 5:06 am Post subject: Re: Question:What's C memory model meaning in M.H.Austern's |
|
|
Nicola Musatti 寫é“:
| Quote: | ....
No, we mean that address A and address B are the same. Note
that we are speaking about A and B being the same, not being
equal or equivalent.
The concept is called identity. It's fundamental in OO
programming, and probably important in most other paradigms as
well.
I'm aware of that, but I'm not sure that the exact meaning of the terms
"identity" and "equivalence", not to mention "equality", are well known
to those who are not native speakers of latin influenced languages (and
also to many that are if you consider that "equal" is often translated
to "uguale" in italian, when the latter term is closer to "the same").
|
I have a definition for operator== in my library:
bool operator==(const T& r) const
This function returns true iff *this and r are not distinguishable
by using any non-private member on the same condition unless
otherwise specified.
All the returned objects or that may be modified by the members
of T should have their operator==(const T&r) defined to prove
the defined equivalence.
Note: Address and __FILE__,__LINE__,.. of an object are not its
own
responsibility.
IJ. Wang
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Guest
|
Posted: Sat Feb 18, 2006 5:06 am Post subject: Re: Question:What's C memory model meaning in M.H.Austern's |
|
|
hi,
what i think is its all about address, two pointer are identical if
they point to same memory location.
if two pointers point to different memory locations they are not
identical whatever the values they contain.
I mean two memory location can contain same value.
try this code out.
#include<stdio.h>
main()
{
int *p,*q;
int x=4,y=4;
p= &x;
q= &y;
if(p == q) /* prints n *
/* if(*p==*q) */
/* prints y */
printf("y");
else
printf("n");
}
cheers
mahind
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Bob Hairgrove Guest
|
Posted: Sat Feb 18, 2006 1:06 pm Post subject: Re: Question:What's C memory model meaning in M.H.Austern's |
|
|
On 17 Feb 2006 23:13:42 -0500, hi_muktesh (AT) yahoo (DOT) com wrote:
| Quote: | hi,
what i think is its all about address, two pointer are identical if
they point to same memory location.
if two pointers point to different memory locations they are not
identical whatever the values they contain.
I mean two memory location can contain same value.
try this code out.
#include<stdio.h
main()
{
int *p,*q;
int x=4,y=4;
p= &x;
q= &y;
if(p == q) /* prints n *
/* if(*p==*q) */
/* prints y */
printf("y");
else
printf("n");
}
cheers
|
Your code has undefined behavior: main() returns int.
Try this, which many people find surprising:
#include <iostream>
#include <ostream>
struct A {
int i;
};
struct B {
int b;
};
struct C : A, B {
int c;
};
int main()
{
using namespace std;
C obj;
A* pa = &obj;
B* pb = &obj;
if ((void*)pa==(void*)pb)
cout << "pa == pb" << endl;
else
cout << "pa != pb" << endl;
cout << "pa's address:\t" << (void*)pa << endl;
cout << "pb's address:\t" << (void*)pb << endl;
return 0;
}
--
Bob Hairgrove
NoSpamPlease (AT) Home (DOT) com
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Nicola Musatti Guest
|
Posted: Mon Feb 20, 2006 1:06 pm Post subject: Re: Question:What's C memory model meaning in M.H.Austern's |
|
|
kanze wrote:
| Quote: | Nicola Musatti wrote:
[...]
I'm aware of that, but I'm not sure that the exact meaning of
the terms "identity" and "equivalence", not to mention
"equality", are well known to those who are not native
speakers of latin influenced languages (and also to many that
are if you consider that "equal" is often translated to
"uguale" in italian, when the latter term is closer to "the
same").
I think the difference is one of technical language vs. general
language. Terms like "identity", "equivalence" and "equality"
may be used with some degree of ambiguity in the everyday
language, but they have a very precise meaning in our particular
technical context.
|
I guess you're right. It still depends on the language in which you
were taught those terms, though.
| Quote: | As for translating "equal" with "uguale", if "equal" is the
mathematical (technical) concept, what is the translation, if
not "ugale"? My technical Italian is pretty bad, but the fact
that the word has other meanings in other contexts doen't
necessarily exclude it from being the correct technical word.
(Note that even in English... when I see "a = b" in a C++
program, I read "a equals b", although that is not at all the
general meaning of "equals".)
|
Actually I wasn't thinking of the mathematical concept, but rather
Orwell's one: "All animals are equal, but some animals are more equal
than others", or "Egualité" on the change you probably have in your
pockets.
Cheers,
Nicola Musatti
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|