Click here to Skip to main content
15,885,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello!
I'm working with this code:
C++
int counter = 0;
int dest_pos = 20;

for (int z = 0; z < 1000; z++)
{
/***2***/

   if(counter++ == dest_pos)
   {
   /***1***/
   //if reach dest position - than would set new value for dest_pos
   dest_pos = 56;
   
   }
}


At the line (marked as /***1***/) I tried to read z-value:
C++
Memo1->Lines->Add(IntToStr(z));

And I've got 0;

But if I try to assign value of z to another variable at str line marked as /***2***/, something like this:
C++
int y = z;

than I've got right z value at the line marked as /***1***/
Why is this happening?
And in what cases the value of z is outputed as 0 inside "for" loop?
Posted
Comments
CPallini 31-Jul-14 15:23pm    
It smells like a bug of IntToStr function.
Sergey Alexandrovich Kryukov 31-Jul-14 15:43pm    
I think this is not just a smell, but can be proven — please see my answer.
Your guess is appreciated and credited.
—SA

1 solution

CPallini is right: this is a bug in IntToStr. I can tell you more: it is certainly the bug there. Here is why: there is no a way for z to get any wrong value, because its values are fully determined by the line with for statement head. (Well, people faced some extreme perverts who tried to modify the loop variable, and a C++ compiler — what a shame! — can permit that, but there is no a sign of such a bad thing in your code sample.)

At the same time, we cannot see the definition of IntToStr in your code sample. Therefore, the problem is in that function. ∎

:-)

—SA
 
Share this answer
 
v2
Comments
CPallini 1-Aug-14 3:21am    
:)5.
Sergey Alexandrovich Kryukov 1-Aug-14 11:44am    
Thank you, Carlo.
—SA
mbue 1-Aug-14 17:22pm    
sorry to intercept. but nothing is proven!
what do you think the function IntToStr should look alike?


#pragma once
#include <stdio.h>
#include <tchar.h>
#include <malloc.h>

static TCHAR __intBuff[64];
#define IntToStr1(i) (const TCHAR*)__IntToStr1(i,__intBuff,sizeof(__intBuff)/sizeof(__intBuff[0]))
#define IntToStr2(i) (const TCHAR*)__IntToStr2(i,__intBuff,sizeof(__intBuff)/sizeof(__intBuff[0]))

const TCHAR* __IntToStr1(int& i,TCHAR* buff,const unsigned int blen)
{
_itot_s(i,buff,blen,10);
i = 0;// <--- parameter 'i' may be lost or nulled by malfunction
return buff;
}

const TCHAR* __IntToStr2(int& i,TCHAR* buff,const unsigned int blen)
{
i = 0;// <--- parameter 'i' may be lost or nulled by malfunction
_itot_s(i,buff,blen,10);
return buff;
}

int _tmain(int argc, _TCHAR* argv[])
{
int counter = 0;
int dest_pos = 20;

_tprintf(__T("loop IntToStr1\r\n"));
for (int z = 0; z < 1000; z++)
{
/***2***/
int y = z;
/***3***/

if(counter++ == dest_pos)
{
/***1***/
_tprintf(__T("y = %s (OP says here comes z unchanged)\r\n"),IntToStr1(y));
_tprintf(__T("z = %s (OP says here comes 0)\r\n"),IntToStr1(z));
//if reach dest position - than would set new value for dest_pos
dest_pos = 56;

}
}

_tprintf(__T("loop IntToStr2\r\n"));

counter = 0;
dest_pos = 20;
for (int z = 0; z < 1000; z++)
{
/***2***/
int y = z;
/***3***/

if(counter++ == dest_pos)
{
/***1***/
_tprintf(__T("y = %s (OP says here comes z unchanged)\r\n"),IntToStr2(y));
_tprintf(__T("z = %s (OP says here comes 0)\r\n"),IntToStr2(z));
//if reach dest position - than would set new value for dest_pos
dest_pos = 56;

}
}
_tprintf(__T("<key> ")); _gettch();
return 0;
}





the result:

loop IntToStr1
y = 20 (OP says here comes z unchanged)
z = 20 (OP says here comes 0)
y = 36 (OP says here comes z unchanged)
z = 36 (OP says here comes 0)
loop IntToStr2
y = 0 (OP says here comes z unchanged)
z = 0 (OP says here comes 0)
y = 0 (OP says here comes z unchanged)
z = 0 (OP says here comes 0)


conclusion:
the only place the value of z can be changed to 0 is between '/***3***/' and '/***1***/'.
how is this possible with the code we've seen?

regards.
Sergey Alexandrovich Kryukov 1-Aug-14 20:57pm    
This is irrelevant. The proof is based on the fact that there cannot be other source of this problem. (I don't consider such cases when one use one code but shows different code in the issue report; it happens all the time.) Please use the debugger and you will see what exactly is going on. If you prove me wrong I'll be much grateful.
Good luck.
—SA
mbue 2-Aug-14 5:46am    
Yes indeed your statement IntToStr is the reason is irrelevant, cause its impossible. There must be other code the OP did not posted here. Use your debugger and see that any kind of that function cannot change parameters in a different way.
regards

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900