<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xml:base="https://mg.to" xmlns:dc="https://purl.org/dc/elements/1.1/">
<channel>
 <title>mg.to - Use elementsof(sz), not sizeof(sz) - Comments</title>
 <link>https://mg.to/2004/08/07/use-elementsof-not-sizeof</link>
 <description>Comments for &quot;Use elementsof(sz), not sizeof(sz)&quot;</description>
 <language>en</language>
<item>
 <title>I like to use:</title>
 <link>https://mg.to/2004/08/07/use-elementsof-not-sizeof#comment-18</link>
 <description>&lt;p&gt;I like to use:&lt;/p&gt;

&lt;div style=&quot;padding: 5px !important; border: 1px solid rgb(253,187,134) !important; background-color: rgb(255,253,245) !important; font-family: Verdana,sans-serif !important;&quot;&gt;&lt;div style=&quot;background-color: rgb(255,250,238) !important;&quot;&gt;&lt;div class=&quot;geshi-cpp&quot;&gt;&lt;div style=&quot;background-color:rgb(255,234,216);&quot;&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;template&lt;/span&gt; &amp;lt;class T, &lt;span style=&quot;color: #0000ff;&quot;&gt;unsigned&lt;/span&gt; S&amp;gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;char&lt;/span&gt; &lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#40;&lt;/span&gt;&amp;amp; lengthof&lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#40;&lt;/span&gt;T &lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#40;&lt;/span&gt;&amp;amp;&lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#91;&lt;/span&gt;S&lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#91;&lt;/span&gt;S&lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#93;&lt;/span&gt;;&lt;br /&gt;&lt;/div&gt;
&lt;span style=&quot;color: #339900;&quot;&gt;#define lengthof(a) (sizeof(lengthof(a))) &lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;That way it protects against doing a lengthof on a pointer instead of an array. Doesn&amp;#8217;t work for local types though because of the way C++ templates work but you run into those rarely and easy to work around.&lt;/p&gt;
</description>
 <pubDate>Mon, 23 Aug 2004 17:53:31 +0000</pubDate>
 <dc:creator>asdf</dc:creator>
 <guid isPermaLink="false">comment 18 at https://mg.to</guid>
</item>
<item>
 <title>Thanks for the sharp eye, Stephanie.</title>
 <link>https://mg.to/2004/08/07/use-elementsof-not-sizeof#comment-17</link>
 <description>&lt;p&gt;Thanks for the sharp eye, Stephanie. That&amp;#8217;s what I get for posting in the wee hours! Fixed now.&lt;/p&gt;</description>
 <pubDate>Sat, 07 Aug 2004 10:07:58 +0000</pubDate>
 <dc:creator>Michael Geary</dc:creator>
 <guid isPermaLink="false">comment 17 at https://mg.to</guid>
</item>
<item>
 <title>Small typo in the last code bl</title>
 <link>https://mg.to/2004/08/07/use-elementsof-not-sizeof#comment-16</link>
 <description>&lt;p&gt;Small typo in the last code block: elementsof(pszPath) should be elementsof(szPath).&lt;/p&gt;</description>
 <pubDate>Sat, 07 Aug 2004 07:18:24 +0000</pubDate>
 <dc:creator>Stephane Lajoie</dc:creator>
 <guid isPermaLink="false">comment 16 at https://mg.to</guid>
</item>
<item>
 <title>Use elementsof(sz), not sizeof(sz)</title>
 <link>https://mg.to/2004/08/07/use-elementsof-not-sizeof</link>
 <description>&lt;p&gt;I had to fix a bug recently where my &lt;a href=&quot;https://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/shell/programmersguide/shell_int/shell_int_extending/extensionhandlers/shell_ext.asp&quot;&gt;shell extension&lt;/a&gt; was crashing another application when you used that app&amp;#8217;s File Open dialog.&lt;/p&gt;

&lt;p&gt;This application has a thumbnail view of the selected file in the File Open dialog, which they generate the same way as Windows Explorer: by loading a shell extension for the selected filetype and calling its &lt;a href=&quot;https://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/shell/reference/ifaces/iextractimage/iextractimage.asp&quot;&gt;&lt;code&gt;IExtractImage&lt;/code&gt;&lt;/a&gt; interface. It&amp;#8217;s a fairly weird protocol: First they call your &lt;a href=&quot;https://msdn.microsoft.com/library/default.asp?url=/library/en-us/com/htm/cmi_n2p_0p7o.asp&quot;&gt;&lt;code&gt;IPersistFile::Load&lt;/code&gt;&lt;/a&gt; to give you the filename, then you give them back the same filename when they call &lt;a href=&quot;https://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/shell/reference/ifaces/iextractimage/GetLocation.asp&quot;&gt;&lt;code&gt;IExtractImage::GetLocation&lt;/code&gt;&lt;/a&gt;. Finally they call &lt;a href=&quot;https://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/shell/reference/ifaces/iextractimage/Extract.asp&quot;&gt;&lt;code&gt;IExtractImage::Extract&lt;/code&gt;&lt;/a&gt; and that&amp;#8217;s when you generate the thumbnail.&lt;/p&gt;

