{"id":1941,"date":"2016-06-01T00:01:07","date_gmt":"2016-06-01T07:01:07","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1941"},"modified":"2016-06-11T07:30:16","modified_gmt":"2016-06-11T14:30:16","slug":"find-the-best-size-container","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1941","title":{"rendered":"Find the Best Size Container"},"content":{"rendered":"<p>Not everything in the real world appreciates the holy computer numbers. These are binary values that parallel the powers of 2: 1, 2, 4, 8, 16, 32, 64, and so on.<\/p>\n<p>A common computer puzzle is how to allocate storage specific to those holy numbers, especially when the sizes of items that you&#8217;re working with don&#8217;t exactly line up to a specific holy computer number.<br \/>\n<!--more--><br \/>\nFor example, suppose you have a string of 11 bytes. You might create a storage container 16 bytes wide to hold those bytes. You don&#8217;t have to do so, but when you built a 16-byte buffer, the processor can more effectively access the information.<\/p>\n<blockquote><p>Internally, the compiler prefers to align values at specific memory locations. If you use a debugger, you&#8217;ll see how variables sit at address offsets that end in 0, 4, 8, and C. This is why in my code I set buffer sizes to a holy number. It&#8217;s not a requirement, but the practice uses memory more efficiently.<\/p><\/blockquote>\n<p>To put it another way, suppose that you&#8217;re tasked with organizing a pile of 2-by-4s. Each board is a different length, but you must set them into containers that are exactly 16, 32, 48, and 64 inches tall. So you would take a 15-inch long board and put it into the 16-inch tall container. The 39-inch board would fit into the 48-inch cubby, and so on.<\/p>\n<p>For this month&#8217;s Exercise, you start with an <em>int<\/em> array of values:<\/p>\n<pre><code>    int lengths[COUNT] = { 4, 28, 52, 16, 35 };<\/code><\/pre>\n<p>Your job is to process each of the <code>COUNT<\/code> (five) values to determine how they best-fit into a container that is a multiple of 16 units in size.<\/p>\n<p>Obviously, I don&#8217;t want you to create a single 64-unit container and just stick everything into it. That&#8217;s not the solution. In fact, here&#8217;s the output from my solution:<\/p>\n<pre><code>Length of 4 fits into 16\r\nLength of 28 fits into 32\r\nLength of 52 fits into 64\r\nLength of 16 fits into 16\r\nLength of 35 fits into 48<\/code><\/pre>\n<p>Your solution must determine which multiple of 16 is closest to the value given and set a container size accordingly. Your code must also detect when a value is identical to a multiple of 16 and set the value to match.<\/p>\n<p>Please try this Exercise on your own before you check out <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1961\">my solution<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Allocate enough storage space for various sizes of data. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1941\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-1941","post","type-post","status-publish","format-standard","hentry","category-exercise"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1941","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1941"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1941\/revisions"}],"predecessor-version":[{"id":1976,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1941\/revisions\/1976"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1941"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1941"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1941"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}