{"id":7053,"date":"2012-07-25T07:00:00","date_gmt":"2012-07-25T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2012\/07\/25\/one-way-to-make-sure-you-pass-an-array-of-the-correct-size\/"},"modified":"2012-07-25T07:00:00","modified_gmt":"2012-07-25T07:00:00","slug":"one-way-to-make-sure-you-pass-an-array-of-the-correct-size","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20120725-00\/?p=7053","title":{"rendered":"One way to make sure you pass an array of the correct size"},"content":{"rendered":"<p>\nAnother entry in the very sporadic series of\n&#8220;very strange code I&#8217;ve seen.&#8221;\nThe code has been changed to protect the guilty,\nbut the essence has been preserved.\n<\/p>\n<pre>\nclass Store\n{\npublic:\n    \/\/ Retrieve \"count\" colors from item \"itemId\" into \"values\"\n    bool GetItemColors(int itemId, int count, COLORREF *values);\n    \/\/ Set \"count\" colors from \"values\" into item \"itemId\"\n    bool SetItemColors(int itemId, int count, const COLORREF *values);\n};\nbool CopyUpToFourColors(Store *store1, Store *store2, int itemId, int count)\n{\n    COLORREF size1[1];\n    COLORREF size2[2];\n    COLORREF size3[3];\n    COLORREF size4[4];\n    int *buffer = ((count == 1) ? size1 :\n                  ((count == 2) ? size2 :\n                  ((count == 3) ? size3 :\n                  ((count == 4) ? size4 :\n                                  nullptr))));\n    if (buffer == nullptr)\n        return false;\n    if (!store1-&gt;GetItemColors(itemId, count, buffer))\n        return false;\n    if (!store2-&gt;SetItemColors(itemId, count, buffer))\n        return false;\n    return true;\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Another entry in the very sporadic series of &#8220;very strange code I&#8217;ve seen.&#8221; The code has been changed to protect the guilty, but the essence has been preserved. class Store { public: \/\/ Retrieve &#8220;count&#8221; colors from item &#8220;itemId&#8221; into &#8220;values&#8221; bool GetItemColors(int itemId, int count, COLORREF *values); \/\/ Set &#8220;count&#8221; colors from &#8220;values&#8221; into [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-7053","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Another entry in the very sporadic series of &#8220;very strange code I&#8217;ve seen.&#8221; The code has been changed to protect the guilty, but the essence has been preserved. class Store { public: \/\/ Retrieve &#8220;count&#8221; colors from item &#8220;itemId&#8221; into &#8220;values&#8221; bool GetItemColors(int itemId, int count, COLORREF *values); \/\/ Set &#8220;count&#8221; colors from &#8220;values&#8221; into [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/7053","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=7053"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/7053\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=7053"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=7053"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=7053"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}