Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Strange behavior of compiler [modified]

Strange behavior of compiler [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
helpvisual-studiotutorial
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Maxim Zarus
    wrote on last edited by
    #1

    Hi experts, I faced following situation sometimes... I am using VS 2005. Sometimes Compiler forgets to warn or raise an error about inappropriate string initialization or functions or other string manipulation. For example: CString str = ”Hello”; Gives error… and I correct this by prefixing “L” CString str = L”Hello”; But sometimes somewhere in my code inside any function or loop CString otherStr = ”Hello world”; Gives neither warning nor error and runs correctly. And when i change/modify that string CString otherStr = ”Hello world”; it gives error. Finally i correct like this CString otherStr = L”Hello world”; I am unable to understand this.please help me to understand this. Thanks

    modified on Friday, May 16, 2008 5:59 AM

    T 1 Reply Last reply
    0
    • M Maxim Zarus

      Hi experts, I faced following situation sometimes... I am using VS 2005. Sometimes Compiler forgets to warn or raise an error about inappropriate string initialization or functions or other string manipulation. For example: CString str = ”Hello”; Gives error… and I correct this by prefixing “L” CString str = L”Hello”; But sometimes somewhere in my code inside any function or loop CString otherStr = ”Hello world”; Gives neither warning nor error and runs correctly. And when i change/modify that string CString otherStr = ”Hello world”; it gives error. Finally i correct like this CString otherStr = L”Hello world”; I am unable to understand this.please help me to understand this. Thanks

      modified on Friday, May 16, 2008 5:59 AM

      T Offline
      T Offline
      toxcct
      wrote on last edited by
      #2

      all depends if you have unicode enabled in you project settings or not. when you assign litterals to CStrings, you have to use _T() or TEXT() macros.

      CString str = _T("Hello"); //OK
      CStringA strAnsi = "Hello"; //OK
      CStringW strUni = L"Hello" //OK

      the _T() macros will expand the literal text to either ansi or unicode regarding your project settings... for your knowledge, here is how it's defined (kinda) :

      #ifdef _UNICODE
      #define _T(x) L##x
      #else
      #define _T(x) x
      #endif

      [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

      M D 2 Replies Last reply
      0
      • T toxcct

        all depends if you have unicode enabled in you project settings or not. when you assign litterals to CStrings, you have to use _T() or TEXT() macros.

        CString str = _T("Hello"); //OK
        CStringA strAnsi = "Hello"; //OK
        CStringW strUni = L"Hello" //OK

        the _T() macros will expand the literal text to either ansi or unicode regarding your project settings... for your knowledge, here is how it's defined (kinda) :

        #ifdef _UNICODE
        #define _T(x) L##x
        #else
        #define _T(x) x
        #endif

        [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

        M Offline
        M Offline
        Maxim Zarus
        wrote on last edited by
        #3

        Sorry i forgot to say that when i change/modify that string CString otherStr = ”Hello world”; it gives error.

        But sometimes somewhere in my code inside any function or loop CString otherStr = ”Hello world”; Gives neither warning nor error and runs correctly.

        I mean if i touch that string, it starts to give error. And finally i correct like this CString otherStr = L”Hello world”;

        P 1 Reply Last reply
        0
        • M Maxim Zarus

          Sorry i forgot to say that when i change/modify that string CString otherStr = ”Hello world”; it gives error.

          But sometimes somewhere in my code inside any function or loop CString otherStr = ”Hello world”; Gives neither warning nor error and runs correctly.

          I mean if i touch that string, it starts to give error. And finally i correct like this CString otherStr = L”Hello world”;

          P Offline
          P Offline
          prasad_som
          wrote on last edited by
          #4

          Best way is to use _T macro. It will take care of everything. Cstring str= _T("test it");

          M 1 Reply Last reply
          0
          • P prasad_som

            Best way is to use _T macro. It will take care of everything. Cstring str= _T("test it");

            M Offline
            M Offline
            Maxim Zarus
            wrote on last edited by
            #5

            you are right.:) but i was talking not only string intialization also about functions like: strcpy()also shows the same behavior. it accepts sometimes CString parameters while i am working with unicode settings.

            T 1 Reply Last reply
            0
            • M Maxim Zarus

              you are right.:) but i was talking not only string intialization also about functions like: strcpy()also shows the same behavior. it accepts sometimes CString parameters while i am working with unicode settings.

              T Offline
              T Offline
              toxcct
              wrote on last edited by
              #6

              it's because strcpy() is accepting ansi strings (char*) only, and if you have unicode defined, your CStrings convert implicitely to unicode string, not ansi (wchar_t*)

              [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

              1 Reply Last reply
              0
              • T toxcct

                all depends if you have unicode enabled in you project settings or not. when you assign litterals to CStrings, you have to use _T() or TEXT() macros.

                CString str = _T("Hello"); //OK
                CStringA strAnsi = "Hello"; //OK
                CStringW strUni = L"Hello" //OK

                the _T() macros will expand the literal text to either ansi or unicode regarding your project settings... for your knowledge, here is how it's defined (kinda) :

                #ifdef _UNICODE
                #define _T(x) L##x
                #else
                #define _T(x) x
                #endif

                [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #7

                But why would the compiler complain about one and not the other? The OP stated that it only starts to complain about that other (second) one if it is modified?

                "Love people and use things, not love things and use people." - Unknown

                "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                M 1 Reply Last reply
                0
                • D David Crow

                  But why would the compiler complain about one and not the other? The OP stated that it only starts to complain about that other (second) one if it is modified?

                  "Love people and use things, not love things and use people." - Unknown

                  "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                  M Offline
                  M Offline
                  Maxim Zarus
                  wrote on last edited by
                  #8

                  DavidCrow wrote:

                  But why would the compiler complain about one and not the other? The OP stated that it only starts to complain about that other (second) one if it is modified?

                  Right. Now only you understand my question. I also want to know. Please sir, explain me about this strange behavior of complier. BTW what is OP?

                  DavidCrow wrote:

                  The OP stated that

                  :)

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups