strange problem with const char *

A

alessio211734

I have problem with conversion from QString to const char * , I know that it's not the right newsgroup but I don't know if is a c++ issue problem.

void _file(const char * file)
{
std::string fileString(file);
int a=0;
}


int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QString str("ciao");
const char * xxx=str.toLatin1().constData();
_file(str.toLatin1().constData());
_file(xxx);
MainWindow w;
w.show();

return a.exec();
}

at the first call "_file(str.toLatin1().constData())" the debugger show that the parameter passed to function _file have the correct value while in the last call _file(xxx) the parameters passed to _file function is incorrect. I didn't understand why?
 
M

Marcel Müller

Am 18.12.2013 11:28, schrieb alessio211734:
I have problem with conversion from QString to const char * , I know that it's not the right newsgroup but I don't know if is a c++ issue problem.
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QString str("ciao");
const char * xxx=str.toLatin1().constData();
_file(str.toLatin1().constData());
_file(xxx);
MainWindow w;
w.show();

return a.exec();
}

at the first call "_file(str.toLatin1().constData())" the debugger show that the parameter passed to function _file have the correct value while in the last call _file(xxx) the parameters passed to _file function is incorrect. I didn't understand why?

Whatever QString is, read the documentation of QString. Especially the
point how long the storage retuned by constData() remains valid.

Most likely you have undefined behavior because of a dangling reference,
because you passed a temporary to xxx and access it after the QString
instance, that provides the storage has been destroyed.


Marcel
 
A

alessio211734

Il giorno mercoledì 18 dicembre 2013 13:02:45 UTC+1, Marcel Müller ha scritto:
Am 18.12.2013 11:28, schrieb alessio211734:
I have problem with conversion from QString to const char * , I know that it's not the right newsgroup but I don't know if is a c++ issue problem.


int main(int argc, char *argv[])

QApplication a(argc, argv);
QString str("ciao");
const char * xxx=str.toLatin1().constData();


MainWindow w;


return a.exec();


at the first call "_file(str.toLatin1().constData())" the debugger showthat the parameter passed to function _file have the correct value while in the last call _file(xxx) the parameters passed to _file function is incorrect. I didn't understand why?



Whatever QString is, read the documentation of QString. Especially the

point how long the storage retuned by constData() remains valid.



Most likely you have undefined behavior because of a dangling reference,

because you passed a temporary to xxx and access it after the QString

instance, that provides the storage has been destroyed.


I think problem it's not the function constData() but the function toLatin1() that return a temporary data QByteArray
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,058
Latest member
QQXCharlot

Latest Threads

Top