{"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[pastacode lang=\u201dc\u201d manual=\u201d%23include%3Cstdio.h%3E%0A%23include%3Cstdlib.h%3E%0A%20%0A%2F*%20Takes%20pointer%20to%20result%20and%20two%20numbers%20as%0A%20%20%20%20arguments.%20If%20there%20is%20no%20overflow%2C%20the%20function%0A%20%20%20%20places%20the%20resultant%20%3D%20sum%20a%2Bb%20in%20%E2%80%9Cresult%E2%80%9D%20and%0A%20%20%20%20returns%200%2C%20otherwise%20it%20returns%20-1%20*%2F%0A%20int%20addOvf(int*%20result%2C%20int%20a%2C%20int%20b)%0A%20%7B%0A%20%20%20%20%20*result%20%3D%20a%20%2B%20b%3B%0A%20%20%20%20%20if(a%20%3E%200%20%26%26%20b%20%3E%200%20%26%26%20*result%20%3C%200)%0A%20%20%20%20%20%20%20%20%20return%20-1%3B%0A%20%20%20%20%20if(a%20%3C%200%20%26%26%20b%20%3C%200%20%26%26%20*result%20%3E%200)%0A%20%20%20%20%20%20%20%20%20return%20-1%3B%0A%20%20%20%20%20return%200%3B%0A%20%7D%0A%20%0A%20int%20main()%0A%20%7B%0A%20%20%20%20%20int%20*res%20%3D%20(int%20*)malloc(sizeof(int))%3B%0A%20%20%20%20%20int%20x%20%3D%202147483640%3B%0A%20%20%20%20%20int%20y%20%3D%2010%3B%0A%20%0A%20%20%20%20%20printf(%22%25d%22%2C%20addOvf(res%2C%20x%2C%20y))%3B%0A%20%0A%20%20%20%20%20printf(%22%5Cn%20%25d%22%2C%20*res)%3B%0A%20%20%20%20%20getchar()%3B%0A%20%20%20%20%20return%200%3B%0A%7D\u201d message=\u201dC Programming\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><strong>Time Complexity :<\/strong> O(1)<br \/>\n<strong>Space Complexity:<\/strong> O(1)<\/p>\n[ad type=\u201dbanner\u201d]\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[pastacode lang=\u201dc\u201d manual=\u201d%23include%3Cstdio.h%3E%0A%23include%3Climits.h%3E%0A%23include%3Cstdlib.h%3E%0A%20%0Aint%20addOvf(int*%20result%2C%20int%20a%2C%20int%20b)%0A%7B%0A%20%20%20if(%20a%20%3E%20INT_MAX%20-%20b)%0A%20%20%20%20%20return%20-1%3B%0A%20%20%20else%0A%20%20%20%7B%0A%20%20%20%20%20*result%20%3D%20a%20%2B%20b%3B%0A%20%20%20%20%20%20return%200%3B%0A%20%20%20%7D%0A%7D%0A%20%0Aint%20main()%0A%7B%0A%20%20int%20*res%20%3D%20(int%20*)malloc(sizeof(int))%3B%0A%20%20int%20x%20%3D%202147483640%3B%0A%20%20int%20y%20%3D%2010%3B%0A%20%0A%20%20printf(%22%25d%22%2C%20addOvf(res%2C%20x%2C%20y))%3B%0A%20%20printf(%22%5Cn%20%25d%22%2C%20*res)%3B%0A%20%20getchar()%3B%0A%20%20return%200%3B%0A%7D\u201d message=\u201dC Programming\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><strong>Time Complexity :<\/strong> O(1)<br \/>\n<strong>Space Complexity:<\/strong> O(1)<\/p>\n[ad type=\u201dbanner\u201d]\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}]}}