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. Assign the same value to three variables, why do the three variables get different values?
QtWS25 Last Chance

Assign the same value to three variables, why do the three variables get different values?

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 5 Posters 790 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.
  • Poor EnglishP Offline
    Poor EnglishP Offline
    Poor English
    wrote on last edited by
    #1

    alt text

       QTcpSocket* socket1;
       QTcpSocket* socket2;
       QTcpSocket* socket3;
       QTcpSocket* socket4;
       socket4 = _server.nextPendingConnection();
       socket1 = socket2 = socket3 = socket4;
    

    First look at the simple code above(sorry I will not upload screenshots)
    I connected the server and client in debug mode,and observed the value of each person in the socket group as follows:

    • socket1::0X28D47C
    • socket2::0X28D480
    • socket3::OX28D484
    • socket4::OX28D488

    This situation is very confusing to me,according to common sense,because the four of them use the same variable assignment,the four of them should also be the same. But in fact,the four of them are different(in decreasing order),the only thing I know is that the most likely case is that the overloaded operator of socket is unusual,but can anyone help me explain Why do seniors do this??

    I am sorry about my poor English!

    KroMignonK JonBJ 2 Replies Last reply
    0
    • Poor EnglishP Poor English

      @KroMignon said in Assign the same value to three variables, why do the three variables get different values?:

      Are you sure your are reading the pointer content and not the pointer location?

      YES!
      In fact,at first I also thought it was an address,so I did various experiments,including interspersed declaration of various variables occupation stack space,until the most direct experimental method:I assigned socket1 etc,like【socket1 = (QTcpSocket *)12;】,and then through this sentence when debugging after the code,the value of socket1 changed directly from C to OX28D47C,so I made sure that debugging gave me the value,not the address value

      KroMignonK Offline
      KroMignonK Offline
      KroMignon
      wrote on last edited by KroMignon
      #5

      @Poor-English said in Assign the same value to three variables, why do the three variables get different values?:

      In fact,at first I also thought it was an address

      And I am sure you are looking at the wrong place, or you can throw away your C/C++ compiler which is unusable!

      QTcpSocket* socket1;
      QTcpSocket* socket2;
      QTcpSocket* socket3;
      QTcpSocket* socket4;
      socket4 = _server.nextPendingConnection();
      socket1 = socket2 = socket3 = socket4;
      
      qDebug() << "Pointer contents": << quint32(socket1)
               <<  "/" << quint32(socket2)
               <<  "/" << quint32(socket3)
               <<  "/" << quint32(socket4);
      

      Will returns the same value

      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

      Poor EnglishP 1 Reply Last reply
      3
      • Poor EnglishP Poor English

        alt text

           QTcpSocket* socket1;
           QTcpSocket* socket2;
           QTcpSocket* socket3;
           QTcpSocket* socket4;
           socket4 = _server.nextPendingConnection();
           socket1 = socket2 = socket3 = socket4;
        

        First look at the simple code above(sorry I will not upload screenshots)
        I connected the server and client in debug mode,and observed the value of each person in the socket group as follows:

        • socket1::0X28D47C
        • socket2::0X28D480
        • socket3::OX28D484
        • socket4::OX28D488

        This situation is very confusing to me,according to common sense,because the four of them use the same variable assignment,the four of them should also be the same. But in fact,the four of them are different(in decreasing order),the only thing I know is that the most likely case is that the overloaded operator of socket is unusual,but can anyone help me explain Why do seniors do this??

        KroMignonK Offline
        KroMignonK Offline
        KroMignon
        wrote on last edited by
        #2

        @Poor-English said in Assign the same value to three variables, why do the three variables get different values?:

        This situation is very confusing to me,according to common sense,because the four of them use the same variable assignment

        Are you sure your are reading the pointer content and not the pointer location?

        It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

        1 Reply Last reply
        5
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #3

          Hi
          Since there are 4 bytes between the values, i think you
          output the address of the variables
          and not the value they point to.
          as @KroMignon suggests :)

          Poor EnglishP 1 Reply Last reply
          5
          • Poor EnglishP Offline
            Poor EnglishP Offline
            Poor English
            wrote on last edited by
            #4

            @KroMignon said in Assign the same value to three variables, why do the three variables get different values?:

            Are you sure your are reading the pointer content and not the pointer location?

            YES!
            In fact,at first I also thought it was an address,so I did various experiments,including interspersed declaration of various variables occupation stack space,until the most direct experimental method:I assigned socket1 etc,like【socket1 = (QTcpSocket *)12;】,and then through this sentence when debugging after the code,the value of socket1 changed directly from C to OX28D47C,so I made sure that debugging gave me the value,not the address value

            I am sorry about my poor English!

            KroMignonK 1 Reply Last reply
            0
            • Poor EnglishP Poor English

              @KroMignon said in Assign the same value to three variables, why do the three variables get different values?:

              Are you sure your are reading the pointer content and not the pointer location?

              YES!
              In fact,at first I also thought it was an address,so I did various experiments,including interspersed declaration of various variables occupation stack space,until the most direct experimental method:I assigned socket1 etc,like【socket1 = (QTcpSocket *)12;】,and then through this sentence when debugging after the code,the value of socket1 changed directly from C to OX28D47C,so I made sure that debugging gave me the value,not the address value

              KroMignonK Offline
              KroMignonK Offline
              KroMignon
              wrote on last edited by KroMignon
              #5

              @Poor-English said in Assign the same value to three variables, why do the three variables get different values?:

              In fact,at first I also thought it was an address

              And I am sure you are looking at the wrong place, or you can throw away your C/C++ compiler which is unusable!

              QTcpSocket* socket1;
              QTcpSocket* socket2;
              QTcpSocket* socket3;
              QTcpSocket* socket4;
              socket4 = _server.nextPendingConnection();
              socket1 = socket2 = socket3 = socket4;
              
              qDebug() << "Pointer contents": << quint32(socket1)
                       <<  "/" << quint32(socket2)
                       <<  "/" << quint32(socket3)
                       <<  "/" << quint32(socket4);
              

              Will returns the same value

              It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

              Poor EnglishP 1 Reply Last reply
              3
              • Poor EnglishP Poor English

                alt text

                   QTcpSocket* socket1;
                   QTcpSocket* socket2;
                   QTcpSocket* socket3;
                   QTcpSocket* socket4;
                   socket4 = _server.nextPendingConnection();
                   socket1 = socket2 = socket3 = socket4;
                

                First look at the simple code above(sorry I will not upload screenshots)
                I connected the server and client in debug mode,and observed the value of each person in the socket group as follows:

                • socket1::0X28D47C
                • socket2::0X28D480
                • socket3::OX28D484
                • socket4::OX28D488

                This situation is very confusing to me,according to common sense,because the four of them use the same variable assignment,the four of them should also be the same. But in fact,the four of them are different(in decreasing order),the only thing I know is that the most likely case is that the overloaded operator of socket is unusual,but can anyone help me explain Why do seniors do this??

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

                @Poor-English said in Assign the same value to three variables, why do the three variables get different values?:

                and observed the value of each person in the socket group as follows:

                And just what did you do to "observe" these values?

                Poor EnglishP 1 Reply Last reply
                1
                • KroMignonK KroMignon

                  @Poor-English said in Assign the same value to three variables, why do the three variables get different values?:

                  In fact,at first I also thought it was an address

                  And I am sure you are looking at the wrong place, or you can throw away your C/C++ compiler which is unusable!

                  QTcpSocket* socket1;
                  QTcpSocket* socket2;
                  QTcpSocket* socket3;
                  QTcpSocket* socket4;
                  socket4 = _server.nextPendingConnection();
                  socket1 = socket2 = socket3 = socket4;
                  
                  qDebug() << "Pointer contents": << quint32(socket1)
                           <<  "/" << quint32(socket2)
                           <<  "/" << quint32(socket3)
                           <<  "/" << quint32(socket4);
                  

                  Will returns the same value

                  Poor EnglishP Offline
                  Poor EnglishP Offline
                  Poor English
                  wrote on last edited by
                  #7

                  @KroMignon
                  you are the right,thank you!!
                  I made a stupid mistake,it can also be said that I was fooled by the Qt debugger,In the Qt debugger,the display value and the display address value are used in the same place!
                  After you guidance,I retested my machine today and discovered my misunderstanding of Qt,So I write here to remind netizens not to make stupid mistakes like me!
                  That is:in the “value” column of the Qt debugger,this “value” is like Schrodinger's cat. it can be either the data value of A or the address value of A。Then,how to distinguish the current situation,is the content here the data value of A or the address value of A?The rule is follows(maybe only usuful for default settings):

                  1 When the content is presented in red font,the content represents the data value,when rendered in gray font,the content represents the address value

                  2 When the content is presented in decimal,the content represents the data value,when presented in hexadecimal,the content represents the address

                  3 When the content starts with “@0X”,the content represents the address value,otherwise,it is the data value

                  Thank you again!

                  I am sorry about my poor English!

                  1 Reply Last reply
                  0
                  • JonBJ JonB

                    @Poor-English said in Assign the same value to three variables, why do the three variables get different values?:

                    and observed the value of each person in the socket group as follows:

                    And just what did you do to "observe" these values?

                    Poor EnglishP Offline
                    Poor EnglishP Offline
                    Poor English
                    wrote on last edited by
                    #8

                    @JonB
                    The reason for this question is that I encountered a strange phenomenon yesterday:in my server class,there is an array of socket pointers【QTcpSocket* _ptrSocket[100];】,and then in the slot function inside,there is a local pointer socket,roughly in the form of the following:

                    ...()
                    {
                      QTcpSocket* socket = _server.nextPendingConnection();
                      for(int i=0; i<100; i++)
                      {
                        ...
                        if(NULL == _ptrSocket[i])
                        {
                          _ptrSocket[i] = socket;
                          connect(_ptrSocket[ i ], SIGNAL(readyRead()),
                                          this,SLOT(slotGetData()));
                         }
                         ...
                      }
                    }
                    

                    The above is the original form,but the program does not work correctly;then I replaced the 【_ptrSocket[ i ]】in the last sentence of the code with 【socket】,and the program can work normally;this phenomenon is very incredible to me;but now that I think about it ,it doesn't matter,ai za za di ba

                    I am sorry about my poor English!

                    JonBJ 1 Reply Last reply
                    0
                    • mrjjM mrjj

                      Hi
                      Since there are 4 bytes between the values, i think you
                      output the address of the variables
                      and not the value they point to.
                      as @KroMignon suggests :)

                      Poor EnglishP Offline
                      Poor EnglishP Offline
                      Poor English
                      wrote on last edited by
                      #9

                      @mrjj
                      yes,thank you!

                      I am sorry about my poor English!

                      1 Reply Last reply
                      0
                      • Poor EnglishP Poor English

                        @JonB
                        The reason for this question is that I encountered a strange phenomenon yesterday:in my server class,there is an array of socket pointers【QTcpSocket* _ptrSocket[100];】,and then in the slot function inside,there is a local pointer socket,roughly in the form of the following:

                        ...()
                        {
                          QTcpSocket* socket = _server.nextPendingConnection();
                          for(int i=0; i<100; i++)
                          {
                            ...
                            if(NULL == _ptrSocket[i])
                            {
                              _ptrSocket[i] = socket;
                              connect(_ptrSocket[ i ], SIGNAL(readyRead()),
                                              this,SLOT(slotGetData()));
                             }
                             ...
                          }
                        }
                        

                        The above is the original form,but the program does not work correctly;then I replaced the 【_ptrSocket[ i ]】in the last sentence of the code with 【socket】,and the program can work normally;this phenomenon is very incredible to me;but now that I think about it ,it doesn't matter,ai za za di ba

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by JonB
                        #10

                        @Poor-English said in Assign the same value to three variables, why do the three variables get different values?:

                        this phenomenon is very incredible to me;but now that I think about it ,it doesn't matter

                        There is indeed something very odd going on if you are claiming from this code that connect(_ptrSocket[ i ], ... behaves differently from connect(socket, .... Could you just confirm what the type of your _ptrSocket is? Show the declaration you are using, and since it is/should be an array show how we/you know the size/number of elements allocated for the array? EDIT Oh, I see you say QTcpSocket* _ptrSocket[100];, that should be OK. Then nobody is going to believe that _ptrSocket[ i ] is going to behave any differently from socket here! :) Maybe your statement "roughly in the form of the following" is not good enough!

                        Poor EnglishP 1 Reply Last reply
                        1
                        • Christian EhrlicherC Online
                          Christian EhrlicherC Online
                          Christian Ehrlicher
                          Lifetime Qt Champion
                          wrote on last edited by
                          #11

                          And apart from this - why using a plain C array here? It's nonsense and as you can see error-prone and hard to use. Use a proper container for the sockets.

                          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                          Visit the Qt Academy at https://academy.qt.io/catalog

                          Poor EnglishP 1 Reply Last reply
                          1
                          • JonBJ JonB

                            @Poor-English said in Assign the same value to three variables, why do the three variables get different values?:

                            this phenomenon is very incredible to me;but now that I think about it ,it doesn't matter

                            There is indeed something very odd going on if you are claiming from this code that connect(_ptrSocket[ i ], ... behaves differently from connect(socket, .... Could you just confirm what the type of your _ptrSocket is? Show the declaration you are using, and since it is/should be an array show how we/you know the size/number of elements allocated for the array? EDIT Oh, I see you say QTcpSocket* _ptrSocket[100];, that should be OK. Then nobody is going to believe that _ptrSocket[ i ] is going to behave any differently from socket here! :) Maybe your statement "roughly in the form of the following" is not good enough!

                            Poor EnglishP Offline
                            Poor EnglishP Offline
                            Poor English
                            wrote on last edited by
                            #12

                            @JonB
                            In fact,there is a stranger phenomenon,which I did not mention here:if I add a statement 【QTcpSocket* _socket;】in my header file(Yesterday, in order to see the difference between this _socket and the local variable socket),even if this 【_socket】is not used at all,it is just in the header file to declare,there will be errors:the program will crash immediately after running!

                            However,agter I deleted several redundant files in the project,these strange phenomena disappeared: the 【_socket】 declared in the header file can coexist peacefully with 【_ptrSocket[100]】,and the program is not will crash;or,the code 【connect(_ptrSocket[i],SIGNAL(readyRead()), this,SLOT(slotGetNetData()));】can also work normally,instead of client side sending data,the server side has no response!

                            In short,this may be related to my bad level coding ability,thank you for your advice!dear Mr.Cat!

                            I am sorry about my poor English!

                            1 Reply Last reply
                            0
                            • Christian EhrlicherC Christian Ehrlicher

                              And apart from this - why using a plain C array here? It's nonsense and as you can see error-prone and hard to use. Use a proper container for the sockets.

                              Poor EnglishP Offline
                              Poor EnglishP Offline
                              Poor English
                              wrote on last edited by
                              #13

                              @Christian-Ehrlicher
                              @Christian-Ehrlicher
                              Thank you for your suggestion. I moved from C,I am used to C thinking,I am now trying to adapt to C++ syntax and containers,thank you

                              I am sorry about my poor English!

                              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