{"id":26193,"date":"2017-10-26T20:24:48","date_gmt":"2017-10-26T14:54:48","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26193"},"modified":"2017-10-26T20:24:48","modified_gmt":"2017-10-26T14:54:48","slug":"c-programming-check-integer-overflow","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/c-programming-check-integer-overflow\/","title":{"rendered":"C Programming-Check for Integer Overflow"},"content":{"rendered":"<p>Write a \u201cC\u201d function, int addOvf(int* result, int a, int b) If there is no overflow, the function places the resultant = sum a+b in \u201cresult\u201d and returns 0. Otherwise it returns -1. The solution of casting to long and adding to find detecting the overflow is not allowed.<br \/>\n<strong>Method 1<\/strong><br \/>\nThere can be overflow only if signs of two numbers are same, and sign of sum is opposite to the signs of numbers.<\/p>\n<pre>1)  Calculate sum\r\n2)  If both numbers are positive and sum is negative then return -1\r\n     Else \r\n        If both numbers are negative and sum is positive then return -1\r\n        Else return 0<\/pre>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">C Programming<\/span> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">#include&lt;stdio.h&gt;<br\/>#include&lt;stdlib.h&gt;<br\/> <br\/>\/* Takes pointer to result and two numbers as<br\/>    arguments. If there is no overflow, the function<br\/>    places the resultant = sum a+b in \u201cresult\u201d and<br\/>    returns 0, otherwise it returns -1 *\/<br\/> int addOvf(int* result, int a, int b)<br\/> {<br\/>     *result = a + b;<br\/>     if(a &gt; 0 &amp;&amp; b &gt; 0 &amp;&amp; *result &lt; 0)<br\/>         return -1;<br\/>     if(a &lt; 0 &amp;&amp; b &lt; 0 &amp;&amp; *result &gt; 0)<br\/>         return -1;<br\/>     return 0;<br\/> }<br\/> <br\/> int main()<br\/> {<br\/>     int *res = (int *)malloc(sizeof(int));<br\/>     int x = 2147483640;<br\/>     int y = 10;<br\/> <br\/>     printf(&quot;%d&quot;, addOvf(res, x, y));<br\/> <br\/>     printf(&quot;\\n %d&quot;, *res);<br\/>     getchar();<br\/>     return 0;<br\/>}<\/code><\/pre> <\/div>\n<p><strong>Time Complexity :<\/strong> O(1)<br \/>\n<strong>Space Complexity:<\/strong> O(1)<\/p>\n[ad type=&#8221;banner&#8221;]\n<p><strong>Method 2<\/strong><br \/>\nThanks to Himanshu Aggarwal for adding this method. This method doesn\u2019t modify *result if there us an overflow.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">C Programming<\/span> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">#include&lt;stdio.h&gt;<br\/>#include&lt;limits.h&gt;<br\/>#include&lt;stdlib.h&gt;<br\/> <br\/>int addOvf(int* result, int a, int b)<br\/>{<br\/>   if( a &gt; INT_MAX - b)<br\/>     return -1;<br\/>   else<br\/>   {<br\/>     *result = a + b;<br\/>      return 0;<br\/>   }<br\/>}<br\/> <br\/>int main()<br\/>{<br\/>  int *res = (int *)malloc(sizeof(int));<br\/>  int x = 2147483640;<br\/>  int y = 10;<br\/> <br\/>  printf(&quot;%d&quot;, addOvf(res, x, y));<br\/>  printf(&quot;\\n %d&quot;, *res);<br\/>  getchar();<br\/>  return 0;<br\/>}<\/code><\/pre> <\/div>\n<p><strong>Time Complexity :<\/strong> O(1)<br \/>\n<strong>Space Complexity:<\/strong> O(1)<\/p>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>C Programming Check for Integer Overflow &#8211; Bit Algorithm &#8211; The solution of casting to long and adding to find detecting the overflow is not allowed.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69969,74852,69866,1],"tags":[77713,77728,77712,77721,77705,77707,77745,77706,77727,77730,77746,77735,77738,77736,77722,77744,77747,77709,77719,77741,77733,77739,77724,77726,77704,77711,77732,77731,77703,77734,74445,77725,77720,77737,77718,77742,77723,77715,77716,77700,77714,77710,77717,77708,77743,77729,77701,77702,77740,75264],"class_list":["post-26193","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-bit-algorithms","category-c-programming","category-coding","tag-16-bit-range","tag-16-bit-signed-integer-converter","tag-64-bit-long","tag-buffer-c","tag-buffer-overflow","tag-buffer-overflow-attack-example","tag-buffer-overflow-example","tag-buffer-overflow-vulnerability","tag-buffer-overrun","tag-c-buffer","tag-c-char","tag-c-char-type","tag-c-int","tag-c-language-float","tag-c-long-long","tag-c-programming-data-types","tag-c-short","tag-c-short-type","tag-c-uint","tag-datatypes-of-c","tag-double-c-language","tag-double-c-programming","tag-double-data-type-c","tag-float-long-double","tag-function-of-buffer","tag-how-to-check-for-overflow","tag-int-32-max","tag-int-32-range","tag-int-c","tag-int-c-programming","tag-integers-examples","tag-integers-test","tag-long-32-bit","tag-long-c-programming","tag-long-data-type-c","tag-long-double-float","tag-long-integer-range","tag-long-long-c","tag-long-long-double","tag-long-long-in-c","tag-long-long-limit","tag-max-int32","tag-overflow-detection","tag-short-type-c","tag-size-of-float","tag-size-of-long","tag-stack-in-c-programming","tag-stack-program-in-c","tag-type-char","tag-unsigned-int-in-c"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26193","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=26193"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26193\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26193"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26193"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26193"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}