Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #44742

Re: This will work or not?

From David Brown <david.brown@hesbynett.no>
Newsgroups comp.lang.c++
Subject Re: This will work or not?
Date 2016-09-02 17:46 +0200
Organization A noiseless patient Spider
Message-ID <nqc6sr$bum$1@dont-email.me> (permalink)
References (9 earlier) <nqaj10$mv2$1@jstuckle.eternal-september.org> <a86af030-51bd-42d4-bc60-ae4be6f6ab0f@googlegroups.com> <nqbhfb$12q1$1@gioia.aioe.org> <nqbk65$bmh$1@dont-email.me> <nqbu5g$1oe5$1@gioia.aioe.org>

Show all headers | View raw


On 02/09/16 15:17, Manfred wrote:
> On 09/02/2016 12:27 PM, David Brown wrote:
>>> It is different because of the presence of the union.
>>> When you access the struct memory area through the array you are
>>> accessing data in a well-specified order, and the struct members must
>>> therefore follow the order specified by the standard.
>>> As long as memory access is legal, and it is in this case, if
>>> optimization could reorder the struct members, the "as-if" rule would be
>>> violated because the optimized and non-optimized code results would be
>>> different.
>>
>> The as-if rule still applies.  If we stick to C, where you /are/ allowed
>> to access data in different ways with a union, we could have this:
>>
>> union {
>>     struct {
>>         uint32_t a;
>>         uint32_t b;
>>         uint64_t c;
>>         uint32_t d;
>>     };
>>     uint32_t as_array[5];
>> } u;
>>
>> When you write:
>>
>> uint32_t foo(void) {
>>     u.d = 123;
>>     u.b = u.as_array[4];
>>     return u.as_array[1];
>> }
>>
>> The compiler will have to return 123.  It must /behave/ in this way.
>> But it does /not/ have to keep the struct in the order you have given -
>> it is allowed to transform that program into this:
>>
>> union {
>>     struct {
>>         uint64_t c;        // <- Note the change
>>         uint32_t a;
>>         uint32_t b;
>>         uint32_t d;
>>     };
>>     uint32_t as_array[5];
>> } u;
>>
>> uint32_t foo(void) {
>>     u.d = 123;
>>     u.b = u.as_array[4];
>>     return u.as_array[3];        // <- Note the change
>> }
>>
> 
> You mean that the compiler would be allowed to rearrange the entire
> address space (variables /and/ addresses: the programmer asks for
> u.as_array[1] and gets u.as_array[3]) so that they appear to be in order
> while physically they are not. I strongly doubt this, simply because it
> would be almost impossible to guarantee the expected results for any
> generic input code (and it would often provide no performance change or
> even a degradation). Note that in your example the compiler designer
> would have to build in the knowledge that the return value of foo is u.b
> instead of u.as_array[1], which is true for your specific example only.
> 
> 
> 
> On the other hand, even if such obscure behaviour occurred, it would be
> completely transparent to the programmer, so that it could be ignored
> anyway.
> 
> 
> 
> To put it more simple:
> 
> 
> 
> #include <cstdio>
> #include <cstdint>
> 
> union {
>   struct __attribute__ ((__packed__)) {
>     uint32_t a;
>     uint32_t b;
>     uint64_t c;
>     uint32_t d;
>   };
>   uint32_t as_array[5];
> } u;
> 
> void foo(void) {
>   u.a = 1;
>   u.b = 2;
>   u.c = 3;
>   u.d = 4;
> }
> 
> void bar(void) {
>   for(uint32_t* p = u.as_array; p < u.as_array+5; ++p) {
>     printf("%u\n", *p);
>   }
> }
> 
> int main(void) {
>   foo();
>   bar();
> }
> 
> The output must be
> 1
> 2
> 3
> 0
> 4
> (assuming little endian), and since we are scanning the memory area
> sequentially, variables must be ordered as coded. Again, even if obscure
> things would be allowed to happen, they would be transparent to the
> programmer, so that they could be ignored.
> 
> 

The point is that the output you have shown here is observable
behaviour.  In this case, it is the /only/ observable behaviour.  And
the compiler is free to make whatever transformations it wants, as long
as the observable behaviour matches.  The compiler is therefore free to
re-arrange the structs as it wants - just as it is free to turn main() into

int main(void) {
	puts("1\n2\n3\n0\n4\n");
}

Of course, that would only be legal if the compiler knew that the code
would not be statically linked with other code that called foo, bar, or
main, or used u.  When gcc had an option for re-ordering structs, it
could only be enabled along with the "-fwhole-program" flag for
precisely that reason.  But if the relevant structs and functions had
been locals or file static (with no "escapes"), then the compiler could
freely transform it.

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

