sharptooth
sharptooth

Reputation: 170539

What does "potential divide by zero" mean in Visual C++ C4723 warning description?

When I compile the following code in Visual C++ 10

int _tmain(int /*argc*/, _TCHAR* /*argv*/[])
{
    int len = strlen( "" );
    if( len / 0 ) {
        rand();
    }
}

the compiler emits C4723 warning potential divide by zero.

What does potential mean here? My C++ code clearly says "divide len by zero", how is the divide potential?

http://msdn.microsoft.com/en-us/library/8kd039eh.aspx

Upvotes: 0

Views: 3392

Answers (4)

Matthieu M.
Matthieu M.

Reputation: 300189

Say you are a compiler developer and you create a static analysis tool to be run during the compilation to help catch errors.

For example, here, a range-based engine that will determine the possible values that the right-hand operator of / could take.

Now there are two cases:

  • the right-hand side is definitely 0
  • the right-hand side is possibly 0

Obviously, separating the two cases requires more effort.

And thus you might rightly assume that the most common cause of errors will be a possibility and not a certainty (nodoby in its right mind would divide by 0, right ?) and save yourself some work.

  • Is it optimal ? For the developer perhaps, for the user not really.

  • Is it usable ? Definitely.

Upvotes: 2

CapelliC
CapelliC

Reputation: 60034

The compiler doesn't assume the execution path will reach the division by 0. It's a reasonable assumption, because execution of _tmain is decided after the compilation.

Upvotes: 3

Roman Ryltsov
Roman Ryltsov

Reputation: 69724

MSDN article is clear that compiler evaluated operand to be zero already at compile time. So potential here means that compiler is unsure about one thing only - whether this code is going to be ever executed or not.

Upvotes: 15

Roee Gavirel
Roee Gavirel

Reputation: 19453

It means the same, it's just been polite.
Would you like it to say "You are dividing by zero, idiot !" ? (-`

Upvotes: 4

Related Questions