{"id":677,"date":"2010-08-16T10:46:37","date_gmt":"2010-08-16T14:46:37","guid":{"rendered":"http:\/\/www.briancbecker.com\/blog\/?p=677"},"modified":"2020-04-12T19:45:34","modified_gmt":"2020-04-12T23:45:34","slug":"unsigned-int-has-to-go","status":"publish","type":"post","link":"http:\/\/www.briancbecker.com\/blog\/2010\/unsigned-int-has-to-go\/","title":{"rendered":"Unsigned int has to go"},"content":{"rendered":"<p>I&#8217;m sure unsigned int has had its place in the eons gone by; however, I think it&#8217;s outlived it&#8217;s useful lifespan. The number of times I&#8217;ve tried to do math with it and shot myself in the foot is quite astonishing. The root of the problem is you simply can&#8217;t do subtraction and expect sane results because it will underflow. Let&#8217;s take a look at an innoculous piece of code.<\/p>\n<blockquote><p>\/\/ Process all but the last element in the queue<\/p>\n<p>for (int i =\u00a0 0; i &lt; queue.size()-1; i++)<\/p><\/blockquote>\n<p>All we want to do here is process all the items in a queue except the last one. And since we&#8217;ve declared our counter as int, everything should be fine, right? If there is nothing in the array, queue.size()-1 will be 0-1 which evaluates to -1 and will not satisfiy the loop condition.<\/p>\n<p>However, this crashes. Why? Because I discovered queue.size() returns an unsigned int. And of course with unsigned int, only positive numbers are represented so when you do 0-1, the result will not be -1 but 4294967295. Brilliant, now my loop will execute 4 billion times when the queue is empty. Ahhh! Why would people code like this? If you need more than 2 billion items (the max size of a normal int), and use unsigned int instead, chances are the max size of 4 billion won&#8217;t be sufficient anyhow.<\/p>\n<p>In conclusion, if I had to re-design C or C++, I would totally leave out unsigned int. Or at least re-define the underflow\/overflow behavior so that 0-1 in unsigned int land will trigger an underflow and automatically reset the result to 0. Disabling the underflow\/overflow behavior by default would also prevent your treasury from becoming -32768 gold if you gain too much money in Civilization II.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m sure unsigned int has had its place in the eons gone by; however, I think it&#8217;s outlived it&#8217;s useful&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10,9],"tags":[],"class_list":["post-677","post","type-post","status-publish","format-standard","hentry","category-cc","category-code"],"_links":{"self":[{"href":"http:\/\/www.briancbecker.com\/blog\/wp-json\/wp\/v2\/posts\/677","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.briancbecker.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.briancbecker.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.briancbecker.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.briancbecker.com\/blog\/wp-json\/wp\/v2\/comments?post=677"}],"version-history":[{"count":4,"href":"http:\/\/www.briancbecker.com\/blog\/wp-json\/wp\/v2\/posts\/677\/revisions"}],"predecessor-version":[{"id":1314,"href":"http:\/\/www.briancbecker.com\/blog\/wp-json\/wp\/v2\/posts\/677\/revisions\/1314"}],"wp:attachment":[{"href":"http:\/\/www.briancbecker.com\/blog\/wp-json\/wp\/v2\/media?parent=677"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.briancbecker.com\/blog\/wp-json\/wp\/v2\/categories?post=677"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.briancbecker.com\/blog\/wp-json\/wp\/v2\/tags?post=677"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}