&lt;p&gt;But, after my &lt;code&gt;GetLocation&lt;/code&gt; method returned, the application silently exited. What could be wrong? My code worked fine in Explorer.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;GetLocation&lt;/code&gt; is a typical function that takes a character string buffer and length along with some other parameters (omitted here):&lt;/p&gt;

&lt;div style=&quot;padding: 5px !important; border: 1px solid rgb(253,187,134) !important; background-color: rgb(255,253,245) !important; font-family: Verdana,sans-serif !important;&quot;&gt;&lt;div style=&quot;background-color: rgb(255,250,238) !important;&quot;&gt;&lt;div class=&quot;geshi-c&quot;&gt;&lt;div style=&quot;background-color:rgb(255,234,216);&quot;&gt;HRESULT GetLocation&lt;span style=&quot;color: #66cc66;color: #0000FF;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;
&amp;nbsp; &amp;nbsp; LPWSTR pszPathBuffer,&lt;br /&gt;
&lt;div style=&quot;background-color:rgb(255,234,216);&quot;&gt;&amp;nbsp; &amp;nbsp; DWORD cchMax, etc. &lt;span style=&quot;color: #66cc66;color: #0000FF;&quot;&gt;&amp;#41;&lt;/span&gt;; &lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;I noticed that this other app was giving me an unusually large file pathname buffer, 520 characters or 1040 bytes to be exact. This number sounded strangely familiar (and not just because of &lt;a href=&quot;https://www.irs.gov/pub/irs-pdf/f1040.pdf&quot;&gt;this&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;Then I realized what happened. I&amp;#8217;ve never seen the source code for this app I was crashing, but I just know it looked like this:&lt;/p&gt;

&lt;div style=&quot;padding: 5px !important; border: 1px solid rgb(253,187,134) !important; background-color: rgb(255,253,245) !important; font-family: Verdana,sans-serif !important;&quot;&gt;&lt;div style=&quot;background-color: rgb(255,250,238) !important;&quot;&gt;&lt;div class=&quot;geshi-c&quot;&gt;&lt;div style=&quot;background-color:rgb(255,234,216);&quot;&gt;WCHAR szPath&lt;span style=&quot;color: #66cc66;color: #0000FF;&quot;&gt;&amp;#91;&lt;/span&gt;MAX_PATH&lt;span style=&quot;color: #66cc66;color: #0000FF;&quot;&gt;&amp;#93;&lt;/span&gt;;&lt;br /&gt;&lt;/div&gt;
pExtractImage-&amp;gt;GetLocation&lt;span style=&quot;color: #66cc66;color: #0000FF;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;background-color:rgb(255,234,216);&quot;&gt;&amp;nbsp; &amp;nbsp; szPath, &lt;span style=&quot;color: #993333;&quot;&gt;sizeof&lt;/span&gt;&lt;span style=&quot;color: #66cc66;color: #0000FF;&quot;&gt;&amp;#40;&lt;/span&gt;szPath&lt;span style=&quot;color: #66cc66;color: #0000FF;&quot;&gt;&amp;#41;&lt;/span&gt;, etc. &lt;span style=&quot;color: #66cc66;color: #0000FF;&quot;&gt;&amp;#41;&lt;/span&gt;; &lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Oops. The &lt;code&gt;cchMax&lt;/code&gt; argument to &lt;code&gt;GetLocation&lt;/code&gt; is a length in &lt;em&gt;characters&lt;/em&gt;, but &lt;code&gt;sizeof&lt;/code&gt; gives you the size in &lt;em&gt;bytes&lt;/em&gt;. And we&amp;#8217;re talking &lt;code&gt;WCHAR&lt;/code&gt; here, so each character is two bytes. &lt;code&gt;MAX_PATH&lt;/code&gt; is 260, making &lt;code&gt;szPath&lt;/code&gt; 520 bytes long, the number that they passed into my code.&lt;/p&gt;

&lt;p&gt;One way to fix the problem is:&lt;/p&gt;

&lt;div style=&quot;padding: 5px !important; border: 1px solid rgb(253,187,134) !important; background-color: rgb(255,253,245) !important; font-family: Verdana,sans-serif !important;&quot;&gt;&lt;div style=&quot;background-color: rgb(255,250,238) !important;&quot;&gt;&lt;div class=&quot;geshi-c&quot;&gt;&lt;div style=&quot;background-color:rgb(255,234,216);&quot;&gt;WCHAR szPath&lt;span style=&quot;color: #66cc66;color: #0000FF;&quot;&gt;&amp;#91;&lt;/span&gt;MAX_PATH&lt;span style=&quot;color: #66cc66;color: #0000FF;&quot;&gt;&amp;#93;&lt;/span&gt;;&lt;br /&gt;&lt;/div&gt;
pExtractImage-&amp;gt;GetLocation&lt;span style=&quot;color: #66cc66;color: #0000FF;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;background-color:rgb(255,234,216);&quot;&gt;&amp;nbsp; &amp;nbsp; szPath, MAX_PATH, etc. &lt;span style=&quot;color: #66cc66;color: #0000FF;&quot;&gt;&amp;#41;&lt;/span&gt;; &lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;That gives correct code, but I never like seeing &lt;code&gt;MAX_PATH&lt;/code&gt; repeated like this. &lt;code&gt;sizeof&lt;/code&gt; is in the right spirit, actually measuring the array length instead of repeating a constant, but it measures the wrong thing, bytes instead of characters (array elements).&lt;/p&gt;

