Site Tools


Differences

This shows you the differences between two versions of the page.

Link to this comparison view

developer:sdksamples:countof [2015/09/14] (current)
Line 1: Line 1:
 +====== Info: Using the sizeof operator with TCHAR and wchar_t ======
 +====== C++ ======
 +
 +The **sizeof** keyword gives the amount of storage, in bytes, associated with a variable or a type. It does not return number of elements in an array, such as an array of characters.
 +
 +Mistakes using the **sizeof** operator when dealing with UNICODE strings is very common. ​ For example:
 +
 +
 +<code c++>
 +char szBuffer[24];​
 +GetWindowText( hWnd, szBuffer, sizeof(szBuffer) ); // OK
 +
 +wchar_t wszBuffer[24];​
 +GetWindowText( hWnd, wszBuffer, sizeof(wszBuffer) ); // WRONG
 +
 +</​code>​
 +In the above examples, the first block of code works because the **char** data type just happens to take up only one byte of storage. Thus, the **sizeof** operator, when used on an array of chars just happens to work. The other example is incorrect for a **wchar_t** requires two bytes of storage. Thus, you have just told the **GetWindowText** that the buffer you have passed in is bigger than it really is. This can crash your plug-in.
 +
 +The following is the proper method for using the **sizeof** operator when using UNICODE text strings.
 +
 +
 +<code c++>
 +wchar_t wszBuffer[24];​
 +GetWindowText( hWnd, wszBuffer, sizeof(wszBuffer)/​sizeof(wchar_t) );
 +
 +</​code>​
 +If you find yourself writing the above statement frequently, you might consider adding the following macro to your project.
 +
 +
 +<code c++>
 +#define _countof(array) (sizeof(array)/​sizeof(array[0]))
 +
 +wchar_t wszBuffer[24];​
 +GetWindowText( hWnd, wszBuffer, _countof(wszBuffer) );
 +
 +</​code>​
 +Note, the **TCHAR** data type definition is based on whether or not your plug-ins compile as MBCS or as UNICODE. Rhino 3.0 plug-ins are MBCS. Thus, a **TCHAR** is really just a **char**. Rhino 4.0 plug-ins are compiled as UNICODE. Thus, a **TCHAR** in Rhino 4.0 will be a **wchar_t**.
 +
 +To be safe in all cases, you should use the following convention when dealing with TCHARs:
 +
 +
 +<code c++>
 +TCHAR tchBuffer[24];​
 +GetWindowText( hWnd, tchBuffer, sizeof(tchBuffer)/​sizeof(TCHAR) );
 +
 +</​code>​
 +In doing this, your code will be safe when compiled as either MBCS or UNICODE.
 +
 +
 +
 +{{tag>​Developer cplusplus}}
  
developer/sdksamples/countof.txt ยท Last modified: 2015/09/14 (external edit)