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. VC++ MFC Strlen [modified]

VC++ MFC Strlen [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
c++questionlounge
6 Posts 3 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.
  • G Offline
    G Offline
    GC104
    wrote on last edited by
    #1

    Hi, I have been trying to maniupulate a wide character string into an ansi character(8bit) string followed with some general string length checks:

    //h-file
    CString TxString; //initialised with "12345"
    char* pOutputString;
    size_t CharCountXyz;
    int CharCount2;
    int CharCount3;
    char BasicString[100];
    int CharCount9;

    //cpp-file
    ...
    pOutputString = CW2A(TxString); //convert wide character string into ansi string using MFC/ATL macro
    CharCount2 = sizeof(TxString); //returns 0x00000004 - as expected
    CharCount3 = sizeof(pOutputString); //returns 0x00000004 - as expected
    CharCountXyz = strlen(pOutputString); //returns 0xbaadfood - not what I expect!!!

    //however if the following lines are added, this previous line behaves and returns 0x00000005
    sprintf(BasicString, "ABCDEF");
    CharCount9 = strlen(BasicString);

    I added the last two lines to test strlen in its basic form, sure enough it worked when used to test 'BasicString' but curiously, the test on 'pOutputString' also worked???

    modified on Tuesday, September 8, 2009 10:50 AM

    D S 2 Replies Last reply
    0
    • G GC104

      Hi, I have been trying to maniupulate a wide character string into an ansi character(8bit) string followed with some general string length checks:

      //h-file
      CString TxString; //initialised with "12345"
      char* pOutputString;
      size_t CharCountXyz;
      int CharCount2;
      int CharCount3;
      char BasicString[100];
      int CharCount9;

      //cpp-file
      ...
      pOutputString = CW2A(TxString); //convert wide character string into ansi string using MFC/ATL macro
      CharCount2 = sizeof(TxString); //returns 0x00000004 - as expected
      CharCount3 = sizeof(pOutputString); //returns 0x00000004 - as expected
      CharCountXyz = strlen(pOutputString); //returns 0xbaadfood - not what I expect!!!

      //however if the following lines are added, this previous line behaves and returns 0x00000005
      sprintf(BasicString, "ABCDEF");
      CharCount9 = strlen(BasicString);

      I added the last two lines to test strlen in its basic form, sure enough it worked when used to test 'BasicString' but curiously, the test on 'pOutputString' also worked???

      modified on Tuesday, September 8, 2009 10:50 AM

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

      GC104 wrote:

      sprintf(BasicString, "ABCDEF");

      Since BasicString is a pointer, have you allocated memory for it?

      "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

      G 1 Reply Last reply
      0
      • G GC104

        Hi, I have been trying to maniupulate a wide character string into an ansi character(8bit) string followed with some general string length checks:

        //h-file
        CString TxString; //initialised with "12345"
        char* pOutputString;
        size_t CharCountXyz;
        int CharCount2;
        int CharCount3;
        char BasicString[100];
        int CharCount9;

        //cpp-file
        ...
        pOutputString = CW2A(TxString); //convert wide character string into ansi string using MFC/ATL macro
        CharCount2 = sizeof(TxString); //returns 0x00000004 - as expected
        CharCount3 = sizeof(pOutputString); //returns 0x00000004 - as expected
        CharCountXyz = strlen(pOutputString); //returns 0xbaadfood - not what I expect!!!

        //however if the following lines are added, this previous line behaves and returns 0x00000005
        sprintf(BasicString, "ABCDEF");
        CharCount9 = strlen(BasicString);

        I added the last two lines to test strlen in its basic form, sure enough it worked when used to test 'BasicString' but curiously, the test on 'pOutputString' also worked???

        modified on Tuesday, September 8, 2009 10:50 AM

        S Offline
        S Offline
        Stuart Dootson
        wrote on last edited by
        #3

        GC104 wrote:

        pOutputString = CW2A(TxString);

        That's a bad thing to do - CW2A creates a temporary object to manage the ASCII string used to hold the result of the wide to ASCII conversion. That temporary object is destructed at the end of the line above, causing pOutputString to point at deallocated memory.

        GC104 wrote:

        sprintf(BasicString, "ABCDEF");

        That's bad as well - mainly because you don't assign a value to BasicString - where is the "12345" going to go?!?!??! You need to learn about pointers and strings and memory management in C/C++...

        Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

        G 1 Reply Last reply
        0
        • D David Crow

          GC104 wrote:

          sprintf(BasicString, "ABCDEF");

          Since BasicString is a pointer, have you allocated memory for it?

          "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

          G Offline
          G Offline
          GC104
          wrote on last edited by
          #4

          Hi, sorry, I made a typo, should be: char BasicString[100]; mistake made due to VC++ machine not on internet. Geoff

          1 Reply Last reply
          0
          • S Stuart Dootson

            GC104 wrote:

            pOutputString = CW2A(TxString);

            That's a bad thing to do - CW2A creates a temporary object to manage the ASCII string used to hold the result of the wide to ASCII conversion. That temporary object is destructed at the end of the line above, causing pOutputString to point at deallocated memory.

            GC104 wrote:

            sprintf(BasicString, "ABCDEF");

            That's bad as well - mainly because you don't assign a value to BasicString - where is the "12345" going to go?!?!??! You need to learn about pointers and strings and memory management in C/C++...

            Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

            G Offline
            G Offline
            GC104
            wrote on last edited by
            #5

            Thanks for feedback, I see your point made about the CW2A and memory deallocation - would be nice if this was made clear in the MSDN documentation! Eventually, the "12345" will be sent through the serial port using 'WriteFile'. I have been under the imporession that the characters /strings need to be in an ansi 8 bit format for the writing to serial port to work. Any suggestions for an alternative to CW2A macro? Sprintf(BasicString, "ABCDDEF") : Bad because I haven't initialised it as soon as I declared it? or the fact that I'm using sprintf to load the string? I also take your point about needing to learn about pointers. I am wading through numerous c++ books but there's nothing like creating a real application :)

            S 1 Reply Last reply
            0
            • G GC104

              Thanks for feedback, I see your point made about the CW2A and memory deallocation - would be nice if this was made clear in the MSDN documentation! Eventually, the "12345" will be sent through the serial port using 'WriteFile'. I have been under the imporession that the characters /strings need to be in an ansi 8 bit format for the writing to serial port to work. Any suggestions for an alternative to CW2A macro? Sprintf(BasicString, "ABCDDEF") : Bad because I haven't initialised it as soon as I declared it? or the fact that I'm using sprintf to load the string? I also take your point about needing to learn about pointers. I am wading through numerous c++ books but there's nothing like creating a real application :)

              S Offline
              S Offline
              Stuart Dootson
              wrote on last edited by
              #6

              Easy one first:

              GC104 wrote:

              Sprintf(BasicString, "ABCDDEF") : Bad because I haven't initialised it as soon as I declared it? or the fact that I'm using sprintf to load the string?

              Bad because it wasn't char BasicString[100]; when I answered the question - it was (IIRC) char* BasicString :-) Also - sprintf is less than optimal for assigning a string to another string - strcpy is better, I guess.

              GC104 wrote:

              Any suggestions for an alternative to CW2A macro?

              CW2A isn't a macro. It's a class. There are a few alternatives:

              1. Make sure that pOutputString has storage associated with it and strcpy the CW2A into it:

                  char pOutputString\[1024\];
                  strcpy(pOutputString, CW2A(TxString));
                
              2. Declare pOutputString to be of type CW2A and assign TXString to it. THat way the storage is associated with the lifetime of pOutputString, which is what you want:

                CW2A pOutputString = TxString;

              3. Declare pOutputString to be of some other ASCII string type and assign CW2A(TXString) to it. That way there is storage associated with the lifetime of pOutputString, which is what you want:

                std::string pOutputString = CW2A(TxString);

              I'd go for option 2, personally.

              Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

              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