[Qt-interest] [Newbie question] Why string.toAscii().data() doesn't work?

Giancarlo Amati ilferraresebono at hotmail.it
Thu Mar 26 12:17:29 CET 2009


I actually had the same experience...but somehow dunnow still why, If I use string.toAscii().data() and then i use a function which takes a Char * as one argument, it works ....although that arguments contains rubbish.

GC.

Date: Thu, 26 Mar 2009 12:10:59 +0100
From: jspinola at gmail.com
To: qt-interest at trolltech.com
Subject: [Qt-interest] [Newbie question] Why string.toAscii().data() doesn't	work?

I'll like to know why this code doesn't work:



QString testingString = "testing";
char *data = testingString .toAscii().data(); 
qDebug() << "data = " << data;
std::cout<<"data = " << data << std::endl;




It prints garbage on data. But if I do this way:
QString testingString = "testing";
QByteArray temp = testingString.toAscii(); 
char *data = temp.data(); 


qDebug() << "data = " << data;
std::cout<<"data = " << data << std::endl;

It prints "testing" correctly.



My explanation is that on the first version the pointer returned by data() points to a temporally object "alive" only on the same line, so, when we reach the next lines the memory of this pointer is invalid.



Is this correct? It doesn't feel natural to me coming from a C# world :P

-- 
Jesús Spínola

_________________________________________________________________
Portati avanti: scarica Messenger 2009!
http://clk.atdmt.com/GBL/go/136430507/direct/01/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090326/35a5c757/attachment.html 


More information about the Qt-interest-old mailing list