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. Set but not used
QtWS25 Last Chance

Set but not used

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 4 Posters 1.6k Views
  • 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-sT Offline
    taku-sT Offline
    taku-s
    wrote on last edited by VRonin
    #1

    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	}
    
    JonBJ 1 Reply Last reply
    0
    • taku-sT taku-s

      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	}
      
      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by JonB
      #2

      @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
      0
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #3

        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

        JonBJ 1 Reply Last reply
        4
        • VRoninV VRonin

          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

          JonBJ Online
          JonBJ Online
          JonB
          wrote on last edited by JonB
          #4

          @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?

          artwawA 1 Reply Last reply
          0
          • JonBJ 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?

            artwawA Offline
            artwawA Offline
            artwaw
            wrote on last edited by
            #5

            @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

            JonBJ 1 Reply Last reply
            0
            • artwawA artwaw

              @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.

              JonBJ Online
              JonBJ Online
              JonB
              wrote on last edited by JonB
              #6

              @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
              0
              • taku-sT Offline
                taku-sT Offline
                taku-s
                wrote on last edited by
                #7

                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
                0
                • VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by
                  #8

                  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
                  1

                  • Login

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