Files
safe_numerics/doc/html/tutorial/10.html
2015-12-21 23:17:05 -08:00

100 lines
4.2 KiB
HTML

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Mixing Data Types Can Create Subtle Errors</title>
<link rel="stylesheet" href="../boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.76.1">
<link rel="home" href="../index.html" title="Safe Numerics">
<link rel="up" href="../tutorial.html" title="Tutorial and Motivating Examples">
<link rel="prev" href="4.html" title="Implicit Conversions Change Data Values">
<link rel="next" href="5.html" title="Array Index Value Can Exceed Array Limits">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img href="index.html" height="164px" src="pre-boost.jpg" alt="Library Documentation Index"></td>
<td><h2>Safe Numerics</h2></td>
</tr></table>
<div class="spirit-nav">
<a accesskey="p" href="4.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../tutorial.html"><img src="../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="5.html"><img src="../images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="safe_numerics.tutorial.10"></a>Mixing Data Types Can Create Subtle Errors</h3></div></div></div>
<p>C++ contains signed and unsigned integer types. In spite of their
names, they function differently which often produces surprising results
for some operands. Program errors from this behavior can be exceedingly
difficult to find. This has lead to recommendations of various ad hoc
"rules" to avoid these problems. It's not always easy to apply these
"rules" to existing code without creating even more bugs. Here is a
typical example of this problem:</p>
<pre class="programlisting">#include &lt;iostream&gt;
#include &lt;cstdint&gt;
#include "../include/safe_integer.hpp"
#include "../include/cpp.hpp"
using namespace std;
using namespace boost::numeric;
void f(const unsigned int &amp; x, const int8_t &amp; y){
cout &lt;&lt; x * y &lt;&lt; endl;
}
void safe_f(
const safe&lt;unsigned int&gt; &amp; x,
const safe&lt;int8_t&gt; &amp; y
){
cout &lt;&lt; x * y &lt;&lt; endl;
}
int main(){
cout &lt;&lt; "example 10: ";
cout &lt;&lt; "mixing types produces surprising results" &lt;&lt; endl;
try {
std::cout &lt;&lt; "Not using safe numerics" &lt;&lt; std::endl;
// problem: arithmetic operations can yield incorrect results.
f(100, 100); // works as expected
f(100, -100); // wrong result - unnoticed
}
catch(std::exception){
// never arrive here
std::cout &lt;&lt; "error detected!" &lt;&lt; std::endl;
}
try {
// solution: use safe types
std::cout &lt;&lt; "Using safe numerics" &lt;&lt; std::endl;
safe_f(100, 100); // works as expected
safe_f(100, -100); // throw error
}
catch(const exception &amp; e){
cout &lt;&lt; "detected error:" &lt;&lt; e.what() &lt;&lt; endl;;
}
return 0;
}
</pre>
<p>Here
is the output of the above program:</p>
<pre class="programlisting">example 10: mixing types produces surprising results
Not using safe numerics
10000
4294957296
Using safe numerics
10000
detected error:converted negative value to unsigned
</pre>
<p>This solution is simple, Just replace instances of the
<code class="computeroutput">int</code>with <code class="computeroutput">safe&lt;int&gt;</code>.</p>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright &#169; 2012 Robert Ramey<p><a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">Subject to Boost
Software License</a></p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="4.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../tutorial.html"><img src="../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="5.html"><img src="../images/next.png" alt="Next"></a>
</div>
</body>
</html>