&lt;p&gt;I like to code this with the &lt;code&gt;elementsof&lt;/code&gt; macro, defined as:&lt;/p&gt;

&lt;div style=&quot;padding: 5px !important; border: 1px solid rgb(253,187,134) !important; background-color: rgb(255,253,245) !important; font-family: Verdana,sans-serif !important;&quot;&gt;&lt;div style=&quot;background-color: rgb(255,250,238) !important;&quot;&gt;&lt;div class=&quot;geshi-c&quot;&gt;&lt;div style=&quot;background-color:rgb(255,234,216);&quot;&gt;&lt;span style=&quot;color: #339933;&quot;&gt;#define elementsof( array )&amp;nbsp; &lt;/span&gt;&lt;br /&gt;&lt;/div&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;color: #0000FF;&quot;&gt;&amp;#40;&lt;/span&gt; &lt;span style=&quot;color: #993333;&quot;&gt;sizeof&lt;/span&gt;&lt;span style=&quot;color: #66cc66;color: #0000FF;&quot;&gt;&amp;#40;&lt;/span&gt;array&lt;span style=&quot;color: #66cc66;color: #0000FF;&quot;&gt;&amp;#41;&lt;/span&gt; / &lt;span style=&quot;color: #993333;&quot;&gt;sizeof&lt;/span&gt;&lt;span style=&quot;color: #66cc66;color: #0000FF;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;color: #0000FF;&quot;&gt;&amp;#40;&lt;/span&gt;array&lt;span style=&quot;color: #66cc66;color: #0000FF;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;color: #0000FF;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #cc66cc;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #66cc66;color: #0000FF;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;color: #0000FF;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #66cc66;color: #0000FF;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Then you can just use &lt;code&gt;elementsof&lt;/code&gt; instead of &lt;code&gt;sizeof&lt;/code&gt;:&lt;/p&gt;

&lt;div style=&quot;padding: 5px !important; border: 1px solid rgb(253,187,134) !important; background-color: rgb(255,253,245) !important; font-family: Verdana,sans-serif !important;&quot;&gt;&lt;div style=&quot;background-color: rgb(255,250,238) !important;&quot;&gt;&lt;div class=&quot;geshi-c&quot;&gt;&lt;div style=&quot;background-color:rgb(255,234,216);&quot;&gt;WCHAR szPath&lt;span style=&quot;color: #66cc66;color: #0000FF;&quot;&gt;&amp;#91;&lt;/span&gt;MAX_PATH&lt;span style=&quot;color: #66cc66;color: #0000FF;&quot;&gt;&amp;#93;&lt;/span&gt;;&lt;br /&gt;&lt;/div&gt;
pExtractImage-&amp;gt;GetLocation&lt;span style=&quot;color: #66cc66;color: #0000FF;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;background-color:rgb(255,234,216);&quot;&gt;&amp;nbsp; &amp;nbsp; szPath, elementsof&lt;span style=&quot;color: #66cc66;color: #0000FF;&quot;&gt;&amp;#40;&lt;/span&gt;szPath&lt;span style=&quot;color: #66cc66;color: #0000FF;&quot;&gt;&amp;#41;&lt;/span&gt;, etc. &lt;span style=&quot;color: #66cc66;color: #0000FF;&quot;&gt;&amp;#41;&lt;/span&gt;; &lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;code&gt;elementsof&lt;/code&gt; is handy anytime you need the length of a character string array or any array.&lt;/p&gt;

&lt;p&gt;Of course, I didn&amp;#8217;t have the luxury of fixing this code at its source (other than reporting the bug to the program&amp;#8217;s authors). So I worked around it by checking for the bogus 520 character &lt;code&gt;cchMax&lt;/code&gt; and cutting it back to 260 (&lt;code&gt;MAX_PATH&lt;/code&gt;) characters.&lt;/p&gt;
</description>
 <comments>https://mg.to/2004/08/07/use-elementsof-not-sizeof#comments</comments>
 <category domain="https://mg.to/topics/programming/c-c">C/C++</category>
 <category domain="https://mg.to/topics/programming">Programming</category>
 <category domain="https://mg.to/topics/programming/win32.html">Win32</category>
 <pubDate>Sat, 07 Aug 2004 02:36:47 +0000</pubDate>
 <dc:creator>Michael Geary</dc:creator>
 <guid isPermaLink="false">9 at https://mg.to</guid>
</item>
</channel>
</rss>
