C ethics question

Discussion in 'C Programming' started by Servé Laurijssen, Mar 31, 2007.

  1. 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 but in this case it's the
    only way to do it. What would you do in such a situation when you work on a
    project that has to be finished shortly?
    Would you jump high and low to get the library writers to change this into
    non UB code or would you continue knowing that it works on the current and
    future platforms that this code will run on?
    Servé Laurijssen, Mar 31, 2007
    #1
    1. Advertising

  2. 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.
    =?utf-8?B?SGFyYWxkIHZhbiBExLNr?=, Mar 31, 2007
    #2
    1. Advertising

  3. Servé Laurijssen said:

    > 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;
    >
    > [...]
    >
    > static void OnChangemeter(struct METER *m)
    > {
    > if (m == &m1) ...
    > if (m == &m2) ...
    > }
    >
    > Now the comparison of these pointers is UB in C


    Why?

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
    Richard Heathfield, Mar 31, 2007
    #3
  4. "Harald van D?k" <> wrote in message
    news:...
    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.

    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.
    Servé Laurijssen, Mar 31, 2007
    #4
  5. Servé Laurijssen said:

    >
    > "Harald van D?k" <> wrote in message
    > news:...
    > Servé Laurijssen wrote:
    >>
    >> Now the comparison of these pointers is UB in C [...]

    >
    > No, it isn't.
    >
    > oh, I thought that you can only compare pointers if they are declared
    > in the same object.


    You're thinking of relational < <= >= > comparisons. There is no problem
    with equality == != comparisons.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
    Richard Heathfield, Mar 31, 2007
    #5
  6. Servé Laurijssen

    Flash Gordon Guest

    Servé Laurijssen wrote, On 31/03/07 12:36:
    > "Harald van D?k" <> wrote in message
    > news:...
    >> 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
    Flash Gordon, Mar 31, 2007
    #6
  7. Servé Laurijssen

    Barry Guest

    "Servé Laurijssen" <> wrote in message
    news:eulh3t$vre$1.ov.home.nl...
    >
    > "Harald van D?k" <> wrote in message
    > news:...
    > 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.
    >
    > 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 may be confusing this with the behavior of <,>,<=,>=.

    Read the standard sections on relational operators and equality
    operators.
    Barry, Mar 31, 2007
    #7
  8. Servé Laurijssen

    CBFalconer Guest

    "Servé Laurijssen" wrote:
    >

    .... snip ...
    >
    > static void OnChangemeter(struct METER *m)
    > {
    > if (m == &m1) ...
    > if (m == &m2) ...
    > }
    >
    > Now the comparison of these pointers is UB in C but in this case
    > it's the only way to do it. What would you do in such a situation
    > when you work on a project that has to be finished shortly?


    No, it's not UB. Those are equality tests, not relative tests.

    --
    Chuck F (cbfalconer at maineline dot net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net>



    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Mar 31, 2007
    #8
  9. "CBFalconer" <> wrote in message
    news:...
    > "Servé Laurijssen" wrote:
    >>

    > ... snip ...
    >>
    >> static void OnChangemeter(struct METER *m)
    >> {
    >> if (m == &m1) ...
    >> if (m == &m2) ...
    >> }
    >>
    >> Now the comparison of these pointers is UB in C but in this case
    >> it's the only way to do it. What would you do in such a situation
    >> when you work on a project that has to be finished shortly?

    >
    > No, it's not UB. Those are equality tests, not relative tests.


    Thanks for clearing that up, funny thing is I thought this was the case
    because somebody in this group said this. I thought usenet was always
    correct :(
    Shame I cant find that post on google


    >
    > --
    > Chuck F (cbfalconer at maineline dot net)
    > Available for consulting/temporary embedded and systems.
    > <http://cbfalconer.home.att.net>
    >
    >
    >
    > --
    > Posted via a free Usenet account from http://www.teranews.com
    >
    Servé Laurijssen, Mar 31, 2007
    #9
  10. Servé Laurijssen said:

    > Thanks for clearing that up, funny thing is I thought this was the
    > case because somebody in this group said this. I thought usenet was
    > always correct :(


    And so it is - eventually!

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
    Richard Heathfield, Mar 31, 2007
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sean
    Replies:
    1
    Views:
    573
    Cowboy \(Gregory A. Beamer\)
    Oct 20, 2003
  2. =?Utf-8?B?UnlhbiBTbWl0aA==?=

    Quick Question - Newby Question

    =?Utf-8?B?UnlhbiBTbWl0aA==?=, Feb 14, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    634
    Iain Norman
    Feb 16, 2005
  3. =?Utf-8?B?YW5kcmV3MDA3?=

    question row filter (more of sql query question)

    =?Utf-8?B?YW5kcmV3MDA3?=, Oct 5, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    927
    Scott Allen
    Oct 6, 2005
  4. Replies:
    0
    Views:
    883
  5. Ministries In Faith

    "Independent Ethics Counsel"

    Ministries In Faith, May 31, 2009, in forum: Java
    Replies:
    0
    Views:
    324
    Ministries In Faith
    May 31, 2009
Loading...

Share This Page