Solved Assign the same value to three variables, why do the three variables get different values?
-
@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 -
@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-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?
-
@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!
-
@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
-
@mrjj
yes,thank you! -
@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 fromconnect(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 sayQTcpSocket* _ptrSocket[100];
, that should be OK. Then nobody is going to believe that_ptrSocket[ i ]
is going to behave any differently fromsocket
here! :) Maybe your statement "roughly in the form of the following" is not good enough! -
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.
-
@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!
-
@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