-
Edit: This post is wrong. Disregard it.
@JKSH said in Convert QString pointer to char pointer ?:
const char *p2 = qstr.toUtf8().constData(); // The QByteArray gets destroyed after this line
Correct, but it's usually more subtle than this. The temporary is valid just as long as the immediate evaluation takes place. The typical shoot-yourself-in-the-foot usage is:
void someFunction(int a, char * x); ... QString z; someFunction(1, z.toUtf8().constData()); //< At the point the compiler pushes 1 to the stack the `z.toUtf8()` temporary had died already (assuming right-to-left argument evaluation like gcc does, I believe).
-
@kshegunov said in Convert QString pointer to char pointer ?:
someFunction(1, z.toUtf8().constData()); //< At the point the compiler pushes 1 to the stack the
z.toUtf8()temporary had died already (assuming right-to-left argument evaluation like gcc does, I believe).
I thought "they" said something/somewhere the temporaries "last until the end of the expression/statement they are in. Or similar.
You should back this claim up :) I was never keen on these "temporaries" being used in Qt (C++?), but if this is true they are "too dangerous" to dare using?!
For example, you can't use two of them, presumably:
someFunction(z.toUtf8().constData(), z2.toUtf8().constData());
?
-
@JonB said in Convert QString pointer to char pointer ?:
I thought "they" said something/somewhere the temporaries "last until the end of the expression/statement they are in.
"They" would be correct, in the sense that expression ends with evaluating the argument for the function. Or in the above example - before putting
1
as the next entry in the stack.You should back this claim up
Which claim? That gcc goes right to left? We tested that with @SGaist 2 years ago or thereabouts and we found that gcc and msvc does it the same, while clang goes left to right, if memory serves me. But in any case it's not that important here.
I was never keen on these "temporaries" being used in Qt (C++?), but if this is true they are "too dangerous" to dare using?!
Nothing to do with Qt, it's a C++ thing. And not if you keep track of what is what and don't return/dereference data from a temporary. Other typical and rather subtle cases of dangling references are also possible. Consider:
class B; class A { A(B & b) : ref(b) { } B & ref; };
When used like this:
A a(B(...)); //< Oops, I did it again
For example, you can't use two of them, presumably
Not unless you like pain and suffering.
-
@kshegunov
[With apologies to the OP now that this hijacking is getting much larger than intended...]There must be something wrong in your analysis, surely, because the way you describe it/I understand what you describe, I can barely see any case/statement/expression in which you could dare to use this temporary.
qDebug() << z.toUtf8().constData(); // ^ Nope: who knows what serializing *might* do, creating temps of its own? somefunc(z.toUtf8().constData()); // ^ Nope, again `somefunc()` could do anything before accessing the passed-in-temporary, messing it up?
I think I am misunderstanding something about which is the temporary and what the behaviour is :( Can you supply one of your proper C++ references for this, so I can look it up and torture my brain, please?
-
@JonB said in Convert QString pointer to char pointer ?:
There must be something wrong in your analysis, surely
Indeed, I'm wrong (about my first post); the last one (with keeping the reference is correct).
@JonB said in Convert QString pointer to char pointer ?:
Can you supply one of your proper C++ references for this, so I can look it up and torture my brain, please?
-
@kshegunov
I read and tried to understand that reference, and it was about as impenetrable as expected for C++.Are you still sticking by your:
someFunction(1, z.toUtf8().constData()); //< At the point the compiler pushes 1 to the stack the `z.toUtf8()` temporary had died already (assuming right-to-left argument evaluation like gcc does, I believe).
If so, where you do you see in the reference something mentioning/explaining that the apparent "full expression" scope before temporary object deletion applies in this case (among parameters), please?
-
@JonB said in Convert QString pointer to char pointer ?:
Are you still sticking by your
No. I did write that I was wrong, didn't I?
-
@kshegunov said in Lifetime of temporary objects:
Edit: This post is wrong. Disregard it.
Object existential crisis averted!
I just ignored the rest of the post and had to comment.
-
Hmmm...a bit of existential-niahlistist-objectivism