Qt double array with weird result
-
wrote on 8 Nov 2011, 13:11 last edited by
Hey guys!
I was playing around with arrays and I created a double array:
HEADER:
@class arraying : public QMainWindow
{
Q_OBJECTpublic:
explicit arraying(QWidget *parent = 0);
~arraying();public:
double array[10];};
@I then created a label which would write out the array number under each other. The expected result would be a bunch of "0"-s...
arraying.cpp
@arraying::arraying(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::arraying)
{
ui->setupUi(this);
QString text;
for (int i=0; i<=10; i++)
{
text+="<font text size=2>";
text+=QString::number(i);
text+=" = ";
text+=QString::number(array[i]);
text+="<br>";
text+="</font>";
}ui->label_array->setText(text);
}@
When I run the program I get a bizarre result...!http://db.tt/fXISEATQ(Result)!
What is the reason for this? Is there a way I can create a double array with empty elements? (other than with an if cycle).
Thank you ahead,
Gabor Nagy -
wrote on 8 Nov 2011, 13:27 last edited by
@
double array[10]
@only reserves space for ten doubles, it does not initialize the data! That means the memory still has the contents it had before (maybe some image data, or else - it's not predictable!) and thus the doubles contain more or less random data.
You will have to initialize the data yourself:
@
for(int i=0; i < 10; ++i)
array[i] = 0.0;
@A better solution would be to use one of Qt's container classes, like [[Doc:QVector]] or [[Doc:QList]]. Those initialize the contents with a default value.
bq. From "Qt Container classes":/doc/qt-4.7/containers.html description:
The documentation of certain container class functions refer to default-constructed values; for example, QVector automatically initializes its items with default-constructed values, and QMap::value() returns a default-constructed value if the specified key isn't in the map. For most value types, this simply means that a value is created using the default constructor (e.g. an empty string for QString). But for primitive types like int and double, as well as for pointer types, the C++ language doesn't specify any initialization; in those cases, Qt's containers automatically initialize the value to 0. -
wrote on 8 Nov 2011, 13:29 last edited by
Ow that makes sense. So basically it doesn't clear the memory section it created.
Thank you for the quick reply!
-
wrote on 13 Aug 2013, 15:30 last edited by
Hello,
i need a double 2dim array. The compiler say that i do not set ; , {} right, but for me it is not clear why.
I defined in header file @double Rm[8][3];
@and then into the cpp file
@Rm[8][3]={{1.15, 10.14, 99.93},
{1.16, 10.20, 100.21},
{1.15, 10.17, 100.28},
{1.16, 10.17, 100.05},
{1.15, 10.13, 99.91},
{1.19, 10.20, 99.88},
{1.1, 10.14, 10.16},
{1.15, 10.14, 99.86}};
@Some ideas? Thank you for your help.
-
Hi,
Please don't revive old (2 years) closed topics, create a new thread.
As for your problem, it's not Qt specific, you should rather ask on either the C++ guru subforum or on a C/C++ forum.
"Array initialization":http://www.cplusplus.com/doc/tutorial/arrays/
-
wrote on 13 Aug 2013, 20:54 last edited by
Hello,
thank you for your fast response and suggestion.