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

Giancarlo Amati ilferraresebono at hotmail.it
Thu Mar 26 12:23:15 CET 2009


So at the end of the story it's only a matter of the statement line..!?

GC.

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

Yes, It is :)

The char array in first case only exists in the call line.

The second case stores the data in a QByteArray, if you do:
std::cout<<"data = " << testingString .toAscii().data() << std::endl;


will work

2009/3/26 Jesús Spí­nola <jspinola at gmail.com>


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


_______________________________________________

Qt-interest mailing list

Qt-interest at trolltech.com

http://lists.trolltech.com/mailman/listinfo/qt-interest




_________________________________________________________________
25 GB di spazio gratis online: attiva SkyDrive!
http://clk.atdmt.com/GBL/go/136430529/direct/01/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090326/346340a5/attachment.html 


More information about the Qt-interest-old mailing list