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. [SOLVED}Strange behaviour of initialization in constructor...
Forum Updated to NodeBB v4.3 + New Features

[SOLVED}Strange behaviour of initialization in constructor...

Scheduled Pinned Locked Moved General and Desktop
15 Posts 3 Posters 3.3k 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.
  • D Offline
    D Offline
    DiIvPa
    wrote on last edited by
    #1

    Dear Forumers, I tryed to initialize some arrays of my Widjet class and come up with unexpected result. Fortunately I could remove all another parts of my codes and get the same. Where am I wrong (please, take a look in codes and output below):
    //main.cpp
    @#include "myWidget.h"

    int main(int argc, char **argv){
    QApplication app(argc, argv);
    myWidget *my_widget = new myWidget;

    my_widget -> show();

    return app.exec();
    }
    @
    //myWidget.h
    @#ifndef MYWIDGET_H
    #define MYWIDGET_H

    #include <QApplication>
    #include <QMainWindow>

    #include <QDebug>

    class myWidget : public QMainWindow
    {
    Q_OBJECT

    public:
    

    myWidget( QWidget *parent = 0 );
    float Modl_V_set[2][18][8];
    float Modl_I_set[2][18][8];
    QString Modl_S_set[2][18][9];

    };

    #endif // MYWIDGET_H
    @
    //myWidget.cpp
    @#include "myWidget.h"

    ///////////////////////////////////////////////////////////////////////////////
    myWidget::myWidget(QWidget *parent) : QMainWindow(parent)
    {
    // qDebug() << "constructor: start";
    // qRegisterMetaType<QItemSelection>();
    for ( int n_can = 0; n_can < 2; n_can++ ) {
    for ( int n_modl = 0; n_modl < 18; n_modl++ ) {

      for ( int n_row = 0; n_row < 8; n_row++ ) {
    

    Modl_V_set[n_can][n_modl][n_row] = 0;
    Modl_I_set[n_can][n_modl][n_row] = 0;
    Modl_S_set[n_can][n_modl][n_row] = "OFF";
    }
    Modl_S_set[n_can][n_modl][8] == "ONN";
    qDebug() << "Modl_S_set[" << n_can << " ][ "
    << n_modl << "][8] == "
    << Modl_S_set[ n_can ][ n_modl ][8];
    qDebug() << "Modl_S_set[" << n_can << " ][ "
    << n_modl << "][7] == "
    << Modl_S_set[ n_can ][ n_modl ][7];

    }
    

    }
    }
    @
    OUTPUT:
    @slowcontrol@FODS-66-019:24:11> ./test_QString 14-04-28
    Modl_S_set[ 0 ][ 0 ][8] == ""
    Modl_S_set[ 0 ][ 0 ][7] == "OFF"
    Modl_S_set[ 0 ][ 1 ][8] == ""
    Modl_S_set[ 0 ][ 1 ][7] == "OFF"
    Modl_S_set[ 0 ][ 2 ][8] == ""
    Modl_S_set[ 0 ][ 2 ][7] == "OFF"
    Modl_S_set[ 0 ][ 3 ][8] == ""
    Modl_S_set[ 0 ][ 3 ][7] == "OFF"
    Modl_S_set[ 0 ][ 4 ][8] == ""
    Modl_S_set[ 0 ][ 4 ][7] == "OFF"
    Modl_S_set[ 0 ][ 5 ][8] == ""
    Modl_S_set[ 0 ][ 5 ][7] == "OFF"
    Modl_S_set[ 0 ][ 6 ][8] == ""
    Modl_S_set[ 0 ][ 6 ][7] == "OFF"
    Modl_S_set[ 0 ][ 7 ][8] == ""
    Modl_S_set[ 0 ][ 7 ][7] == "OFF"
    Modl_S_set[ 0 ][ 8 ][8] == ""
    Modl_S_set[ 0 ][ 8 ][7] == "OFF"
    Modl_S_set[ 0 ][ 9 ][8] == ""
    Modl_S_set[ 0 ][ 9 ][7] == "OFF"
    and so on...

    slowcontrol@FODS-66-019:24:15>
    @
    Thank you for help in advance.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      MuldeR
      wrote on last edited by
      #2

      What output would you have expected ???

      @for ( int n_can = 0; n_can < 2; n_can++ ) {
      for ( int n_modl = 0; n_modl < 18; n_modl++ ) {
      for ( int n_row = 0; n_row < 8; n_row++ ) {
      Modl_V_set[n_can][n_modl][n_row] = 0;
      Modl_I_set[n_can][n_modl][n_row] = 0;
      Modl_S_set[n_can][n_modl][n_row] = "OFF";
      }
      Modl_S_set[n_can][n_modl][8] == "ONN";
      qDebug() << "Modl_S_set[" << n_can << " ][ "
      << n_modl << "][8] == "
      << Modl_S_set[ n_can ][ n_modl ][8];
      qDebug() << "Modl_S_set[" << n_can << " ][ "
      << n_modl << "][7] == "
      << Modl_S_set[ n_can ][ n_modl ][7];
      }
      }@

      My OpenSource software at: http://muldersoft.com/

      Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

      Go visit the coop: http://youtu.be/Jay...

      1 Reply Last reply
      0
      • D Offline
        D Offline
        DiIvPa
        wrote on last edited by
        #3

        I expected, for instance, @Modl_S_set[ 1 ][ 17 ][8] == "ONN" @

        1 Reply Last reply
        0
        • M Offline
          M Offline
          MuldeR
          wrote on last edited by
          #4

          Update:

          I think this is an out-of-bounds access!

          @Modl_S_set[n_can][n_modl][8] == "ONN";@

          Highest index of an array of size 8 is 7, not 8.

          My OpenSource software at: http://muldersoft.com/

          Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

          Go visit the coop: http://youtu.be/Jay...

          1 Reply Last reply
          0
          • D Offline
            D Offline
            DiIvPa
            wrote on last edited by
            #5

            Why ? Take a look at declaration:
            @ QString Modl_S_set[2][18][9];@

            1 Reply Last reply
            0
            • M Offline
              M Offline
              MuldeR
              wrote on last edited by
              #6

              [quote author="DiIvPa" date="1398699896"]Why ? Take a look at declaration:
              @ QString Modl_S_set[2][18][9];@[/quote]

              Oh, I overlooked that the one array is size 9, not 8.

              So that's not the problem...

              My OpenSource software at: http://muldersoft.com/

              Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

              Go visit the coop: http://youtu.be/Jay...

              1 Reply Last reply
              0
              • M Offline
                M Offline
                MuldeR
                wrote on last edited by
                #7

                Update 2:

                Replace:
                @Modl_S_set[n_can][n_modl][8] == "ONN";@

                With:
                @Modl_S_set[n_can][n_modl][8] = "ONN";@

                ;-)

                My OpenSource software at: http://muldersoft.com/

                Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

                Go visit the coop: http://youtu.be/Jay...

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  DiIvPa
                  wrote on last edited by
                  #8

                  That's pity but I still need help....

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    MuldeR
                    wrote on last edited by
                    #9

                    [quote author="DiIvPa" date="1398700334"]That's pity but I still need help....[/quote]

                    To make it clear again:

                    You must use assignment operator instead of equality operator!

                    My OpenSource software at: http://muldersoft.com/

                    Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

                    Go visit the coop: http://youtu.be/Jay...

                    1 Reply Last reply
                    0
                    • D Offline
                      D Offline
                      DiIvPa
                      wrote on last edited by
                      #10

                      Thank you very much. I lost a couple hours to figure out this. I appreciate your help. (Fresh eye is quick sometimes..)

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        MuldeR
                        wrote on last edited by
                        #11

                        [quote author="DiIvPa" date="1398700558"]I lost a couple hours to figure out this.[/quote]

                        I've been trough this many times :-)

                        My OpenSource software at: http://muldersoft.com/

                        Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

                        Go visit the coop: http://youtu.be/Jay...

                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          Hi,

                          On a side note, you have a memory leak in your main, you don't delete my_widget before ending the main function.

                          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
                          • D Offline
                            D Offline
                            DiIvPa
                            wrote on last edited by
                            #13

                            Hi, SGaist, thank you for a side note. It would be nice if you explain me a little more detailed what do you mean. I thought that in main I haven't do anything exept issueing
                            @my_widget -> show();@

                            In myWidget class I keep an eye on it and make appropriate deletes. What have I to do in main ? Do I need to make

                            @delete my_widget;@

                            after issueing

                            @my_widget -> show();@

                            I tryed and found that the window disappeared from screen as I expected. Maybe you mean something else ?

                            1 Reply Last reply
                            0
                            • SGaistS Offline
                              SGaistS Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              Let's use your application as the example:

                              Following your technique:
                              @int main(int argc, char **argv){
                              QApplication app(argc, argv);
                              myWidget *my_widget = new myWidget; << heap allocation

                              my_widget -> show();

                              int ret = app.exec();
                              delete my_widget; << delete once it's done
                              return ret;
                              }@

                              The Qt way:

                              @int main(int argc, char **argv){
                              QApplication app(argc, argv);
                              myWidget my_widget; << stack allocation
                              my_widget.show();

                              return app.exec();
                              }@

                              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
                              • D Offline
                                D Offline
                                DiIvPa
                                wrote on last edited by
                                #15

                                Hi, SGaist, thank you for explanation. I did as you recomended.

                                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