3

here is my code.

#include <stdio.h>
#define MAX(a,b) ((a)>(b)?(a):(b))
#define SZA(a) (sizeof(a)/sizeof(a[0]))

int anish=0; // global variable showing abrupt behaviour
int a[]={0,1,5,8,9,10,17,17,20,24,30};
int n= SZA(a);

int cache[100];
int road(int len)
{
    anish++;

    if(cache[len]!=-1)
        return cache[len];
    if(len<=0)
        return 0;
    int max=0;
    int i;
    for(i=1;(i<n && len>=i);i++)
    {
        max=MAX(max,a[i]+road(len-i));
    }
    return cache[len]=max;
}

void f()
{
    anish=13;
}

int main()
{
    int i;
    for(i=0;i<=13;i++)
        cache[i]=-1;
    int len=10;
    // f();

    printf("%d %d\n",road(len),anish);
    return 0;
}

In this, road() is a recursive function and I want to calculate the number of times this function is being executed. So, I am doing this via a global variable anish. But the value of anish is not changing in this road() function, while in function f() value of anish is being modified.

Any reason for this absurd behavior?

RBerteig
  • 41,948
  • 7
  • 88
  • 128
  • Lol, you keeping changing your mind about which answer to accept. – Chimera Aug 30 '12 at 21:16
  • I wanted to accept both answer as both are same but turned out you can accept only one answer :/ –  Aug 30 '12 at 21:18
  • If you find my answer worthy, you could upvote it. Click on the arrow above the 0. Thanks for considering my answer. – Chimera Aug 30 '12 at 21:20
  • 2
    I edited the question to copy the original source code from ideone. SO prefers to have questions (and answers) be self contained so that when the link inevitably gets stale, the question is still meaningful and helpful to some future reader. While there I also tweaked the text to make it clearer. – RBerteig Aug 30 '12 at 21:44

4 Answers4

3

your c code shows an outdated anish variable succesive printf's shows different value

this has something to do with c's internals

it has something to do with this : Parameter evaluation order before a function calling in C

loki astari said:

The order that function parameters are evaluated is unspecified behavior. (This won't make your program crash, explode, or order pizza... unlike undefined behavior.)

The only requirement is that all parameters must be fully evaluated before the function is called.

Community
  • 1
  • 1
maazza
  • 7,016
  • 15
  • 63
  • 96
3

In

printf("%d %d\n",road(len),anish);

the order in which the arguments are evaluated is unspecified. In this case, it seems anish is evaluated before road(len), so the value passed to printf is the unmodified value.

Evaluate road(len) before calling printf and store the result in a temporary variable.

Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431
1

Try this instead:

int main()
{    int i;
    for(i=0;i<=13;i++)
      cache[i]=-1;
      int len=10;
     // f();

  printf("%d\n",road(len));
  printf("%d\n", anish);
  return 0;
}

Most likely what is happening is printf() is using the value of anish before road(len) returns.

See this for the details.

Chimera
  • 5,884
  • 7
  • 49
  • 81
0

If you do it like this, it'll work:

printf("%d ",road(len));
printf("%d\n",anish);

may be its because of something inside the printf definition,

Rami Jarrar
  • 4,523
  • 7
  • 36
  • 52