By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,234 Members | 1,872 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,234 IT Pros & Developers. It's quick & easy.

relational operators on pointers

P: n/a

The subject recently came up in comp.compilers, though I believe that
I asked here before.

If you use relational operators, other than == and !=, on
pointers to different objects, is there any requirement on
the result?
#include <stdio.h>
int main() {
char *one="one";
char *two="two";

if(one<two || one>two) printf("not equal\n");
else printf("equal\n");
}
Is it allowed to print equal?

This question comes up for systems such as JVM that consider object
references as opaque, and the offset must be kept separately. For
operators other than == and != I would only compare the offset part.

JVM has only equality test for object references.

-- glen

Nov 13 '05 #1
Share this Question
Share on Google+
27 Replies


P: n/a
In article <z28Bb.341376$275.1122431@attbi_s53>,
glen herrmannsfeldt <ga*@ugcs.caltech.edu> wrote:
The subject recently came up in comp.compilers, though I believe that
I asked here before.

If you use relational operators, other than == and !=, on
pointers to different objects, is there any requirement on
the result?
#include <stdio.h>
int main() {
char *one="one";
char *two="two";

if(one<two || one>two) printf("not equal\n");
else printf("equal\n");
}
Is it allowed to print equal?

This question comes up for systems such as JVM that consider object
references as opaque, and the offset must be kept separately. For
operators other than == and != I would only compare the offset part.


It invokes undefined behavior, so it can print anything. Or your
computer might explode.

Comparing pointers with <, <=, > or >= is only allowed if both point
into the same object.
Nov 13 '05 #2

P: n/a


glen herrmannsfeldt wrote:

The subject recently came up in comp.compilers, though I believe that
I asked here before.

If you use relational operators, other than == and !=, on
pointers to different objects, is there any requirement on
the result?

#include <stdio.h>
int main() {
char *one="one";
char *two="two";

if(one<two || one>two) printf("not equal\n");
else printf("equal\n");
}

Is it allowed to print equal?

This question comes up for systems such as JVM that consider object
references as opaque, and the offset must be kept separately. For
operators other than == and != I would only compare the offset part.

JVM has only equality test for object references.

-- glen


There is rarely any point in using relational operators other than "=="
and "!=" unless the pointers point to some primitive. Otherwise, you are
comparing addresses of structs or arrays, which would appear to you as
rather random, since you have no control over where the compiler places
them on creation.

In your example above, the only guarantee is that they are not equal,
since the two pointers point to different locations in memory.
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Common User Interface Services
M/S 2R-94 (206)544-5225
Nov 14 '05 #3

P: n/a
In article <z28Bb.341376$275.1122431@attbi_s53>,
glen herrmannsfeldt <ga*@ugcs.caltech.edu> wrote:
If you use relational operators, other than == and !=, on
pointers to different objects, is there any requirement on
the result?
No. (In fact, the C standard calls == and != "equality operators"
just so that it can separate them out from the <, <=, >, and >=
operators; and the only reason to do that is because the latter
four are undefined on pointers.)
This question comes up for systems such as JVM that consider object
references as opaque, and the offset must be kept separately. For
operators other than == and != I would only compare the offset part.


This has historical precedent in "large model" 80x86 C compilers,
which did precisely the same thing. I suspect those 80x86 C
compilers are the main reason the C standard says what it says. :-)

(Now let's see if the .signature comes out at all this time...)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (4039.22'N, 11150.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #4

P: n/a
Christian Bau wrote:
It invokes undefined behavior, so it can print anything. Or your
computer might explode.


Do you know an ANSI-conformant way to make one's computer explode?

Nov 14 '05 #5

P: n/a
Grumble wrote:
Do you know an ANSI-conformant way to make one's computer explode?


Install Windows 3.1 ?

--
Morris Dovey
West Des Moines, Iowa USA
C links at http://www.iedu.com/c
Read my lips: The apple doesn't fall far from the tree.

Nov 14 '05 #6

P: n/a
On 2003-12-09, Grumble <in*****@kma.eu.org> wrote:
Christian Bau wrote:
It invokes undefined behavior, so it can print anything. Or your
computer might explode.


Do you know an ANSI-conformant way to make one's computer explode?


There is no such thing as an exploding computer in Standard C. Please
try a newsgroup more specific to your platform.

-- James
Nov 14 '05 #7

P: n/a

"Chris Torek" <no****@torek.net> schrieb im Newsbeitrag
news:br*********@enews1.newsguy.com...
In article <z28Bb.341376$275.1122431@attbi_s53>,
glen herrmannsfeldt <ga*@ugcs.caltech.edu> wrote:
If you use relational operators, other than == and !=, on
pointers to different objects, is there any requirement on
the result?
No. (In fact, the C standard calls == and != "equality operators"
just so that it can separate them out from the <, <=, >, and >=
operators; and the only reason to do that is because the latter

^^^^^^^^^^^^^^^^^^ four are undefined on pointers.)

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Sorry, Chris, I don't get that.
I understand that you refer to pointers to different objects here, (if the
pointers point to the same object, these operators are well defined if I
understand 6.5.8 v2 of n869 correctly). But I cannot see a connection with
the reason you mention.

kind regards
Robert
Nov 14 '05 #8

P: n/a
Morris Dovey <mr*****@iedu.com> wrote in message news:<sx**************@news.uswest.net>...
Grumble wrote:
Do you know an ANSI-conformant way to make one's computer explode?


Install Windows 3.1 ?


Install Windows XP and show it a Linux boot disk?
Nov 14 '05 #9

P: n/a

"August Derleth" <li***************@yahoo.com> schrieb im Newsbeitrag
news:b6*************************@posting.google.co m...
Morris Dovey <mr*****@iedu.com> wrote in message

news:<sx**************@news.uswest.net>...
Grumble wrote:
Do you know an ANSI-conformant way to make one's computer explode?


Install Windows 3.1 ?


Install Windows XP and show it a Linux boot disk?


Install Linux and show it an XP boot disk? <g, d&r>
Nov 14 '05 #10

P: n/a
In article <br**********@news-rocq.inria.fr>, in*****@kma.eu.org says...
Christian Bau wrote:
It invokes undefined behavior, so it can print anything. Or your
computer might explode.


Do you know an ANSI-conformant way to make one's computer explode?


Assume a nuclear power plant is the device attached to stdout and
parses control rod positioning commands sent to it via the same. It
shouldn't be difficult in such a case to cause not only the
computer, but a large area surrounding it turn into a smoking hole.

--
Randy Howard _o
2reply remove FOOBAR \<,
______________________()/ ()______________________________________________
SCO Spam-magnet: po********@sco.com
Nov 14 '05 #11

P: n/a
On Tue, 09 Dec 2003 08:38:39 -0800, August Derleth wrote:
Morris Dovey <mr*****@iedu.com> wrote in message news:<sx**************@news.uswest.net>...
Grumble wrote:
> Do you know an ANSI-conformant way to make one's computer explode?


Install Windows 3.1 ?


Install Windows XP and show it a Linux boot disk?


I'd think the other way around would be more likely. "Oh, no! You're
*not* going to make me run *THAT* are you???" :)
Nov 14 '05 #12

P: n/a
Chris Torek <no****@torek.net> writes:
In article <z28Bb.341376$275.1122431@attbi_s53>,
glen herrmannsfeldt <ga*@ugcs.caltech.edu> wrote:
If you use relational operators, other than == and !=, on
pointers to different objects, is there any requirement on
the result?


No. (In fact, the C standard calls == and != "equality operators"
just so that it can separate them out from the <, <=, >, and >=
operators; and the only reason to do that is because the latter
four are undefined on pointers.)


They are not undefined if the pointers point within a single
array. Another reason to separate them is that equality
operators and relational operators have different precedence.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Nov 14 '05 #13

P: n/a
In <br*********@enews1.newsguy.com> Chris Torek <no****@torek.net> writes:
In article <z28Bb.341376$275.1122431@attbi_s53>,
glen herrmannsfeldt <ga*@ugcs.caltech.edu> wrote:
If you use relational operators, other than == and !=, on
pointers to different objects, is there any requirement on
the result?


No. (In fact, the C standard calls == and != "equality operators"
just so that it can separate them out from the <, <=, >, and >=
operators; and the only reason to do that is because the latter
four are undefined on pointers.) ^^^^^^^^^^

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Are you sure? ;-)

