Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > C ethics question

Reply
Thread Tools

C ethics question

 
 
Servé Laurijssen
Guest
Posts: n/a
 
      03-31-2007
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?


 
Reply With Quote
 
 
 
 
=?utf-8?B?SGFyYWxkIHZhbiBExLNr?=
Guest
Posts: n/a
 
      03-31-2007
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.

 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      03-31-2007
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.
 
Reply With Quote
 
Servé Laurijssen
Guest
Posts: n/a
 
      03-31-2007

"Harald van D?k" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
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.



 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      03-31-2007
Servé Laurijssen said:

>
> "Harald van D?k" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) ups.com...
> 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.
 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      03-31-2007
Servé Laurijssen wrote, On 31/03/07 12:36:
> "Harald van D?k" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) ups.com...
>> 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
 
Reply With Quote
 
Barry
Guest
Posts: n/a
 
      03-31-2007

"Servé Laurijssen" <(E-Mail Removed)> wrote in message
news:eulh3t$vre$(E-Mail Removed)1.ov.home.nl...
>
> "Harald van D?k" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) ups.com...
> 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.


 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      03-31-2007
"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

 
Reply With Quote
 
Servé Laurijssen
Guest
Posts: n/a
 
      03-31-2007

"CBFalconer" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "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
>



 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      03-31-2007
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
a question of ethics Robert A. Cunningham Digital Photography 164 10-26-2006 10:27 AM
Jon Harrop's strange ethics (was Ray tracer) alex.gman@gmail.com Java 0 08-08-2005 09:33 AM
Ethics Doom MCSE 14 07-29-2004 06:28 PM
Ethics (Photography versus life or death, film or digital) Steven M. Scharf Digital Photography 5 02-13-2004 04:01 PM
ethics of braindumps, read this! roYal MCSE 0 09-08-2003 09:07 PM



Advertisments