Recently I experienced a problem using optimization with g++
. The problem was related to a PCI card, where the registers (for command and data) were repreented by a memory address. My driver mapped the physical address to a pointer within the application and gave it to the called process, which worked with it like this:
unsigned int * pciMemory;
askDriverForMapping( & pciMemory );
...
pciMemory[ 0 ] = someCommandIdx;
pciMemory[ 0 ] = someCommandLength;
for ( int i = 0; i < sizeof( someCommand ); i++ )
pciMemory[ 0 ] = someCommand[ i ];
The card didn't act as expected. When I saw the assembly I understood that the compiler only wrote someCommand[ the last ]
into pciMemory
, omitting all preceding writes.
In conclusion: be accurate and attentive with optimization.