They are undefined on pointers to different objects only.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #14

P: n/a
>"Chris Torek" <no****@torek.net> schrieb im Newsbeitrag
news:br*********@enews1.newsguy.com...
No. (In fact, the C standard calls == and != "equality operators"
just so that it can separate them out from the <, <=, >, and >=
operators; and the only reason to do that is because the latter ^^^^^^^^^^^^^^^^^^
four are undefined on pointers.)

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


In article <3f***********************@newsreader02.highway.te lekom.at>,
Robert Stankowic <pc******@netway.at> wrote:Sorry, Chris, I don't get that.
I understand that you refer to pointers to different objects here, (if the
pointers point to the same object, these operators are well defined if I
understand 6.5.8 v2 of n869 correctly). But I cannot see a connection with
the reason you mention.


Perhaps it is best if I just say "it was late at night and I was
tired while testing the new trn" :-)

The separation of the operators also gives them different binding
("precedence" in an operator-precedence grammar), so that:

p == q < r == s

parses as:

(p == q) < (r == s)

Hence the separation serves a number of purposes. I was just plain
wrong.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (4039.22'N, 11150.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #15

P: n/a
In article <br**********@news-rocq.inria.fr>,
Grumble <in*****@kma.eu.org> wrote:
Christian Bau wrote:
It invokes undefined behavior, so it can print anything. Or your
computer might explode.


Do you know an ANSI-conformant way to make one's computer explode?


I don't, but I wouldn't be surprised if there was an ANSI standard for
the use of explosives, so there is probably an ANSI-conforming way to
make a computer explode safely without the risk of injuring anyone. I
wouldn't know which newsgroup you would have to check if you are
interested.
Nov 14 '05 #16

P: n/a
Randy Howard wrote:
In article <br**********@news-rocq.inria.fr>, in*****@kma.eu.org says...
Christian Bau wrote:
It invokes undefined behavior, so it can print anything. Or your
computer might explode.


Do you know an ANSI-conformant way to make one's computer explode?

Assume a nuclear power plant is the device attached to stdout and
parses control rod positioning commands sent to it via the same. It
shouldn't be difficult in such a case to cause not only the
computer, but a large area surrounding it turn into a smoking hole.


Usually they have interlocks that will guarantee that software can't
go too wrong. Unless the valve gets stuck open, or something
similar happens.

-- glen

Nov 14 '05 #17

P: n/a
On Tue, 09 Dec 2003 04:42:48 -0600, in comp.lang.c , James Hu
<jx*@despammed.com> wrote:
On 2003-12-09, Grumble <in*****@kma.eu.org> wrote:
Christian Bau wrote:
It invokes undefined behavior, so it can print anything. Or your
computer might explode.


Do you know an ANSI-conformant way to make one's computer explode?


There is no such thing as an exploding computer in Standard C.


Nonsense. Invoke UB in a tight loop on an infinite number of computers
and /eventually/ one of them will explode.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 14 '05 #18

P: n/a
On 2003-12-13, Mark McIntyre <ma**********@spamcop.net> wrote:
On Tue, 09 Dec 2003 04:42:48 -0600, in comp.lang.c , James Hu
<jx*@despammed.com> wrote:
On 2003-12-09, Grumble <in*****@kma.eu.org> wrote:
Christian Bau wrote:
It invokes undefined behavior, so it can print anything. Or your
computer might explode.

Do you know an ANSI-conformant way to make one's computer explode?


There is no such thing as an exploding computer in Standard C.


Nonsense. Invoke UB in a tight loop on an infinite number of computers
and /eventually/ one of them will explode.


Chapter and verse please!

-- James
Nov 14 '05 #19

P: n/a
On Sat, 13 Dec 2003 04:09:00 -0600, in comp.lang.c , James Hu
<jx*@despammed.com> wrote:
On 2003-12-13, Mark McIntyre <ma**********@spamcop.net> wrote:
On Tue, 09 Dec 2003 04:42:48 -0600, in comp.lang.c , James Hu
<jx*@despammed.com> wrote:
On 2003-12-09, Grumble <in*****@kma.eu.org> wrote:
Christian Bau wrote:
> It invokes undefined behavior, so it can print anything. Or your
> computer might explode.

Do you know an ANSI-conformant way to make one's computer explode?

There is no such thing as an exploding computer in Standard C.


Nonsense. Invoke UB in a tight loop on an infinite number of computers
and /eventually/ one of them will explode.


Chapter and verse please!


3.4.3 covers it nicely.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 14 '05 #20

P: n/a
On 2003-12-13, Mark McIntyre <ma**********@spamcop.net> wrote:
On Sat, 13 Dec 2003 04:09:00 -0600, in comp.lang.c , James Hu
<jx*@despammed.com> wrote:
On 2003-12-13, Mark McIntyre <ma**********@spamcop.net> wrote:
On Tue, 09 Dec 2003 04:42:48 -0600, in comp.lang.c , James Hu
<jx*@despammed.com> wrote:

On 2003-12-09, Grumble <in*****@kma.eu.org> wrote:
> Do you know an ANSI-conformant way to make one's computer explode?

There is no such thing as an exploding computer in Standard C.

Nonsense. Invoke UB in a tight loop on an infinite number of computers
and /eventually/ one of them will explode.


Chapter and verse please!


3.4.3 covers it nicely.


I find no mention of "eventually a computer will explode" in that
paragraph.

It doesn't even imply that out of an infinite set of computers, one
will explode if UB is invoked.

And furthermore, if a C program is invoking UB, it is not a strictly
conforming program.

Please stop trolling.

:-p

-- James
Nov 14 '05 #21

P: n/a
James Hu wrote:
On 2003-12-13, Mark McIntyre <ma**********@spamcop.net> wrote:
On Tue, 09 Dec 2003 04:42:48 -0600, in comp.lang.c , James Hu
<jx*@despammed.com> wrote:
There is no such thing as an exploding computer in Standard C.


Nonsense. Invoke UB in a tight loop on an infinite number of computers
and /eventually/ one of them will explode.


Chapter and verse please!


Since it is well-known that UB is an abominable thing to invoke, and since
everybody who has ever watched a science fiction movie knows that fire
makes computers explode (irrespective of the presence or absence of, say,
Semtex or a large bottle of nitroglycerine within the computer's casing), I
suspect that Revelation 21:8 satisfies your requirement for chapter and
verse.

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 14 '05 #22

P: n/a
On Sat, 13 Dec 2003 04:39:22 -0600, in comp.lang.c , James Hu
<jx*@despammed.com> wrote:
On 2003-12-13, Mark McIntyre <ma**********@spamcop.net> wrote:
On Sat, 13 Dec 2003 04:09:00 -0600, in comp.lang.c , James Hu
<jx*@despammed.com> wrote:
On 2003-12-13, Mark McIntyre <ma**********@spamcop.net> wrote:
3.4.3 covers it nicely.
I find no mention of "eventually a computer will explode" in that
paragraph.


It clearly says that undefined behaviour is anything not defined by
the standard. Exploding computers are not defined by the standard. So
therefore they're covered by this section.
It doesn't even imply that out of an infinite set of computers, one
will explode if UB is invoked.
It also doesn't say that your machine will print an error
"segmentation fault: core dumped" or "this program has performed an
illegal instruction" or "a small blue fish has just flown out of your
nostril".
So what?
Please stop trolling.


Stop yourself...
:-)

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 14 '05 #23

P: n/a
On Sat, 13 Dec 2003 11:46:37 +0000 (UTC), in comp.lang.c , Richard
Heathfield <do******@address.co.uk.invalid> wrote:
James Hu wrote:
On 2003-12-13, Mark McIntyre <ma**********@spamcop.net> wrote:
On Tue, 09 Dec 2003 04:42:48 -0600, in comp.lang.c , James Hu
<jx*@despammed.com> wrote:

There is no such thing as an exploding computer in Standard C.

Nonsense. Invoke UB in a tight loop on an infinite number of computers
and /eventually/ one of them will explode.


Chapter and verse please!


I suspect that Revelation 21:8 satisfies your requirement for chapter and
verse.


Presumably the Authorised Version as revised by CLC now reads:

"But for the cowardly, unbelieving, sinners, abominable, murderers,
sexually immoral, sorcerers, idolaters, and all users of undefined
behaviour, their part is in the lake that burns with fire and sulfur,
which is the second death."
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 14 '05 #24

P: n/a
On Sat, 13 Dec 2003 11:46:37 +0000 (UTC), Richard Heathfield
<do******@address.co.uk.invalid> wrote:
<snip>
Since it is well-known that UB is an abominable thing to invoke, and since
everybody who has ever watched a science fiction movie knows that fire
makes computers explode (irrespective of the presence or absence of, say,
Semtex or a large bottle of nitroglycerine within the computer's casing), <snip>


When has fire ever caused a computer to explode? What movie/TV fire
causes is the rapid failure of physical structures, preferentially
downward even in space, usually while not injuring people in the same
room beyond depositing some soot on them.

(Unless you mean "fire" in the military sense of shooting at it. That
causes even inert things to explode.)

What mostly causes explosion of computers, or rather usually displays
including (noncomputer) data and even video monitors, is a physical
impact at some remote location on the sensors or other peripherals
connected electronically or virtually to the monitor(s), or less often
the input of invalid data and/or unauthorized commands.

- David.Thompson1 at worldnet.att.net
Nov 14 '05 #25

P: n/a
Mark McIntyre <ma**********@spamcop.net> spoke thus:
"But for the cowardly, unbelieving, sinners, abominable, murderers,
sexually immoral, sorcerers, idolaters, and all users of undefined
behaviour, their part is in the lake that burns with fire and sulfur,
which is the second death."


Other than the fact that void main() is omitted, that seems reasonable
to me.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #26

P: n/a
"Dave Thompson" <da*************@worldnet.att.net> wrote in message
news:31********************************@4ax.com...
On Sat, 13 Dec 2003 11:46:37 +0000 (UTC), Richard Heathfield
<do******@address.co.uk.invalid> wrote:
<snip>
Since it is well-known that UB is an abominable thing to invoke, and since
everybody who has ever watched a science fiction movie knows that fire
makes computers explode (irrespective of the presence or absence of, say,
Semtex or a large bottle of nitroglycerine within the computer's casing),
<snip>
When has fire ever caused a computer to explode? What movie/TV fire
causes is the rapid failure of physical structures, preferentially
downward even in space, usually while not injuring people in the same
room beyond depositing some soot on them.

(Unless you mean "fire" in the military sense of shooting at it. That
causes even inert things to explode.)

What mostly causes explosion of computers, or rather usually displays
including (noncomputer) data and even video monitors, is a physical
impact at some remote location on the sensors or other peripherals
connected electronically or virtually to the monitor(s), or less often
the input of invalid data and/or unauthorized commands.


Or installing Windows...
Nov 14 '05 #27

P: n/a

"xarax" <xa***@email.com> schrieb im Newsbeitrag
news:oa******************@newsread1.news.pas.earth link.net...
"Dave Thompson" <da*************@worldnet.att.net> wrote in message
news:31********************************@4ax.com...
On Sat, 13 Dec 2003 11:46:37 +0000 (UTC), Richard Heathfield
<do******@address.co.uk.invalid> wrote:
<snip>
Since it is well-known that UB is an abominable thing to invoke, and since everybody who has ever watched a science fiction movie knows that fire
makes computers explode (irrespective of the presence or absence of, say, Semtex or a large bottle of nitroglycerine within the computer's
casing), <snip>

When has fire ever caused a computer to explode? What movie/TV fire
causes is the rapid failure of physical structures, preferentially
downward even in space, usually while not injuring people in the same
room beyond depositing some soot on them.

(Unless you mean "fire" in the military sense of shooting at it. That
causes even inert things to explode.)

What mostly causes explosion of computers, or rather usually displays
including (noncomputer) data and even video monitors, is a physical
impact at some remote location on the sensors or other peripherals
connected electronically or virtually to the monitor(s), or less often
the input of invalid data and/or unauthorized commands.


Or installing Windows...


Or installing *nux - it does seldom cause the computer to explode, but
forgets the mouse, doesn't use DMA if not told explicitely.. and sometimes
at release 69255.1024.1234 it even may..... er - hm

Sorry, could not resist :^/
Robert
Nov 14 '05 #28

This discussion thread is closed

Replies have been disabled for this discussion.