This will work or not? Melzzzzz <mel@zzzzz.com> - 2016-08-31 14:59 +0200
  Re: This will work or not? David Brown <david.brown@hesbynett.no> - 2016-08-31 15:20 +0200
    Re: This will work or not? mark <mark@invalid.invalid> - 2016-08-31 16:05 +0200
      Re: This will work or not? David Brown <david.brown@hesbynett.no> - 2016-08-31 16:19 +0200
    Re: This will work or not? scott@slp53.sl.home (Scott Lurndal) - 2016-08-31 14:20 +0000
    Re: This will work or not? Bo Persson <bop@gmb.dk> - 2016-08-31 18:53 +0200
      Re: This will work or not? Tim Rentsch <txr@alumni.caltech.edu> - 2016-09-01 09:03 -0700
        Re: This will work or not? David Brown <david.brown@hesbynett.no> - 2016-09-01 22:45 +0200
          Re: This will work or not? Tim Rentsch <txr@alumni.caltech.edu> - 2016-09-01 18:08 -0700
            Re: This will work or not? David Brown <david.brown@hesbynett.no> - 2016-09-02 09:24 +0200
              Re: This will work or not? Tim Rentsch <txr@alumni.caltech.edu> - 2016-09-04 06:31 -0700
                Re: This will work or not? David Brown <david.brown@hesbynett.no> - 2016-09-04 20:19 +0200
  Re: This will work or not? Manfred <noname@invalid.add> - 2016-08-31 15:45 +0200
    Re: This will work or not? Melzzzzz <mel@zzzzz.com> - 2016-08-31 15:59 +0200
    Re: This will work or not? David Brown <david.brown@hesbynett.no> - 2016-08-31 16:22 +0200
      Re: This will work or not? Manfred <noname@invalid.add> - 2016-08-31 17:06 +0200
        Re: This will work or not? David Brown <david.brown@hesbynett.no> - 2016-08-31 19:29 +0200
          Re: This will work or not? Chris Vine <chris@cvine--nospam--.freeserve.co.uk> - 2016-08-31 21:52 +0100
            Re: This will work or not? David Brown <david.brown@hesbynett.no> - 2016-09-01 09:07 +0200
              Re: This will work or not? Chris Vine <chris@cvine--nospam--.freeserve.co.uk> - 2016-09-01 12:30 +0100
            Re: This will work or not? Tim Rentsch <txr@alumni.caltech.edu> - 2016-09-01 09:01 -0700
              Re: This will work or not? Chris Vine <chris@cvine--nospam--.freeserve.co.uk> - 2016-09-01 18:16 +0100
                Re: This will work or not? Tim Rentsch <txr@alumni.caltech.edu> - 2016-09-01 18:37 -0700
                Re: This will work or not? Chris Vine <chris@cvine--nospam--.freeserve.co.uk> - 2016-09-02 10:42 +0100
                Re: This will work or not? Tim Rentsch <txr@alumni.caltech.edu> - 2016-09-04 06:59 -0700
          Re: This will work or not? Jerry Stuckle <jstucklex@attglobal.net> - 2016-08-31 23:17 -0400
            Re: This will work or not? David Brown <david.brown@hesbynett.no> - 2016-09-01 09:35 +0200
              Re: This will work or not? Jerry Stuckle <jstucklex@attglobal.net> - 2016-09-01 15:34 -0400
                Re: This will work or not? me <crisdunbar@gmail.com> - 2016-09-01 13:50 -0700
                Re: This will work or not? Jerry Stuckle <jstucklex@attglobal.net> - 2016-09-01 21:01 -0400
                Re: This will work or not? me <crisdunbar@gmail.com> - 2016-09-01 19:02 -0700
                Re: This will work or not? Jerry Stuckle <jstucklex@attglobal.net> - 2016-09-01 22:17 -0400
                Re: This will work or not? me <crisdunbar@gmail.com> - 2016-09-02 08:19 -0700
                Re: This will work or not? Jerry Stuckle <jstucklex@attglobal.net> - 2016-09-02 12:39 -0400
                Re: This will work or not? me <crisdunbar@gmail.com> - 2016-09-02 12:26 -0700
                Re: This will work or not? Jerry Stuckle <jstucklex@attglobal.net> - 2016-09-02 16:38 -0400
                Re: This will work or not? me <crisdunbar@gmail.com> - 2016-09-02 13:51 -0700
                Re: This will work or not? me <crisdunbar@gmail.com> - 2016-09-02 14:16 -0700
                Re: This will work or not? Jerry Stuckle <jstucklex@attglobal.net> - 2016-09-02 19:58 -0400
                Re: This will work or not? Paavo Helde <myfirstname@osa.pri.ee> - 2016-09-02 23:56 +0300
                Re: This will work or not? David Brown <david.brown@hesbynett.no> - 2016-09-03 17:58 +0200
                Re: This will work or not? Manfred <noname@invalid.add> - 2016-09-02 11:41 +0200
                Re: This will work or not? David Brown <david.brown@hesbynett.no> - 2016-09-02 12:27 +0200
                Re: This will work or not? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-09-02 12:37 +0100
                Re: This will work or not? David Brown <david.brown@hesbynett.no> - 2016-09-02 17:39 +0200
                Re: This will work or not? Melzzzzz <mel@zzzzz.com> - 2016-09-02 17:40 +0200
                Re: This will work or not? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-09-02 16:54 +0100
                Re: This will work or not? David Brown <david.brown@hesbynett.no> - 2016-09-02 20:22 +0200
                Re: This will work or not? Richard Damon <Richard@Damon-Family.org> - 2016-09-02 08:04 -0400
                Re: This will work or not? BartC <bc@freeuk.com> - 2016-09-02 13:51 +0100
                Re: This will work or not? Richard Damon <Richard@Damon-Family.org> - 2016-09-02 21:48 -0400
                Re: This will work or not? Manfred <noname@invalid.add> - 2016-09-02 15:17 +0200
                Re: This will work or not? David Brown <david.brown@hesbynett.no> - 2016-09-02 17:46 +0200
                Re: This will work or not? David Brown <david.brown@hesbynett.no> - 2016-09-01 23:14 +0200
                Re: This will work or not? Jerry Stuckle <jstucklex@attglobal.net> - 2016-09-01 21:02 -0400
                Re: This will work or not? David Brown <david.brown@hesbynett.no> - 2016-09-02 09:35 +0200
                Re: This will work or not? Jerry Stuckle <jstucklex@attglobal.net> - 2016-09-02 08:50 -0400
                Re: This will work or not? David Brown <david.brown@hesbynett.no> - 2016-09-02 17:59 +0200
                Re: This will work or not? Jerry Stuckle <jstucklex@attglobal.net> - 2016-09-02 12:49 -0400
                Re: This will work or not? David Brown <david.brown@hesbynett.no> - 2016-09-02 20:36 +0200
                Re: This will work or not? Jerry Stuckle <jstucklex@attglobal.net> - 2016-09-02 16:32 -0400
                Re: This will work or not? David Brown <david.brown@hesbynett.no> - 2016-09-03 18:08 +0200
                Re: This will work or not? Jerry Stuckle <jstucklex@attglobal.net> - 2016-09-03 18:25 -0400
                Re: This will work or not? Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2016-09-03 23:51 +0100
                Re: This will work or not? Jerry Stuckle <jstucklex@attglobal.net> - 2016-09-03 21:48 -0400
                Re: This will work or not? Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2016-09-04 03:48 +0100
                Re: This will work or not? Jerry Stuckle <jstucklex@attglobal.net> - 2016-09-03 23:05 -0400
                Re: This will work or not? Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2016-09-04 05:02 +0100
                Re: This will work or not? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-09-04 10:57 +0100
                Re: This will work or not? Jerry Stuckle <jstucklex@attglobal.net> - 2016-09-04 09:40 -0400
                Re: This will work or not? Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2016-09-04 16:39 +0100
                Re: This will work or not? Jerry Stuckle <jstucklex@attglobal.net> - 2016-09-04 12:46 -0400
                Re: This will work or not? Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2016-09-04 17:51 +0100
                Re: This will work or not? Jerry Stuckle <jstucklex@attglobal.net> - 2016-09-04 13:07 -0400
                Re: This will work or not? Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2016-09-04 18:37 +0100
                Re: This will work or not? Jerry Stuckle <jstucklex@attglobal.net> - 2016-09-04 17:16 -0400
                Re: This will work or not? Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2016-09-04 22:23 +0100
                Re: This will work or not? Jerry Stuckle <jstucklex@attglobal.net> - 2016-09-04 18:54 -0400
                Re: This will work or not? "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2016-09-04 09:58 -0700
                Re: This will work or not? Jerry Stuckle <jstucklex@attglobal.net> - 2016-09-04 13:08 -0400
                Re: This will work or not? "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2016-09-04 10:21 -0700
                Re: This will work or not? Jerry Stuckle <jstucklex@attglobal.net> - 2016-09-04 17:18 -0400
                Re: This will work or not? Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2016-09-04 22:24 +0100
                Re: This will work or not? Jerry Stuckle <jstucklex@attglobal.net> - 2016-09-04 18:56 -0400
                Re: This will work or not? Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2016-09-05 00:27 +0100
                Re: This will work or not? Jerry Stuckle <jstucklex@attglobal.net> - 2016-09-04 22:53 -0400
                Re: This will work or not? Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2016-09-05 18:18 +0100
                Re: This will work or not? Jerry Stuckle <jstucklex@attglobal.net> - 2016-09-05 15:27 -0400
                Re: This will work or not? Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2016-09-05 20:32 +0100
                Re: This will work or not? woodbrian77@gmail.com - 2016-09-05 14:07 -0700
                Re: This will work or not? Jerry Stuckle <jstucklex@attglobal.net> - 2016-09-05 18:15 -0400
                Re: This will work or not? Ike Naar <ike@iceland.freeshell.org> - 2016-09-05 20:12 +0000
                Re: This will work or not? Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2016-09-05 22:05 +0100
                Re: This will work or not? Jerry Stuckle <jstucklex@attglobal.net> - 2016-09-05 18:16 -0400
                Re: This will work or not? David Brown <david.brown@hesbynett.no> - 2016-09-06 08:47 +0200
                Re: This will work or not? Jerry Stuckle <jstucklex@attglobal.net> - 2016-09-06 09:12 -0400
                Re: This will work or not? David Brown <david.brown@hesbynett.no> - 2016-09-06 16:31 +0200
                Re: This will work or not? Jerry Stuckle <jstucklex@attglobal.net> - 2016-09-06 11:20 -0400
                Re: This will work or not? bighe.trole@gmail.com - 2016-09-04 18:55 -0700
                Re: This will work or not? Jerry Stuckle <jstucklex@attglobal.net> - 2016-09-04 22:55 -0400
                Re: This will work or not? "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2016-09-04 17:17 -0700
                Re: This will work or not? Jerry Stuckle <jstucklex@attglobal.net> - 2016-09-04 22:57 -0400
                Re: This will work or not? "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2016-09-05 11:20 -0700
                Re: This will work or not? Luuk <luuk@invalid.lan> - 2016-09-04 18:54 +0200
                Re: This will work or not? Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2016-09-04 17:59 +0100
                Re: This will work or not? Jerry Stuckle <jstucklex@attglobal.net> - 2016-09-04 13:08 -0400
                Re: This will work or not? David Brown <david.brown@hesbynett.no> - 2016-09-04 20:25 +0200
                Re: This will work or not? Jerry Stuckle <jstucklex@attglobal.net> - 2016-09-04 17:20 -0400
                Re: This will work or not? Öö Tiib <ootiib@hot.ee> - 2016-09-04 02:36 -0700
                Re: This will work or not? Jerry Stuckle <jstucklex@attglobal.net> - 2016-09-04 09:41 -0400
                Re: This will work or not? Jerry Stuckle <jstucklex@attglobal.net> - 2016-09-03 21:49 -0400
                Re: This will work or not? Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2016-09-04 03:49 +0100
                Re: This will work or not? Jerry Stuckle <jstucklex@attglobal.net> - 2016-09-03 23:06 -0400
                Re: This will work or not? woodbrian77@gmail.com - 2016-09-03 21:16 -0700
  Re: This will work or not? Vir Campestris <vir.campestris@invalid.invalid> - 2016-08-31 21:08 +0100
  Re: This will work or not? "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2016-09-01 02:28 +0200
    Re: This will work or not? Öö Tiib <ootiib@hot.ee> - 2016-09-01 01:28 -0700
    Re: This will work or not? "Chris M. Thomasson" <invalid@invalid.invalid> - 2016-09-02 12:06 -0700
  Re: This will work or not? Öö Tiib <ootiib@hot.ee> - 2016-09-05 02:21 -0700
    Re: This will work or not? Melzzzzz <mel@zzzzz.com> - 2016-09-05 13:38 +0200
  Re: This will work or not? Juha Nieminen <nospam@thanks.invalid> - 2016-09-05 11:34 +0000
    Re: This will work or not? Melzzzzz <mel@zzzzz.com> - 2016-09-05 13:36 +0200
      Re: This will work or not? Vir Campestris <vir.campestris@invalid.invalid> - 2016-09-05 21:25 +0100

csiph-web