Thread: why do my TCHARS change size?!

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    50

    Exclamation why do my TCHARS change size?!

    hi,

    I'm working in MSVC and I added UNICODE to the project settings preprocessor settings, which i thought would cause all the TCHAR's in my code to be typedef'd to wchar_t variables, ie 2 bytes wide.

    But, the size of a TCHAR seems to vary through my program. I tried to copy the technique of have functionA() and functionW() versions of a function, applying it to member functions of a class. When the program runs it will step into the functions (they are class members btw, which i have an inkling might be to do with it?) and suddenly the TCHARs are 1 byte. which is a bit of a problem when i am trying to pack pointers into a TCHAR array.

    Do i need to create seperate version of a class, eg a classA and classW and then hard-define char and wchar_t within to make this work maybe?

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    You need to define "_UNICODE" to make TCHAR a wide char.

    You should carefully consider why you are using TCHAR's in the first place.

    >> I tried to copy the technique of have functionA() and functionW() ...
    No need for that in C++, just let the compiler call the correct method for you:
    Code:
    // uncomment to test UNICODE
    //#define _UNICODE
    #include <tchar.h>
    #include <windows.h>
    
    #include<iostream>
    using namespace std;
    
    class Test
    {
    public:
        void print(const CHAR *str) {cout << "[CHAR]: " << str << endl;}
        void print(const WCHAR *str) {wcout << L"[WCHAR]: " << str << endl;}
    };//Test
    
    int main()
    {
        CHAR szStr[] = "Hello World!";
        WCHAR wzStr[] = L"Hello World!";
        TCHAR tzStr[] = _T("Hello World!");
    
        Test t;
        
        cout << "szStr = ";
        t.print(szStr);
    
        cout << "wzStr = ";
        t.print(wzStr);
    
        cout << "tzStr = ";
        t.print(tzStr);
    
        return 0;
    }//main
    gg

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    50
    Thanks, defining _UNICODE worked, and i've used overloaded functions instead of using #defines.

    I still don't understand though, why sizeof(TCHAR) evaluated to 2 in the main() function, but 1 within a class scope with just the UNICODE compiler option. Surely it should be consitent?

    NB: i am using TCHARs so i can go back and remove the unicode compiler options and compile for ASCII (eg win9x) systems. I was under the impression this was their aim?
    Last edited by reanimated; 01-11-2004 at 05:41 PM.

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    The definition of TCHAR isn't application wide. Its true type is determined at compile time for each source file that includes tchar.h
    Therefore, it is up to you to ensure that all source files are built with _UNICODE defined.

    >>I was under the impression this was their aim?
    Yup.

    gg

  5. #5
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    TCHAR is defined by including either <windows.h>(<windows.h>--><windef.h>--><winnt.h>) or <tchar.h>.

    The problem is that in <tchar.h> it is defined as WCHAR if _UNICODE is defined, while in <winnt.h> it is defined as WCHAR if UNICODE is defined.

    This means that if you do not have both of these symbols defined it depends on the order of your #includes whether you get a TCHAR which is one byte or two.

    Code:
    // Sample 1.
    #define UNICODE
    #include <tchar.h>
    #include <windows.h>
    ...
    Code:
    // Sample 2.
    #define UNICODE
    #include <windows.h>
    #include <tchar.h>
    ...
    In sample one we get a single byte TCHAR because it is defined in <tchar.h> and _UNICODE is not defined. In sample two we get a two byte TCHAR because it is defined in <winnt.h> and UNICODE is defined.

    If we define only _UNICODE then we can reverse the above samples.

    The morals of this story are:
    1. Always define both UNICODE and _UNICODE.
    2. Something as simple as changing the order of #includes can introduce hard to diagnose bugs in to your program.
    3. Make sure item no. 2 doesn't apply to your header files.
    4. Always check for the boogie monster under your bed before going to sleep at night.

  6. #6
    Registered User
    Join Date
    Oct 2003
    Posts
    50
    Thanks for the help, particulary on the #include order. It would've taken me forever to think of something as subtle as that

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory leaks problem in C -- Help please
    By Amely in forum C Programming
    Replies: 14
    Last Post: 05-21-2008, 11:16 AM
  2. Need help with C program
    By ChrisH in forum C Programming
    Replies: 38
    Last Post: 11-13-2004, 01:11 AM
  3. adding a function to a program
    By trippedwire in forum C++ Programming
    Replies: 9
    Last Post: 09-30-2004, 12:35 PM
  4. Changing a Structures Members array size
    By Xei in forum C++ Programming
    Replies: 1
    Last Post: 11-07-2002, 07:45 PM
  5. help with calculating change from a dollar
    By z.tron in forum C++ Programming
    Replies: 3
    Last Post: 09-13-2002, 03:58 PM