Re: Optimization o2 vs o3 issue

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Manish Baphna wrote:
> Hi
> 1.Its NOT multi threaded application.
> 
> 2. I created a small example where this issue is visible. Let me know what you think after it.
> 
> ///////////// bug.cpp ///////////
> #include <iostream>
> #include <string>
> #include <stdio.h>
> 
> using namespace std ;
> 
> typedef unsigned char uint8_t ;
> 
> class DBus {
> public:
>    uint8_t data[16] ;
> };
> 
> int main(){
>          DBus dout ;  
>          long long unsigned upper_data = 0xFFFFFFFff ; 
> 	 long long unsigned int mask = 0xFF ;
> 
> 	 for(int count=0 ; count < 8 ; count++)
>          {
> 	    dout.data[15 - count] = (uint8_t)((upper_data & (mask << ((8+count)*8)))>>((8+count)*8));
>           //cout << "Dout data " << (uint64_t)((dout.data)[15-count]) << endl; // Line to be played with
>          }
> 
>          for(int count=0 ; count < 8 ; count++)
>          {
>             cout << "Dout data " << (uint64_t)((dout.data)[15-count]) << endl;
>          }
> 
> 
> 
> 
>    return 1 ;		
> };
> 
> ///////////// end of bug.cpp /////////
> Step1 :   Compile using o2/o3 
> bash>> g++-4.1.1 -g3 -O2 -fPIC -Wall -W -Wcast-qual -Wpointer-arith 
> bash>> g++-Woverloaded-virtual -Werror -m64 -Werror bug.cpp
> bash>> ./a.out
> Dout data 255
> Dout data 255
> Dout data 255
> Dout data 255
> Dout data 15
> Dout data 0
> Dout data 0
> Dout data 0
> bash>> g++-4.1.1 -g3 -O3 -fPIC -Wall -W -Wcast-qual -Wpointer-arith 
> bash>> g++-Woverloaded-virtual -Werror -m64 -Werror bug.cpp
> bash>> ./a.out
> Dout data 0
> Dout data 0
> Dout data 0
> Dout data 0
> Dout data 0
> Dout data 0
> Dout data 0
> Dout data 0
> 
> Now if you uncomment the cout line , o3 would also work !!
> 
> 
> Let me know what you guys think of it.

It's undefined behaviour.

(mask << ((8+count)*8))

See C99, Section 6.5.7, Para 3.

Andrew.

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux