Hi,
I have a C++ program compiled under g++ 4. This program was running good when it was compiled under g++ 3. Under g++ 4, this program exits w/o any error msg. I have traced the code that caused this problem. The code is :
...
char myInput[100];
...
string strInput = "1234567890abcdefg";
...
memset(myInput, '\0', 100);
strcpy((char*)myInput, strInput.c_str()); // This is where it hangs or exits
...
So I successfully re-code this to :
...
unsigned char* myInput;
...
string strInput = "1234567890abcdefg";
...
myInput = (unsigned char*)mallac(100);
if (!myInput) {
return;
}
memset(myInput, '\0', 100);
strcpy((char*)myInput, strInput.c_str());
...
Is it not right to copy a C++ String to a C buffer? What's the difference if I copy a C++ String to a pointer to a string? By the way, I have compiled the above code using the default parameters of g++.
Thanks,
Van
View this message in context: C++ String to C buffer
Sent from the Gnu - Common C++ forum at Nabble.com.