Qt double array with weird result

  • Hey guys!

    I was playing around with arrays and I created a double array:

    @class arraying : public QMainWindow

    explicit arraying(QWidget *parent = 0);

    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::arraying(QWidget *parent) :
    ui(new Ui::arraying)
    QString text;
    for (int i=0; i<=10; i++)
    text+="<font text size=2>";
    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

  • @
    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.

  • Ow that makes sense. So basically it doesn't clear the memory section it created.

    Thank you for the quick reply!

  • 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.

  • Lifetime Qt Champion


    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/

  • Hello,

    thank you for your fast response and suggestion.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.