public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Pointers in comparison expressions
@ 2005-07-12 16:24 Mirco Lorenzoni
  2005-07-12 16:32 ` Daniel Berlin
                   ` (3 more replies)
  0 siblings, 4 replies; 26+ messages in thread
From: Mirco Lorenzoni @ 2005-07-12 16:24 UTC (permalink / raw)
  To: gcc

Can a pointer appear in a C/C++ relational expression which doesn't test the 
equality (or the inequality) of that pointer with respect to another pointer? 
For example, are the comparisons in the following program legal code?

/* test.c */
#include <stdio.h>

int main(int argc, char* argv[])
{
	void *a, *b;
	int aa, bb;

	a = &aa;
	b = &bb;
	
	printf("a: %p, b: %p\n", a, b);
	if (a < b) 
		printf("a < b\n");
	else 
		printf("a >= b\n");

	if (b < a) 
		printf("b < a\n");
	else 
		printf("b >= a\n");
	return 0;
}

The execution of the compiled program produces an output like this:
a: 0xbffff55c, b: 0xbffff558
a >= b
b < a

I have compiled this program with gcc and g++, versions 3.4.4 and 4.0.1, and 
(the prehistoric) egcs 2.91.66. None of these compilers has issued any 
warning or error. I have compiled the program above whit these options: -Wall 
--ansi --pedantic .

These are the version information about the compilers: 

Reading specs from /usr/local/lib/gcc/i686-pc-linux-gnu/3.4.4/specs
Configured with: ../gcc-3.4.4/configure --srcdir=../gcc-3.4.4 
--mandir=/usr/local/share/man --infodir=/usr/local/share/info 
--enable-threads --enable-languages=c,c++,f77,java,objc --prefix=/usr/local 
--with-cpu=pentium2 --with-tune=k8 --enable-__cxa_atexit --with-x 
--enable-java-awt=gtk
Thread model: posix
gcc version 3.4.4

Using built-in specs.
Target: i686-pc-linux-gnu
Configured with: ../gcc-4.0.1/configure --srcdir=../gcc-4.0.1 
--mandir=/usr/local/share/man --infodir=/usr/local/share/info 
--enable-threads --enable-languages=c,c++,f95,java,objc --prefix=/usr/local 
--with-cpu=pentium2 --with-tune=k8 --enable-__cxa_atexit --with-x 
--enable-java-awt=gtk
Thread model: posix
gcc version 4.0.1

Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/egcs-2.91.66/specs
gcc version egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)

I think that even if the use of relational operators other than '==' and '!=' 
is legal with pointers, the compiler should issue a warning (when the option 
-Wall is used), as it does for assignment, used as truth values, not 
surrounded with parentheses.

Thanks in advance for your reply.
Regards,
		Mirco Lorenzoni

P.S.
I'm not a list subscriber. Send me a copy of your reply, please.

^ permalink raw reply	[flat|nested] 26+ messages in thread
* Re: Pointers in comparison expressions
@ 2005-07-13 13:53 Morten Welinder
  0 siblings, 0 replies; 26+ messages in thread
From: Morten Welinder @ 2005-07-13 13:53 UTC (permalink / raw)
  To: gcc

> Relational tests between pointers is only allowed by
> the ISO C standard if the two pointers point into the
> same array, or if a pointer points to exactly one byte
> beyond the array.

There actually is a way to compare arbitrary data pointers
within the C standards: you send the pointers through a printf-
type function using "%p" and strcmp the results.  That'll give
you some kind of ordering, though not necessarily the obvious
one.

(No, I am not serously suggesting that anyone in their right mind
you actually do so.  Why does this keyboard have a smiley key?)

Note, btw., that such use of %p also rules out using a garbage
collector for C and C++.  It simply cannot work if some of your
pointers have been sent off by email to the other end of the
world.  At the very least you would have to consider any pointer
that made it through %p to be live forever.

Morten

^ permalink raw reply	[flat|nested] 26+ messages in thread
* Re: Pointers in comparison expressions
@ 2005-07-24  1:13 Paul Schlie
  2005-07-26  4:43 ` Geoff Keating
  0 siblings, 1 reply; 26+ messages in thread
From: Paul Schlie @ 2005-07-24  1:13 UTC (permalink / raw)
  To: Geoffrey Keating; +Cc: mrc.lrn, gcc

> Geoffrey Keating wrote:
>> Mirco Lorenzon wrote:
>>
>> .., are comparisons in the following program legal code?
>
> No.
>
>> ...
>> void *a, *b;
>> ...
>> if (a < b) 
>
> Because 'a' and 'b' are not part of the same array,
> the behaviour is undefined.

Although I don't mean to contest the conclusion, I do find it curious that
as all pointer values referencing unique objects must be correspondingly
unique, it would follow that they will be correspondingly ordered with
respect to each other.  Therefore although technically undefined, it seems
quite reasonable to expect an implementation to support ordered inequality
comparisons between arbitrary pointers to equivalent effective types?

As it would seem otherwise impossible for an implementation to support the
ability to write code which enables the relative comparison of generalized
memory pointers unless one were to explicitly declare a union of an array
of all potentially allocateable memory, and all explicitly and implicitly
declared objects; which doesn't seem reasonable?




^ permalink raw reply	[flat|nested] 26+ messages in thread

end of thread, other threads:[~2005-07-26  4:43 UTC | newest]

Thread overview: 26+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-07-12 16:24 Pointers in comparison expressions Mirco Lorenzoni
2005-07-12 16:32 ` Daniel Berlin
2005-07-12 16:53   ` Dave Korn
2005-07-12 21:00     ` Andreas Schwab
2005-07-12 21:42     ` Erik Trulsson
2005-07-12 22:09       ` Joe Buck
2005-07-12 22:28         ` Erik Trulsson
2005-07-12 22:38           ` Falk Hueffner
2005-07-12 23:17             ` Joe Buck
2005-07-12 23:28               ` Falk Hueffner
2005-07-13  2:02                 ` Joe Buck
2005-07-13 21:42                 ` Olivier Galibert
2005-07-12 23:27             ` Michael Meissner
2005-07-12 22:46           ` Joe Buck
2005-07-16 11:49       ` Vincent Lefevre
2005-07-17 16:55         ` Paul Koning
2005-07-18  1:49           ` Vincent Lefevre
2005-07-18  3:13             ` D. Hugh Redelmeier
2005-07-18 13:13               ` Paul Koning
2005-07-18 13:09             ` Paul Koning
2005-07-12 17:03 ` chris jefferson
2005-07-12 23:18 ` Michael Meissner
2005-07-22 23:20 ` Geoffrey Keating
2005-07-13 13:53 Morten Welinder
2005-07-24  1:13 Paul Schlie
2005-07-26  4:43 ` Geoff Keating

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).