[Qt-interest] [Qt interest] QByteArray conversion
Malyushytsky, Alex
alex at wai.com
Sat Jan 23 01:41:03 CET 2010
Why you don't debug your code yourself?
First of all nobody has file you are reading except you, and your code can't be tested as is cause it uses global variables.
As I am not able to debug it, all below is questionable,
but it seems you never fill your "signal" array (or whatever is it - no declaration is available).
You already reached the end of the file in the first "while" loop.
Code inside second "while" loop will never be executed.
Side note:
- using malloc in C++ code is evil, it seems allocated memory might never be freed either,
so each call to the PaintArea will allocate new memory and you will have the memory leak as result.
- Reading files is the slowest operation possible, consider removing second loop and use QVector to store your data, so you don't need to read it twice.
Regards,
Alex
From: qt-interest-bounces at trolltech.com [mailto:qt-interest-bounces at trolltech.com] On Behalf Of Gerardo Gutierrez
Sent: Friday, January 22, 2010 3:57 PM
To: qt-interest at trolltech.com
Subject: [Qt-interest] [Qt interest] QByteArray conversion
This piece of code intends to read a file with "double" data to plot, it doesn't matter wich index i put in signal to write on stdout it's zero, and obviously the plot is a nice line at zero.
PaintArea::PaintArea(QString ffile)
{
fileSize=0;
int i=0;
QFile file(ffile);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
while (!file.atEnd()) {
QByteArray line = file.readLine();
fileSize++;
}
signal=(double*)malloc(fileSize*sizeof(double));
while (!file.atEnd()) {
QByteArray line = file.readLine();
signal[i]=line.toDouble();
i++;
}
cout<<signal[35]<<endl;
}
--
"Solo existen 10 tipos de personas en el mundo...
las que saben binario y las que no"
_-`````-, ,- '- .
.' .- - | | - -. `.
/.' / `. \
:/ : _... ..._ `` :
:: : /._ .`:'_.._\. || :
:: `._ ./ ,` : \ . _.'' .
`:. / | -. \-. \\_ /
\:._ _/ .' .@) \@) ` `\ ,.'
_/,--' .- .\,-.`--`.
,'/'' (( \ ` )
/'/' \ `-' (
'/'' `._,-----'
''/' .,---'
''/' ;:
''/'' ''/
''/''/''
'/'/'
`;
---------------------------------------------------------------------------------------------------
Weidlinger Associates, Inc. made the following annotations.
"This message and any attachments are solely for the intended recipient and may contain confidential or privileged information. If you are not the intended recipient, any disclosure, copying, use, or distribution of the information included in this message and any attachments is prohibited. If you have received this communication in error, please notify us by reply e-mail and immediately and permanently delete this message and any attachments. Thank you."
"Please consider our environment before printing this email."
More information about the Qt-interest-old
mailing list