can not print correctly after convert QString to char *
-
Env: vs2017 , Qt 5.14.0, Qt 5.12.6
code:QString s = "hello world"; qDebug() << s; qDebug() << s.toUtf8().data(); char* p = s.toUtf8().data(); qDebug() << p; QByteArray b = s.toUtf8(); p = b.data(); qDebug() << p;
output:
"hello world" hello world ????????????????????????????????????????????7 hello world
can somebody explain this, I'd be appreciate
-
@Mozzie said in can not print correctly after convert QString to char *:
char* p = s.toUtf8().data();
C++ basics - you're creating a temporary here so p points to garbage after this statement.
@Christian-Ehrlicher
Thank you very much, and thank other replyer.
I think i understand your reply, and I do fogot the temp object , maybe because I also use java a lot.and i alse have a few questions:
- where is the temp object in memory, stack or heap or somewhere else.
- if it is on stack, it can not remain until the stack is finished
-
@Mozzie You had a bit of bad luck, if you compile in Release mode instead of Debug it'll work fine
"hello world" hello world hello world hello world
And if you switch to MinGW compiler it'll work both in Debug and Release :-)
@hskoglund
Your findings are even more scary in view of the above conversation! :)@aha_1980 , and others
I think I get it. Also that it's nothing to do with Qt specific classes. Not because of sharedQByteArray
s and stuff.So to summarize:
s.toUtf8()
only "lasts" for the lifetime of the statement (probably rather expression) it is in. But if you goQByteArray b = s.toUtf8()
then theb
will persist OK as usual. Right? -
@Mozzie You had a bit of bad luck, if you compile in Release mode instead of Debug it'll work fine
"hello world" hello world hello world hello world
And if you switch to MinGW compiler it'll work both in Debug and Release :-)
@hskoglund said in can not print correctly after convert QString to char *:
And if you switch to MinGW compiler it'll work both in Debug and Release :-)
Today. Tomorrow it will run away with your wife, bankrupt your workplace and aim for world domination.
-
@hskoglund
Your findings are even more scary in view of the above conversation! :)@aha_1980 , and others
I think I get it. Also that it's nothing to do with Qt specific classes. Not because of sharedQByteArray
s and stuff.So to summarize:
s.toUtf8()
only "lasts" for the lifetime of the statement (probably rather expression) it is in. But if you goQByteArray b = s.toUtf8()
then theb
will persist OK as usual. Right?Yes! I's just luck that the bits are still around in Release mode. The Debug mode output of ??????? could happen in Release also some other day when the sun doesn't shin.e
Anyway, one simple modification to make it waterproof could be:
QString s = "hello world"; qDebug() << s; qDebug() << s.toUtf8().data(); QByteArray a = s.toUtf8(); char* p = a.data(); qDebug() << p; QByteArray b = s.toUtf8(); p = b.data(); qDebug() << p;
Edit: too fast, didn't read the code in the 3d paragraph ! But they are both waterproof now :-)
-
@Mozzie You had a bit of bad luck, if you compile in Release mode instead of Debug it'll work fine
"hello world" hello world hello world hello world
And if you switch to MinGW compiler it'll work both in Debug and Release :-)
@hskoglund
that is interesting .
i dont have test on linux or MinGW, maybe vs and MinGW is diffrent on deal with temp object? -
@hskoglund
that is interesting .
i dont have test on linux or MinGW, maybe vs and MinGW is diffrent on deal with temp object? -
@Christian-Ehrlicher
Thank you very much, and thank other replyer.
I think i understand your reply, and I do fogot the temp object , maybe because I also use java a lot.and i alse have a few questions:
- where is the temp object in memory, stack or heap or somewhere else.
- if it is on stack, it can not remain until the stack is finished
@Mozzie said in can not print correctly after convert QString to char *:
where is the temp object in memory, stack or heap or somewhere else.
It's on the stack since you did not allocate it with new
if it is on stack, it can not remain until the stack is finished
No, this is not allowed since it's unnamed.
It's also not c++ specific - you can do the same (in a little bit more obvious way) in C:
int *myPtr = nullptr; { int a = 3; myPtr = &a; printf("%d\n", *myPtr); // works fine } printf("%d\n", *myPtr); // works on garbage and may eat kitten
-
@Mozzie said in can not print correctly after convert QString to char *:
where is the temp object in memory, stack or heap or somewhere else.
It's on the stack since you did not allocate it with new
if it is on stack, it can not remain until the stack is finished
No, this is not allowed since it's unnamed.
It's also not c++ specific - you can do the same (in a little bit more obvious way) in C:
int *myPtr = nullptr; { int a = 3; myPtr = &a; printf("%d\n", *myPtr); // works fine } printf("%d\n", *myPtr); // works on garbage and may eat kitten
@Christian-Ehrlicher said in can not print correctly after convert QString to char *:
int *myPtr = nullptr;
Never heard of
nullptr
in C ;-)NULL
was much nicer to read anyway. -
@aha_1980
Wow, OK, yes, I need to read! My problem is I have been "spoiled" by using C# and then Python/PyQt/PySide2 for so long now that I rarely have to think about this!So let's take a basic, if my C++ holds up. If I write a function
QByteArray func() { QByteArray qb; return qb; }
does that return such a "temporary object"? And that would be true for any class/struct I decalred and then returned in that fashion?
This post is deleted! -
@Mozzie said in can not print correctly after convert QString to char *:
where is the temp object in memory, stack or heap or somewhere else.
It's on the stack since you did not allocate it with new
if it is on stack, it can not remain until the stack is finished
No, this is not allowed since it's unnamed.
It's also not c++ specific - you can do the same (in a little bit more obvious way) in C:
int *myPtr = nullptr; { int a = 3; myPtr = &a; printf("%d\n", *myPtr); // works fine } printf("%d\n", *myPtr); // works on garbage and may eat kitten
@Christian-Ehrlicher
thanks, it helped a lot.
and i have a hunch{//main stack QString s = "hello world"; char* p = nullptr; {// toUtf8() QByteArray b = s.toUtf8(); {// data(); p = b.data(); qDebug() << p; // does this is same as "qDebug() << s.toUtf8().data();" } } // b is freed qDebug() << p; // this is same as "char * p = s.toUtf8().data(); qDebug() << p;" }
does this right?
-
@Christian-Ehrlicher said in can not print correctly after convert QString to char *:
int *myPtr = nullptr;
Never heard of
nullptr
in C ;-)NULL
was much nicer to read anyway.@JonB
nullptr is a c++11 key word, you can still use NULL, but NULL is defined as 0, sometimes it may cause some problem.such as:
void test(int *p) { qDebug() << "int *"; } void test(int i) { qDebug() << "int"; } test(NULL); test(nullptr);
output
int int *
-
@JonB
nullptr is a c++11 key word, you can still use NULL, but NULL is defined as 0, sometimes it may cause some problem.such as:
void test(int *p) { qDebug() << "int *"; } void test(int i) { qDebug() << "int"; } test(NULL); test(nullptr);
output
int int *
-
@Christian-Ehrlicher
thanks, it helped a lot.
and i have a hunch{//main stack QString s = "hello world"; char* p = nullptr; {// toUtf8() QByteArray b = s.toUtf8(); {// data(); p = b.data(); qDebug() << p; // does this is same as "qDebug() << s.toUtf8().data();" } } // b is freed qDebug() << p; // this is same as "char * p = s.toUtf8().data(); qDebug() << p;" }
does this right?
@Mozzie said in can not print correctly after convert QString to char *:
does this right?
Yes, exactly.
-
@Mozzie said in can not print correctly after convert QString to char *:
does this right?
Yes, exactly.
@Christian-Ehrlicher
thanks.