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. const_cast problem??

const_cast problem??

Scheduled Pinned Locked Moved C / C++ / MFC
comtoolsperformancehelpquestion
7 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.
  • B Offline
    B Offline
    Bob Stanneveld
    wrote on last edited by
    #1

    Hello, I've encountered a strange problem when I use the const_cast() operator. See the following code that behaves strange:

    std::stringstream sstrMsg;

    // bla bla bla

    char* pszMsg = const_cast(sstrMsg.str().c_str());
    SPrintBuffer(&pszMsg);

    I've checked the return values of both stringstream::str() and std::string::c_str(). They both return the expected data. Strangely enough, after the cast, pszMsg points to some undefined memory address. (The address pointed to holds the value -18). I'm currently using the following workaround:

    char* pszMsg = new char[sstrMsg.str().length()];
    strncpy(pszMsg, sstrMsg.str().c_str(), sstrMsg.str().length());
    SPrintBuffer(&pszMsg);
    delete[] pszMsg;

    Does anybody have a clue why const_cast is producing undesired results? Behind every great black man...             ... is the police. - Conspiracy brother Blog[^]

    B J 2 Replies Last reply
    0
    • B Bob Stanneveld

      Hello, I've encountered a strange problem when I use the const_cast() operator. See the following code that behaves strange:

      std::stringstream sstrMsg;

      // bla bla bla

      char* pszMsg = const_cast(sstrMsg.str().c_str());
      SPrintBuffer(&pszMsg);

      I've checked the return values of both stringstream::str() and std::string::c_str(). They both return the expected data. Strangely enough, after the cast, pszMsg points to some undefined memory address. (The address pointed to holds the value -18). I'm currently using the following workaround:

      char* pszMsg = new char[sstrMsg.str().length()];
      strncpy(pszMsg, sstrMsg.str().c_str(), sstrMsg.str().length());
      SPrintBuffer(&pszMsg);
      delete[] pszMsg;

      Does anybody have a clue why const_cast is producing undesired results? Behind every great black man...             ... is the police. - Conspiracy brother Blog[^]

      B Offline
      B Offline
      BadKarma
      wrote on last edited by
      #2

      Hi, If you mean the const_cast casting operator, I believe you have used it wrong. It should be used like this: char* pszMsg = const_cast<char*>(sstrMsg.str().c_str()); This shouldn't give any problems. codito ergo sum

      B 1 Reply Last reply
      0
      • B Bob Stanneveld

        Hello, I've encountered a strange problem when I use the const_cast() operator. See the following code that behaves strange:

        std::stringstream sstrMsg;

        // bla bla bla

        char* pszMsg = const_cast(sstrMsg.str().c_str());
        SPrintBuffer(&pszMsg);

        I've checked the return values of both stringstream::str() and std::string::c_str(). They both return the expected data. Strangely enough, after the cast, pszMsg points to some undefined memory address. (The address pointed to holds the value -18). I'm currently using the following workaround:

        char* pszMsg = new char[sstrMsg.str().length()];
        strncpy(pszMsg, sstrMsg.str().c_str(), sstrMsg.str().length());
        SPrintBuffer(&pszMsg);
        delete[] pszMsg;

        Does anybody have a clue why const_cast is producing undesired results? Behind every great black man...             ... is the police. - Conspiracy brother Blog[^]

        J Offline
        J Offline
        John R Shaw
        wrote on last edited by
        #3

        Your code looks very dangerous. Q1) Why would you need to cast away the constancy of the pointer returned by c_str()? How about using: const char* pszMsg = sstrMsg.str().c_str(); Q2) Why would a function named SPrintBuffer() need a pointer to a pointer of type char*? The name implies that it is just going to print out the contents of the buffer, not modify it. Note: Modifing the data return via c_str(), is a very bad idea. INTP Every thing is relative...

        B 1 Reply Last reply
        0
        • J John R Shaw

          Your code looks very dangerous. Q1) Why would you need to cast away the constancy of the pointer returned by c_str()? How about using: const char* pszMsg = sstrMsg.str().c_str(); Q2) Why would a function named SPrintBuffer() need a pointer to a pointer of type char*? The name implies that it is just going to print out the contents of the buffer, not modify it. Note: Modifing the data return via c_str(), is a very bad idea. INTP Every thing is relative...

          B Offline
          B Offline
          Bob Stanneveld
          wrote on last edited by
          #4

          I know that the code looks very dangerous. I double checked the code for SPrintBuffer() and it doesn't modify the string. The reason that the reason the function takes just a char* and not a const char* is unknown to me. I need to add a link layer of a protocol to a program that my teacher wrote. Please don't tell me, the entire program is badly designed and all the code looks like spaghetti... If you want to see some obfuscated C code, this program is right for you! Behind every great black man...             ... is the police. - Conspiracy brother Blog[^]

          J 1 Reply Last reply
          0
          • B BadKarma

            Hi, If you mean the const_cast casting operator, I believe you have used it wrong. It should be used like this: char* pszMsg = const_cast<char*>(sstrMsg.str().c_str()); This shouldn't give any problems. codito ergo sum

            B Offline
            B Offline
            Bob Stanneveld
            wrote on last edited by
            #5

            Hello, I used it just like you said it should be used. It's just that the '<' and the '>' 'disappeared' because I didn't use '<' and '>' respectively. Sometimes the parser for the message removes those in pre tags and sometimes it doesn't. Behind every great black man...             ... is the police. - Conspiracy brother Blog[^]

            1 Reply Last reply
            0
            • B Bob Stanneveld

              I know that the code looks very dangerous. I double checked the code for SPrintBuffer() and it doesn't modify the string. The reason that the reason the function takes just a char* and not a const char* is unknown to me. I need to add a link layer of a protocol to a program that my teacher wrote. Please don't tell me, the entire program is badly designed and all the code looks like spaghetti... If you want to see some obfuscated C code, this program is right for you! Behind every great black man...             ... is the police. - Conspiracy brother Blog[^]

              J Offline
              J Offline
              John R Shaw
              wrote on last edited by
              #6

              Sorry I have been gone for a few days.:( 1) It sounds like your teachers code would not pass a review by me, let alone a quality assurance team. If the function does not modify the data pointed to by the argument, then you should say so by making it a pointer to constant data. 2) You still should not need to use const_cast, instead you can just call the function like so: SPrintBuffer((char*)str.c_str());. Note: The const_cast should not be needed in this particular case. Note: In your original code you are trying to pass a char** and not a char*

              B 1 Reply Last reply
              0
              • J John R Shaw

                Sorry I have been gone for a few days.:( 1) It sounds like your teachers code would not pass a review by me, let alone a quality assurance team. If the function does not modify the data pointed to by the argument, then you should say so by making it a pointer to constant data. 2) You still should not need to use const_cast, instead you can just call the function like so: SPrintBuffer((char*)str.c_str());. Note: The const_cast should not be needed in this particular case. Note: In your original code you are trying to pass a char** and not a char*

                B Offline
                B Offline
                Bob Stanneveld
                wrote on last edited by
                #7

                John R. Shaw wrote:

                Sorry I have been gone for a few days.

                So have I... :-D

                John R. Shaw wrote:

                1. It sounds like your teachers code would not pass a review by me, let alone a quality assurance team.

                That was my first thought and I offered to rewrite the entire program instead of doing all the other assignments. This was a no-go since the course is not going to be given anymore. Anyway, that's the way we learn things in college these days.. :sigh:

                John R. Shaw wrote:

                1. You still should not need to use const_cast, instead you can just call the function like so: SPrintBuffer((char*)str.c_str());.

                I force myself to use straight C++ only. The C-Style casts are there fore backwards compatibility. They shouldn't make a difference. I'll try it and let you know if there it will work. Behind every great black man...             ... is the police. - Conspiracy brother Blog[^]

                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