help-gplusplus
[Top][All Lists]
Advanced

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

Re: g++ warning for unsafe operation


From: Maxim Yegorushkin
Subject: Re: g++ warning for unsafe operation
Date: 14 Dec 2006 00:40:37 -0800
User-agent: G2/1.0

mathieu wrote:

> Hi there,
>
>   I am looking for a warning in gcc that would warn me about
> potentially dangerous operation, such as the one describe in (*). I
> understand this is perfectly valid operation, but it looks to me like a
> valid warning.
>
>   I tried the following so far:
>
> -Wall -Wextra -Wconversion -Wsign-compare
>
> Thanks
> -Mathieu
>
> (*)
> #include <vector>
>
> int main()
> {
>   std::vector<int> v;
>   int s = 0;
>   for(unsigned int i = 0; i < v.size() - 1; ++i) // v.size() == 0
>     {
>     s += v[i] * v[i+1];
>     }
>
>   return 0;
> }

You could just change the loop to make it play well when 0 == v.size():

for(unsigned int i = 1; i < v.size(); ++i)
{
    s += v[i - 1] * v[i];
}



reply via email to

[Prev in Thread] Current Thread [Next in Thread]