const_cast problem??
-
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()
andstd::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[^] -
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()
andstd::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[^] -
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()
andstd::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[^]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 namedSPrintBuffer()
need a pointer to a pointer of typechar*
? The name implies that it is just going to print out the contents of the buffer, not modify it. Note: Modifing the data return viac_str()
, is a very bad idea. INTP Every thing is relative... -
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 namedSPrintBuffer()
need a pointer to a pointer of typechar*
? The name implies that it is just going to print out the contents of the buffer, not modify it. Note: Modifing the data return viac_str()
, is a very bad idea. INTP Every thing is relative...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 achar*
and not aconst 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[^] -
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 sumHello, 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[^]
-
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 achar*
and not aconst 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[^]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: Theconst_cast
should not be needed in this particular case. Note: In your original code you are trying to pass achar**
and not achar*
-
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: Theconst_cast
should not be needed in this particular case. Note: In your original code you are trying to pass achar**
and not achar*
John R. Shaw wrote:
Sorry I have been gone for a few days.
So have I... :-D
John R. Shaw wrote:
- 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:
- 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[^]