Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #122784
| Subject | Re: Equal yet different pointers |
|---|---|
| Newsgroups | comp.lang.c |
| References | <pointers-20171116105459@ram.dialup.fu-berlin.de> <oujpb3$b9s$1@dont-email.me> <elePB.43928$WQ.6569@fx10.am4> <ouk4q6$ql4$1@dont-email.me> |
| From | bartc <bc@freeuk.com> |
| Message-ID | <CfpPB.21633$UP1.12104@fx31.am4> (permalink) |
| Organization | virginmedia.com |
| Date | 2017-11-16 23:34 +0000 |
On 16/11/2017 13:45, David Brown wrote:
> On 16/11/17 12:09, bartc wrote:
>> I think you ought to look around for a new one, because that one can't
>> be relied up to do what you want and regardless of optimisation setting.
>
> I don't rely on gcc to do what I /want/. I rely on it to do what I tell
> it.
And what have you told it here?
You have p and q both as int pointers, both pointing to location
0x12345, which currently contains the value 2.
You've told the compiler to write the value 3 to that location via
pointer p.
But then you tell it to read the same location via pointer q, and it
says it's still 2. What the hell...?
I haven't mentioned C yet; these low level concepts are universal. What
is universally expected is that you write 3 via p, and read back 3 via q.
If I did the exact same thing in assembly, it would work. If I did it in
my language it would work. If do it in most C compilers - including my C
compiler - it will work.
But not gcc -O3 which chooses to do something different, something that
isn't even compatible with itself, using gcc -O0.
And probably, dressing up the same operation but so that p isn't a
simple variable, or its initialisation is not visible to the compiler,
then likely it's -O3 behaviour will change once more.
To me, that's an unstable compiler. Mine at least will always do what I
say because it doesn't have a mind of its own!
> If you think it is the compiler's job to read your mind and figure out
> what you want it to do, then you are in the wrong profession.
You've got it back to front. MY compiler has done exactly what I've told
it. Yours is just making it up as it goes along.
>> All tests were for x64 and with optimisation either on or off (if the
>> compiler had such a setting). Only gcc -O3 gave the unexpected result:
>>
>
> I tested this, and looked at the generated assembly:
>
> #include <string.h>
>
> int foo(void)
> {
> int i = 1;
> int j = 2;
> int * p = &i + 1;
> int * q = &j;
> if (!memcmp(&p, &q, sizeof(p))) {
> *p = 3;
> return *q;
> }
> return *q;
> }
>
> gcc with -O1 upwards gives:
>
> foo():
> mov eax, 2
> ret
So you've told it do the memcmp, but it's totally disregarded your
instructions. I see you've got your compiler well-trained!
>
> MSVC does the same at -O2, and as noted icc with optimisation generates
> messy code which AFAICS also returns 2.
C doesn't like pointers to different objects to interact, because
sometimes those objects can be in different, incompatible memory blocks.
But on for variables in the same stack frame, on most machines you're
likely to be using, that isn't a problem. In fact I sometimes use the
technique, on x64, of setting a pointer to a local variable and stepping
it through the stack to see what's there. That clearly won't work with
gcc which apparently does what IT thinks is best.
--
bartc
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar
Re: Equal yet different pointers David Brown <david.brown@hesbynett.no> - 2017-11-16 11:29 +0100
Re: Equal yet different pointers Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2017-11-16 03:01 -0800
Re: Equal yet different pointers "Bill Davy" <Bill@XchelSys.co.uk> - 2017-11-16 11:03 +0000
Re: Equal yet different pointers David Brown <david.brown@hesbynett.no> - 2017-11-16 13:55 +0100
Re: Equal yet different pointers Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2017-11-16 06:44 -0800
Re: Equal yet different pointers Spiros Bousbouras <spibou@gmail.com> - 2017-11-16 14:49 +0000
Re: Equal yet different pointers Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2017-11-16 08:28 -0800
Re: Equal yet different pointers David Brown <david.brown@hesbynett.no> - 2017-11-16 16:46 +0100
Re: Equal yet different pointers "James R. Kuyper" <jameskuyper@verizon.net> - 2017-11-16 12:29 -0500
Re: Equal yet different pointers Nick Bowler <nbowler@draconx.ca> - 2017-11-17 22:47 +0000
Re: Equal yet different pointers Richard Damon <Richard@Damon-Family.org> - 2017-11-18 17:03 -0500
Re: Equal yet different pointers bartc <bc@freeuk.com> - 2017-11-16 11:09 +0000
Re: Equal yet different pointers bartc <bc@freeuk.com> - 2017-11-16 11:14 +0000
Re: Equal yet different pointers "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2017-11-16 03:41 -0800
Rick's neuroses (Was: Equal yet different pointers) gazelle@shell.xmission.com (Kenny McCormack) - 2017-11-16 12:11 +0000
Re: Rick's neuroses (Was: Equal yet different pointers) "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2017-11-16 04:30 -0800
Re: Rick's neuroses (Was: Equal yet different pointers) David Brown <david.brown@hesbynett.no> - 2017-11-16 14:52 +0100
Obscure references (Was: Rick's neuroses (Was: Equal yet different pointers)) gazelle@shell.xmission.com (Kenny McCormack) - 2017-11-19 12:32 +0000
Re: Obscure references Noob <root@127.0.0.1> - 2017-11-20 11:08 +0100
Re: Obscure references David Brown <david.brown@hesbynett.no> - 2017-11-20 12:36 +0100
Re: Equal yet different pointers David Brown <david.brown@hesbynett.no> - 2017-11-16 14:45 +0100
Re: Equal yet different pointers Keith Thompson <kst-u@mib.org> - 2017-11-16 08:55 -0800
Re: Equal yet different pointers Spiros Bousbouras <spibou@gmail.com> - 2017-11-16 17:09 +0000
Re: Equal yet different pointers Keith Thompson <kst-u@mib.org> - 2017-11-16 09:33 -0800
Re: Equal yet different pointers Gareth Owen <gwowen@gmail.com> - 2017-11-16 19:20 +0000
Re: Equal yet different pointers Keith Thompson <kst-u@mib.org> - 2017-11-16 11:36 -0800
Re: Equal yet different pointers David Brown <david.brown@hesbynett.no> - 2017-11-16 22:50 +0100
Re: Equal yet different pointers supercat@casperkitty.com - 2017-11-16 14:34 -0800
Re: Equal yet different pointers David Brown <david.brown@hesbynett.no> - 2017-11-17 10:44 +0100
Re: Equal yet different pointers supercat@casperkitty.com - 2017-11-17 08:23 -0800
Re: Equal yet different pointers David Brown <david.brown@hesbynett.no> - 2017-11-17 19:13 +0100
Re: Equal yet different pointers supercat@casperkitty.com - 2017-11-17 16:45 -0800
Re: Equal yet different pointers Spiros Bousbouras <spibou@gmail.com> - 2017-11-18 07:30 +0000
Re: Equal yet different pointers David Brown <david.brown@hesbynett.no> - 2017-11-16 22:25 +0100
Re: Equal yet different pointers Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-11-16 23:11 +0000
Re: Equal yet different pointers supercat@casperkitty.com - 2017-11-16 15:47 -0800
Re: Equal yet different pointers David Brown <david.brown@hesbynett.no> - 2017-11-17 10:49 +0100
Re: Equal yet different pointers Gareth Owen <gwowen@gmail.com> - 2017-11-16 19:20 +0000
Re: Equal yet different pointers "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2017-11-16 11:26 -0800
Re: Equal yet different pointers gazelle@shell.xmission.com (Kenny McCormack) - 2017-11-16 22:22 +0000
Re: Equal yet different pointers bartc <bc@freeuk.com> - 2017-11-16 23:09 +0000
Re: Equal yet different pointers gazelle@shell.xmission.com (Kenny McCormack) - 2017-11-17 08:50 +0000
Re: Equal yet different pointers Gareth Owen <gwowen@gmail.com> - 2017-11-17 17:42 +0000
Re: Equal yet different pointers gazelle@shell.xmission.com (Kenny McCormack) - 2017-11-17 19:35 +0000
Re: Equal yet different pointers bartc <bc@freeuk.com> - 2017-11-16 23:34 +0000
Re: Equal yet different pointers supercat@casperkitty.com - 2017-11-16 15:58 -0800
Re: Equal yet different pointers Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-11-17 00:57 +0000
Re: Equal yet different pointers David Brown <david.brown@hesbynett.no> - 2017-11-17 12:32 +0100
Re: Equal yet different pointers bartc <bc@freeuk.com> - 2017-11-17 12:31 +0000
Re: Equal yet different pointers mark.bluemel@gmail.com - 2017-11-17 07:23 -0800
Re: Equal yet different pointers David Brown <david.brown@hesbynett.no> - 2017-11-17 17:14 +0100
Re: Equal yet different pointers bartc <bc@freeuk.com> - 2017-11-18 00:12 +0000
Re: Equal yet different pointers Ian Collins <ian-news@hotmail.com> - 2017-11-18 13:39 +1300
Re: Equal yet different pointers bartc <bc@freeuk.com> - 2017-11-18 01:39 +0000
Re: Equal yet different pointers Ian Collins <ian-news@hotmail.com> - 2017-11-18 14:42 +1300
Re: Equal yet different pointers bartc <bc@freeuk.com> - 2017-11-18 02:01 +0000
Re: Equal yet different pointers Melzzzzz <Melzzzzz@zzzzz.com> - 2017-11-18 02:07 +0000
Re: Equal yet different pointers Ian Collins <ian-news@hotmail.com> - 2017-11-18 15:35 +1300
Re: Equal yet different pointers bartc <bc@freeuk.com> - 2017-11-18 11:23 +0000
Re: Equal yet different pointers Ian Collins <ian-news@hotmail.com> - 2017-11-19 12:02 +1300
Re: Equal yet different pointers bartc <bc@freeuk.com> - 2017-11-18 23:25 +0000
Re: Equal yet different pointers Ian Collins <ian-news@hotmail.com> - 2017-11-19 13:44 +1300
Re: Equal yet different pointers bartc <bc@freeuk.com> - 2017-11-19 01:05 +0000
Re: Equal yet different pointers Ian Collins <ian-news@hotmail.com> - 2017-11-19 14:30 +1300
Re: Equal yet different pointers bartc <bc@freeuk.com> - 2017-11-19 01:48 +0000
Re: Equal yet different pointers Ian Collins <ian-news@hotmail.com> - 2017-11-19 16:21 +1300
Re: Equal yet different pointers Reinhardt Behm <rbehm@hushmail.com> - 2017-11-19 11:44 +0800
Re: Equal yet different pointers bartc <bc@freeuk.com> - 2017-11-19 10:10 +0000
Re: Equal yet different pointers mark.bluemel@gmail.com - 2017-11-20 00:52 -0800
Re: Equal yet different pointers David Brown <david.brown@hesbynett.no> - 2017-11-19 23:27 +0100
Re: Equal yet different pointers Richard Damon <Richard@Damon-Family.org> - 2017-11-19 22:48 -0500
Re: Equal yet different pointers Ian Collins <ian-news@hotmail.com> - 2017-11-20 16:52 +1300
Re: Equal yet different pointers Spiros Bousbouras <spibou@gmail.com> - 2017-11-16 14:28 +0000
Re: Equal yet different pointers Barry Schwarz <schwarzb@dqel.com> - 2017-11-16 07:10 -0800
Re: Equal yet different pointers David Brown <david.brown@hesbynett.no> - 2017-11-16 16:51 +0100
Re: Equal yet different pointers "James R. Kuyper" <jameskuyper@verizon.net> - 2017-11-16 12:57 -0500
Re: Equal yet different pointers supercat@casperkitty.com - 2017-11-16 08:03 -0800
Re: Equal yet different pointers "James R. Kuyper" <jameskuyper@verizon.net> - 2017-11-16 12:38 -0500
Re: Equal yet different pointers "Pascal J. Bourguignon" <pjb@informatimago.com> - 2017-11-16 20:13 +0100
Re: Equal yet different pointers "James R. Kuyper" <jameskuyper@verizon.net> - 2017-11-16 14:29 -0500
Re: Equal yet different pointers Jorgen Grahn <grahn+nntp@snipabacken.se> - 2017-11-16 15:57 +0000
Re: Equal yet different pointers Spiros Bousbouras <spibou@gmail.com> - 2017-11-16 16:07 +0000
Re: Equal yet different pointers Jorgen Grahn <grahn+nntp@snipabacken.se> - 2017-11-16 19:01 +0000
Re: Equal yet different pointers Spiros Bousbouras <spibou@gmail.com> - 2017-11-16 19:14 +0000
Re: Equal yet different pointers Jorgen Grahn <grahn+nntp@snipabacken.se> - 2017-11-16 19:47 +0000
Re: Equal yet different pointers bartc <bc@freeuk.com> - 2017-11-16 23:08 +0000
Re: Equal yet different pointers Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-11-17 00:33 +0000
csiph-web