Qt 4.5 QString have a way to output a null terminaded string ( QString.cstr() )
Qt 4.5 QString have a way to output a null terminaded string ( QString.cstr() )
Could also do this:
Qt Code:
#include <string> To copy to clipboard, switch view to plain text mode
edit*
Qt Code:
To copy to clipboard, switch view to plain text mode
how so? I use this method often to convert QStrings into c-strings and its never seg faulted. In what situtation does it seg fault?
maybe the confusion here is my semi pseudo code:
Qt Code:
const char * c_string = str.toStdString().c_str();To copy to clipboard, switch view to plain text mode
not
Qt Code:
To copy to clipboard, switch view to plain text mode
I agree putting the latter line into code would probably segfault or return NULL to the pointer. Sorry for the confusion.
No, that's still wrong. The whole concept of doing what you are doing is wrong.
Is this the whole "intermediate references smell like poo" thing?
Well could you explain why you think that? I dont see how its any different from converting to a QBtyeArray and pulling the char * out of it.
Well, this is wrong too of course:
Qt Code:
const char *cstr = str.toAscii().constData();To copy to clipboard, switch view to plain text mode
This is much better although might still be wrong in some cases:
Qt Code:
const char *cstr = ba.constData();To copy to clipboard, switch view to plain text mode
This is ok:
So is this:Qt Code:
const char *cstr = strcpy(ba.constData());To copy to clipboard, switch view to plain text mode
Qt Code:
someCallThatNeedsACStr(ba.constData());To copy to clipboard, switch view to plain text mode
Ok i see what you mean now. Yes, if you want a deep copy that you can manipulate safely strcpy() is the way to go. Other than that, this method is effectively the same as using a std::string as the medium. My code was intended for use as an argument to a function that wants a c-str. Also, getting the shallow copy isnt necessarily "wrong" it just depends on what you intend to do with it.
Yes you're right. QString().toStdString() creates a temporary std::string variable with the string value of the QString. The c_str() routine returns a pointer to memory held by that temp std::string not the original QString. I never thought of that but I really only use this method to immediately use the data for a function call. The function you use it on must deep copy the data before any other operation overwrites it. It is a very dangerous statement and can result in a segfault. Perhaps I should rethink using it. Thanks for expounding your opinion.
ok hows this:
Qt Code:
#include <string> std::string stdstr = str.toStdString(); const char * cstr = stdstr.c_str();To copy to clipboard, switch view to plain text mode
If you go through a temporary, the data might already be invalid when you enter the function.
It's already too late. Initialization of the function itself or any of its local variables might overwrite the temporary. Of course unless the temporary is created during the function call:The function you use it on must deep copy the data before any other operation overwrites it.
Qt Code:
func(str.toStdString().c_str());To copy to clipboard, switch view to plain text mode
This is ok as long as stdstr lives.ok hows this:
Qt Code:
#include <string> std::string stdstr = str.toStdString(); const char * cstr = stdstr.c_str();To copy to clipboard, switch view to plain text mode
Ok so I just went back to bjarne. According to him, a temp variable is destroyed at the end of the expression in which it is used. Becuase a function call in an expression is executed as part of the expression, the temp variable memory block will exist and be valid in this context:
Qt Code:
(void)somefunction(str.toStdString().c_str());To copy to clipboard, switch view to plain text mode
so that is correct code.
Thats not true if the function call and the temp are in a single statement (see above).
That was my point in the first place. I would only use a statement like this during the function call.
const char * str = QString().toStdString().c_str();
by itself definately goes out of scope. I was only using that as an example of the syntax
Try to avoid that. People tend to copy and paste code directly from this forum to their applications without changing it or reading the whole thread that discusses a particular piece of code. And then they will write somewhere that at Qt Centre they gave them code that didn't work.
Bookmarks