Qt Forum

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

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Unsolved Set but not used

    General and Desktop
    4
    8
    462
    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.
    • taku-s
      taku-s last edited by VRonin

      Hi, All

      In the following code, on line 8, the
      "Valiable 'rdata' set but not used", I get a warning.
      Then when I delete the comment out, the warning goes away.
      Why is this?

      Thank you.

       1	void MainWindow::on_pushButton_clicked()
       2	{
       3	    unsigned char data[4];
       4	        data[0] = 0xA6;
       5	        data[1] = 0x90;
       6	        data[2] = 0x21;
       7	        data[3] = 0x02;
       8	    unsigned char rdata[4];
       9	    unsigned long ret = 0x0;
      10
      11	    for(int i=0; i<4; i++){
      12	        ret = ret << 8;
      13	        ret = ret | data[i];
      14	    }
      15
      16	    qDebug() << QString::number(ret, 16);
      17
      18	    for(int i=0; i<4; i++){
      19	        rdata[i] = ret & 0xff;
      20	        ret = ret >> 8;
      21	    }
      22	
      23	//    for(int i=0; i<4; i++){
      24	//        qDebug() << QString::number(rdata[i], 16);
      25	//    }
      26	}
      
      JonB 1 Reply Last reply Reply Quote 0
      • JonB
        JonB @taku-s last edited by JonB

        @taku-s
        Get a warning from what? The compiler when compiling? From within Qt Creator where clang is being used to parse while you edit? Then it can be mistaken, which does happen. Your existing code does indeed use rdata. Unless the compiler is so clever as to notice that all you are doing is storing into a local array which you never then use/access, unless you have the qDebug() output line? Because if you think about your assigments into the rdata array don't actually achieve anything... The error message does look as though it has noticed this....!

        1 Reply Last reply Reply Quote 0
        • VRonin
          VRonin last edited by

          I'm not sure I can be more clear than your compiler but I'll try. That warning comes up if your variable only ever appears on the left hand side of an =. When you uncomment the last part it shows up as an argument to a function call QDebug::operator<< so your compiler is happy

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          JonB 1 Reply Last reply Reply Quote 4
          • JonB
            JonB @VRonin last edited by JonB

            @VRonin said in Set but not used:

            That warning comes up if your variable only ever appears on the left hand side of an =

            Over the years I am not aware of ever having noticed that a compiler gives this warning against array element assignments. MSVC. Is this user/warning gcc?

            artwaw 1 Reply Last reply Reply Quote 0
            • artwaw
              artwaw @JonB last edited by

              @JonB I use gcc/mingw, those warnings about var not being used (they apply also to unused method params) are here for quite some time already.

              For more information please re-read.

              Kind Regards,
              Artur

              JonB 1 Reply Last reply Reply Quote 0
              • JonB
                JonB @artwaw last edited by JonB

                @artwaw
                Hmm. Of course about unused individual variables/parameters, I can only say that in many years of MSVC I have never (don't think so anyway) seen one where it realizes that the whole array is never used if you only assign to its elements.... BTW I assume this can only happen because the array elements are a simple type, so it can guarantee no side-effects.

                @taku-s
                BTW: while you are developing the assignments on the array, but are not yet ready to write the code to read from it, you should be able to put in UNUSED(rdata). I do, but now I'm not sure where I get this macro from! I think it comes with gcc, but now I can't find a refence to it? Or do I actually use Qt's Q_UNUSED()? :) OK, use that one! (And ignore what the docs say about "the parameter with the specified name is not used in the body of a function", it applies equally to non-parameter local variables.

                1 Reply Last reply Reply Quote 0
                • taku-s
                  taku-s last edited by

                  Thank you all for your responses.
                  I wasn't sure I might have made some rudimentary mistakes myself, but I'm relieved.

                  I'm using both Linux + GCC,GDB and Windows 10 + MSVC2019 32bit.
                  The phenomenon I asked about is occurring on Linux + GCC. This one works fine, although I get a warning.
                  In fact, it's the Windows 10+MSVC one that I'm really struggling with.
                  In this case, while debugging, when I enter the for statement, starting on line 11, the imediate window that was normally displayed before disappears.
                  Is there any solution to this?

                  Best regards, please.

                  1 Reply Last reply Reply Quote 0
                  • VRonin
                    VRonin last edited by

                    C++ doesn't really do "immediate". The compiler might hack simple code to let you use it but as a general rule it's not possible to use C++ as an interpreted language

                    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                    ~Napoleon Bonaparte

                    On a crusade to banish setIndexWidget() from the holy land of Qt

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