Optimization/clarity coding : what is gcc able to do for me ?

Falk Hueffner hueffner@minet.uni-jena.de
Sun Jan 16 20:43:00 GMT 2005


Nick NoSpam <nicknospam@optonline.net> writes:

> On Fri, 2005-01-14 at 11:25 +0100, Pierre Chatelier wrote:
>
>> vector<int> v(1000);
>> for(int i=0 ; i<v.size() ; ++i)
>>    v[i] = i;
>
> 2.  Can the compiler optimize the size() call away (after calling it
> once)?

No, since it might have side effects. However, in this case the
compiler knows the definition of size() and can see it has no side
effects. Therefore, it can optimize out the calculation, except in the
case of vector<char>, where the optimizer has to assume the change of
v[i] might clobber members of the vector<char> struct.

-- 
        Falk



More information about the Gcc mailing list