Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Qt double array with weird result

    General and Desktop
    4
    6
    5786
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • N
      Nagamo last edited by

      Hey guys!

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

      HEADER:
      @class arraying : public QMainWindow
      {
      Q_OBJECT

      public:
      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

      1 Reply Last reply Reply Quote 0
      • G
        goetz 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.

        http://www.catb.org/~esr/faqs/smart-questions.html

        1 Reply Last reply Reply Quote 0
        • N
          Nagamo last edited by

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

          Thank you for the quick reply!

          1 Reply Last reply Reply Quote 0
          • P
            PROuC 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.

            1 Reply Last reply Reply Quote 0
            • SGaist
              SGaist Lifetime Qt Champion last edited by

              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/

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply Reply Quote 0
              • P
                PROuC last edited by

                Hello,

                thank you for your fast response and suggestion.

                1 Reply Last reply Reply Quote 0
                • First post
                  Last post