Re: C vs. C++



CBFalconer <cbfalconer@xxxxxxxxx> writes:
James Kuyper wrote:
CBFalconer wrote:
REH wrote:

... snip ...

There are a lot of things C++ has to improve portability. For
example, unlike C, C++ has a portable mechanism for comparing
unrelated pointers. The next version of the standard will
include portable mechanisms for doing such things as memory
fences and atomic operations.

C can do that also, with an equal/notequal comparison guaranteed.
What it can't do is compare pointers for relative position, if
both pointers are not to areas in a single object.

Yes, that is precisely what REH presumably meant to refer to;
std::less<T*> provides a portable method for comparing unrelated
pointers for relative position, despite the issue you describe:

This is caused by the fact that there are NO restrictions on
where to get memory. Thus pointers can include a lot more
information than just the offset in some area.

For example, imagine a system where, after writing 0 to ioport 5,
memory addresses 1000 to 2000 address memory on one chip. After
writing 1 to ioport 5, those addresses reach another chip. There
is NO way to define greater or smaller addresses between the
chips. Yet pointers have to be able to access both!

There is a way. In order to uniquely identify that memory location,
the '0' and '1' that are most recently written to the port are part
of the address. Otherwise 1234 in bank 0 and 1234 in bank 1 would
be compared equal, which they mustn't be. How this extra bit is
included in the numeric value being compared is arbitrary (and thus
intrinsically non-portable, as you say (snipped)).

Phil
--
I tried the Vista speech recognition by running the tutorial. I was
amazed, it was awesome, recognised every word I said. Then I said the
wrong word ... and it typed the right one. It was actually just
detecting a sound and printing the expected word! -- pbhj on /.
.



Relevant Pages

  • Re: C vs. C++
    ... example, unlike C, C++ has a portable mechanism for comparing ... portable mechanisms for doing such things as memory fences and ... What it can't do is compare pointers for relative position, ...
    (comp.lang.c)
  • Re: C programming in 2011
    ... a pointer to ijkl:000h or i000:jklh before comparing pointers. ... counter doesn't have to imply that the *offset* is signed.. ... register and contains a 32-bit value. ...
    (comp.lang.c)
  • Re: map.find doesnt find
    ... Basically the map.findmethod fails to find a match for a key that I ... The method is comparing pointers to determine if the objects are ... In my case the map key is of type const wchar_t* so this is ... Key type is a pointer so mapcompares those pointers, ...
    (microsoft.public.vc.stl)
  • Re: 74HC123 and long pulse
    ... Multiple memory banks, weak instruction ... existing C code because pointers don't work well with multiple memory ... You're comparing a PIC to a real CPU. ... Do you actually need to use C (with pointers, ...
    (sci.electronics.design)
  • Re: C vs. C++
    ... example, unlike C, C++ has a portable mechanism for comparing ... unrelated pointers. ... where to get memory. ... those addresses reach another chip. ...
    (comp.lang.c)