Re: C ethics question



Servé Laurijssen wrote, On 31/03/07 12:36:
"Harald van D?k" <truedfx@xxxxxxxxx> wrote in message news:1175340222.133195.49390@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Servé Laurijssen wrote:
Recently, I found myself in the following situation:

There is library software written in C which declares some externals like:

struct METER m1;
struct METER m2;

in different sourcefiles and I have no control over these sources.
Then there's functions that operate on these meters and will generate an
event which passes a METER * to identify which meter has been changed.

static void OnChangemeter(struct METER *m)
{
if (m == &m1) ...
if (m == &m2) ...
}

Now the comparison of these pointers is UB in C [...]

No, it isn't.

Please get MSOE to quote properly (it can). All quoted material should be prefixed with a ">" for each level of quoting. As you are doing it how are we meant to see what is quoted and what you wrote? I had to check up for this, and I've fixed it this time.

oh, I thought that you can only compare pointers if they are declared in the same object.

struct METER meters[10];

if (m == &m[1]) ..
etc would have worked but comparing "random" pointers is UB.

You can compare for equality as long as both fall in to the following categories (they do not have to be in the same category):
1) null pointer
2) pointer to an object
3) pointer to 1 past the end of an object

Relational operators are more limited.
--
Flash Gordon
.



Relevant Pages

  • Re: Triplet 630APL meter movement adjustment
    ... the movement had come loose and were floating around the meter dial. ... I also noted that mechanical zero ... There are three weights on the bottom of the meter pointer; ...
    (sci.electronics.repair)
  • Re: Triplet 630APL meter movement adjustment
    ... the movement had come loose and were floating around the meter dial. ... I also noted that mechanical zero ... There are three weights on the bottom of the meter pointer; ...
    (sci.electronics.repair)
  • Re: Triplet 630APL meter movement adjustment
    ... was the same whether the meter was standind up or laying down. ... mechanical zero with the unit in the vertical position and took a ... There are three weights on the bottom of the meter pointer; ...
    (sci.electronics.repair)
  • Re: Triplet 630APL meter movement adjustment
    ... the movement had come loose and were floating around the meter dial. ... I also noted that mechanical zero ... There are three weights on the bottom of the meter pointer; ...
    (sci.electronics.repair)
  • Re: Triplet 630APL meter movement adjustment
    ... the movement had come loose and were floating around the meter dial. ... I also noted that mechanical zero ... There are three weights on the bottom of the meter pointer; ...
    (sci.electronics.repair)