Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qt double array with weird result
Forum Updated to NodeBB v4.3 + New Features

Qt double array with weird result

Scheduled Pinned Locked Moved General and Desktop
6 Posts 4 Posters 6.4k Views 1 Watching
  • 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 Offline
    N Offline
    Nagamo
    wrote on 8 Nov 2011, 13:11 last edited by
    #1

    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
    0
    • G Offline
      G Offline
      goetz
      wrote on 8 Nov 2011, 13:27 last edited by
      #2

      @
      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
      0
      • N Offline
        N Offline
        Nagamo
        wrote on 8 Nov 2011, 13:29 last edited by
        #3

        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
        0
        • P Offline
          P Offline
          PROuC
          wrote on 13 Aug 2013, 15:30 last edited by
          #4

          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
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 13 Aug 2013, 19:19 last edited by
            #5

            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
            0
            • P Offline
              P Offline
              PROuC
              wrote on 13 Aug 2013, 20:54 last edited by
              #6

              Hello,

              thank you for your fast response and suggestion.

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved