-1

I have listed 2 codes below

CODE 1:

int  main()
{       
int z=0 ,a[100];    
for(int i=10;i<=30;i++)
{
if(palindrome(i))
a[++z]=i;
}    
cout<<"value of z = "<<z<<endl;    
}

CODE 2:

int  main()
{
int z=0 ,a[100];

for(int i=10;i<=30;i++)
{
if(palindrome(i))
a[++z]=i;
cout<<z<<endl;   //  JUST ADDED THIS EXTRA LINE
}

cout<<"value of z = "<<z<<endl;
}

#Note:

the function palindrome return 0 if the number given is not a palindrome

function palindrome is as follows

int palindrome (int n)
{
    int rev;
    int n1=n;
    while(n!=0)
    {
        int t=n%10;
        rev = (rev*10) + t;
        n/=10;
    }
    
    if(rev!=n1)
    return 0;   
}

OUTPUT FOR CODE 1: value of z = 0

OUTPUT FOR CODE 2:

0
1
1
1
1
1
1
1
1
1
1
1
2
2
2
2
2
2
2
2
2
value of z = 2

can anyone explain why addition of cout<<z<<endl; inside the loop creating such a drastic change in value of final z ?

EDIT:

through comments for this question i realized that initializing rev=0 in my palindrome function would sort the issue. But Can anyone tell me why adding cout<<z<<endl; inside the loop made a change in output ?

  • 1/Your `rev` variable in the `palindrome` function is uninitialised. 2/ At the end of the `palindrome` function, if `rev == n1` the return value is not defined. This does not even compile for me with clang. – op414 Jun 11 '21 at 14:56
  • @op414 at the beginning of palindrome function i have initialized the value `rev` as `int rev;` –  Jun 11 '21 at 14:58
  • 1
    `int rev;` _declares_ `rev`, it does not _initalize_ it. To initialise to 0, write `int rev{};` – op414 Jun 11 '21 at 14:59
  • @op414 yeah doing `int rev=0;` made the change .......thanks a lot ......but can you explain me how ???? –  Jun 11 '21 at 15:03
  • @op414 please let me knw how a small initialization to `0` made a change ????? that too in a function ... –  Jun 11 '21 at 15:06
  • C++ does not initialise variables automatically. When you write, `int rev;`, `rev` could be anything: it could be `0`, `1212`, or any other value. So when your program reaches `rev = (rev*10) + t;` your new `rev` value can be anything as well. This is of course going to alter what happens in your program. This is as you mentioned [undefined behaviour](https://en.wikipedia.org/wiki/Undefined_behavior). When you have UB, weird things can happen, the program can behave the same 99% of the time, but sometimes differently, or just adding a line of code can alter the behaviour. – op414 Jun 11 '21 at 15:08
  • 1
    I suggest you use a modern compiler like MSVC, gcc or clang, and turn on all warnings (and read + understand them). This will catch this kinds of programming mistakes easily. Read [this post](https://stackoverflow.com/questions/57842756/why-should-i-always-enable-compiler-warnings) for more information. – op414 Jun 11 '21 at 15:14
  • 2
    `17:1: warning: control reaches end of non-void function [-Wreturn-type]` https://godbolt.org/z/Tf8s199fz – Marek R Jun 11 '21 at 16:44
  • @godbolt.org/z/Tf8s199fz in which compiler did you get this error ? –  Jun 12 '21 at 08:15
  • @godbolt.org/z/Tf8s199fz i guess adding `else return 1 ;` at end of palindrome function will sort this issue ... –  Jun 12 '21 at 08:17
  • 1
    @SMaheshKumar you have confused link to online compiler with a user :) – Marek R Jun 14 '21 at 14:35

1 Answers1

1

The code causes undefined behaviour:

  • rev was used while uninitialized.
  • palindrome can finish without executing a return statement, and the caller uses the return value.

The reason that a cout statement affects the behaviour could be that when the compiler reads the uninitialized storage , it happened to be in the same memory location as memory used by the cout call . (You cannot rely on this)

M.M
  • 138,810
  • 21
  • 208
  • 365