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. Weird warning message, it makes no sense
Forum Updated to NodeBB v4.3 + New Features

Weird warning message, it makes no sense

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 7 Posters 1.3k Views 2 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.
  • GuerrianG Offline
    GuerrianG Offline
    Guerrian
    wrote on last edited by Guerrian
    #1

    I get a weird warning message in Qt creator:

    /home/daniel/Documents/Qt/MMKPHV/mmkphv.cpp:730: warning: 'tr[9]' may be used uninitialized in this function [-Wmaybe-uninitialized]
    tr[k] += res[i][j][k] - res[i][sj][k];
    ^

    void MMKPH::rndLcl(int seed, int its, int lcl)
    {
        int i, j, k, l, v, q, sj, r[MAX_DIMS], tr[MAX_DIMS], ti[MAX_LCLS], tj[MAX_LCLS];
        bool flg;
        srand(seed);
        for (k = 0; k < dims; k++)
        {
            r[k] = 0;
            for (i = 0; i < cla; i++)
                r[k] += res[i][sol[i]][k];
        }
        for (l = 0; l < its; l++)
        {
            v = 0;
            for (k = 0; k < dims; k++)
                tr[k] = 0; // <- it's initialised here!
            for (q = 0; q < lcl; q++)
            {
                i = rndU(0, cla - 1);
                j = rndU(0, items[i] - 1);
                ti[q] = i;
                tj[q] = j;
                sj = sol[i];
                v += val[i][j] - val[i][sj];
                for (k = 0; k < dims; k++)
                    tr[k] += res[i][j][k] - res[i][sj][k]; // <- this is the line of the warning
            }
            if (v > 0)
            {
                ...
            }
        }
    }
    

    Linux Mint 18.3
    Qt 5.14.1
    Qt Creator 4.11.1

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

      Hi,

      What is dims ?
      What is its value ?
      Where does it come from ?

      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
      3
      • GuerrianG Guerrian

        I get a weird warning message in Qt creator:

        /home/daniel/Documents/Qt/MMKPHV/mmkphv.cpp:730: warning: 'tr[9]' may be used uninitialized in this function [-Wmaybe-uninitialized]
        tr[k] += res[i][j][k] - res[i][sj][k];
        ^

        void MMKPH::rndLcl(int seed, int its, int lcl)
        {
            int i, j, k, l, v, q, sj, r[MAX_DIMS], tr[MAX_DIMS], ti[MAX_LCLS], tj[MAX_LCLS];
            bool flg;
            srand(seed);
            for (k = 0; k < dims; k++)
            {
                r[k] = 0;
                for (i = 0; i < cla; i++)
                    r[k] += res[i][sol[i]][k];
            }
            for (l = 0; l < its; l++)
            {
                v = 0;
                for (k = 0; k < dims; k++)
                    tr[k] = 0; // <- it's initialised here!
                for (q = 0; q < lcl; q++)
                {
                    i = rndU(0, cla - 1);
                    j = rndU(0, items[i] - 1);
                    ti[q] = i;
                    tj[q] = j;
                    sj = sol[i];
                    v += val[i][j] - val[i][sj];
                    for (k = 0; k < dims; k++)
                        tr[k] += res[i][j][k] - res[i][sj][k]; // <- this is the line of the warning
                }
                if (v > 0)
                {
                    ...
                }
            }
        }
        
        aha_1980A Offline
        aha_1980A Offline
        aha_1980
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @Guerrian

        In addition to what @SGaist said, if

        • dims is a global variable and
        • dims is not const

        then it might be possible that dims is changed (e.g. by another thread) while your function is running.

        To be save, I'd init tr completely to zero (e.g. with memset, or by looping up to MAX_DIMS).

        And to go a step further, it would not hurt to init all other variables too.

        Regards

        Qt has to stay free or it will die.

        1 Reply Last reply
        3
        • fcarneyF Offline
          fcarneyF Offline
          fcarney
          wrote on last edited by
          #4

          @Guerrian said in Weird warning message, it makes no sense:

          r[MAX_DIMS], tr[MAX_DIMS], ti[MAX_LCLS], tj[MAX_LCLS];

          You might consider using vectors. They can make your life easier:

              std::vector<int> vect1(10); // 10 elements with default initializer
              std::vector<int> vect2(10,1); // 10 elements inited to 1
              // easy looping
              for(auto& v: vect1){
                  qInfo() << v;
              }
              for(auto& v: vect2){
                  qInfo() << v;
              }
          

          I don't know your requirements though. So YMMV.

          C++ is a perfectly valid school of magic.

          1 Reply Last reply
          3
          • GuerrianG Offline
            GuerrianG Offline
            Guerrian
            wrote on last edited by Guerrian
            #5

            dims, res, cap, v_sol and sol are just class members, they are set elsewhere in the program. It kind of makes the point.

            Linux Mint 18.3
            Qt 5.14.1
            Qt Creator 4.11.1

            jsulmJ 1 Reply Last reply
            0
            • GuerrianG Guerrian

              dims, res, cap, v_sol and sol are just class members, they are set elsewhere in the program. It kind of makes the point.

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Guerrian You did not answer second question from @SGaist
              Did you debug to see whether really all elements of tr were initialised?

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              GuerrianG 1 Reply Last reply
              1
              • jsulmJ jsulm

                @Guerrian You did not answer second question from @SGaist
                Did you debug to see whether really all elements of tr were initialised?

                GuerrianG Offline
                GuerrianG Offline
                Guerrian
                wrote on last edited by Guerrian
                #7

                @jsulm dims is read in from a file it's usually 5 or 10 with:
                #define MAX_DIMS 10

                Why wouldn't all elements be initialised? The init loop will always execute and dims is not changed, there is no multithreading.

                Linux Mint 18.3
                Qt 5.14.1
                Qt Creator 4.11.1

                jsulmJ 1 Reply Last reply
                0
                • GuerrianG Guerrian

                  @jsulm dims is read in from a file it's usually 5 or 10 with:
                  #define MAX_DIMS 10

                  Why wouldn't all elements be initialised? The init loop will always execute and dims is not changed, there is no multithreading.

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @Guerrian said in Weird warning message, it makes no sense:

                  Why wouldn't all elements be initialised, the init loop will always execute?

                  Did you debug your code to see what happens? This is fastest way to find out what is happening.

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  GuerrianG 1 Reply Last reply
                  0
                  • jsulmJ jsulm

                    @Guerrian said in Weird warning message, it makes no sense:

                    Why wouldn't all elements be initialised, the init loop will always execute?

                    Did you debug your code to see what happens? This is fastest way to find out what is happening.

                    GuerrianG Offline
                    GuerrianG Offline
                    Guerrian
                    wrote on last edited by
                    #9

                    @jsulm I don't need to debug it because it is working fine, the results are good and look correct.

                    Linux Mint 18.3
                    Qt 5.14.1
                    Qt Creator 4.11.1

                    J.HilkJ 1 Reply Last reply
                    0
                    • GuerrianG Guerrian

                      @jsulm I don't need to debug it because it is working fine, the results are good and look correct.

                      J.HilkJ Offline
                      J.HilkJ Offline
                      J.Hilk
                      Moderators
                      wrote on last edited by
                      #10

                      @Guerrian
                      I don't quite get your problem, dims is a non constant member variable, that may or may not be changed between the 2 for loops.

                      the code model can't tell and is therefore warning you. If you're sure that everything will always be fine, ignore it.

                      if you want to get rid of the warning. shadow it locally.

                      void MMKPH::rndLcl(int seed, int its, int lcl)
                      {
                          const int dims = this->dims;
                          .....
                      

                      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                      Q: What's that?
                      A: It's blue light.
                      Q: What does it do?
                      A: It turns blue.

                      GuerrianG 1 Reply Last reply
                      2
                      • J.HilkJ J.Hilk

                        @Guerrian
                        I don't quite get your problem, dims is a non constant member variable, that may or may not be changed between the 2 for loops.

                        the code model can't tell and is therefore warning you. If you're sure that everything will always be fine, ignore it.

                        if you want to get rid of the warning. shadow it locally.

                        void MMKPH::rndLcl(int seed, int its, int lcl)
                        {
                            const int dims = this->dims;
                            .....
                        
                        GuerrianG Offline
                        GuerrianG Offline
                        Guerrian
                        wrote on last edited by
                        #11

                        @J.Hilk said in Weird warning message, it makes no sense:

                        const int dims = this->dims;

                        Your shadow trick didn't work. I added this line to the top of the function and I still get the warning:

                        const int dims = this->dims;
                        

                        Linux Mint 18.3
                        Qt 5.14.1
                        Qt Creator 4.11.1

                        J.HilkJ 1 Reply Last reply
                        0
                        • GuerrianG Guerrian

                          @J.Hilk said in Weird warning message, it makes no sense:

                          const int dims = this->dims;

                          Your shadow trick didn't work. I added this line to the top of the function and I still get the warning:

                          const int dims = this->dims;
                          
                          J.HilkJ Offline
                          J.HilkJ Offline
                          J.Hilk
                          Moderators
                          wrote on last edited by
                          #12

                          @Guerrian is

                          dims == MAX_DIMS?
                          

                          otherwise you will have uninitialized entries in your array


                          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                          Q: What's that?
                          A: It's blue light.
                          Q: What does it do?
                          A: It turns blue.

                          GuerrianG 1 Reply Last reply
                          1
                          • J.HilkJ J.Hilk

                            @Guerrian is

                            dims == MAX_DIMS?
                            

                            otherwise you will have uninitialized entries in your array

                            GuerrianG Offline
                            GuerrianG Offline
                            Guerrian
                            wrote on last edited by Guerrian
                            #13

                            @J.Hilk Got it, finally it makes sense. So I just wrote it like this to only declare what's required:

                            int i, j, k, l, v, q, sj, r[dims], tr[dims], ti[lcl], tj[lcl];
                            

                            Linux Mint 18.3
                            Qt 5.14.1
                            Qt Creator 4.11.1

                            Pablo J. RoginaP 1 Reply Last reply
                            1
                            • GuerrianG Guerrian

                              @J.Hilk Got it, finally it makes sense. So I just wrote it like this to only declare what's required:

                              int i, j, k, l, v, q, sj, r[dims], tr[dims], ti[lcl], tj[lcl];
                              
                              Pablo J. RoginaP Offline
                              Pablo J. RoginaP Offline
                              Pablo J. Rogina
                              wrote on last edited by
                              #14

                              @Guerrian if your issue is solved, please don't forget to mark your post as such. Thanks.

                              Upvote the answer(s) that helped you solve the issue
                              Use "Topic Tools" button to mark your post as Solved
                              Add screenshots via postimage.org
                              Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                              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