1

I'm having something like this:

void test(int iter){    
  int i;    
  if(iter>3){    
    i=5;    
    printf("%d",i);    
  }else{    
    printf("%d",i);    
  }    
  return;    
}
int main(){
  test(5);
  test(2);
  return 1;
}

This is just for example. Is it possible when called by main variable i hold value of 5 (in memory) if at least iter is greater than 3? I know that it is not normal. But I have a function in my program (I don't have static var that is doing this) which does it. So maybe the address where this variable is initialised each time the same (so I'm getting the old value)? Or what does happen? I don't want to hold the old value or something. I'm just curious: how is this possible?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Theredone
  • 55
  • 1
  • 6
  • 1
    Is this behavior that you are experiencing and find to be an error, or is it behavior you want to create? – Jayson Boubin Jun 02 '17 at 00:34
  • 3
    This is undefined behavior so the displayed value could be anything (or nothing or a crash). One way to reason about the behavior is this famous SO answer: https://stackoverflow.com/a/6445794/12711 – Michael Burr Jun 02 '17 at 00:48

4 Answers4

2

If i is stored at some location on the stack, and if that location is not modified, then i will retain it's value.

Assuming that i is even stored on the stack (optimization may eliminate this), if there is an interrupt that uses the stack (or perhaps a context switch due to time slice), the stack area below the stack pointer will get overwritten. Some compilers may fill in the stack area, usually in debug mode. Visual Studio in debug mode will catch the usage of an uninitialized variable with the above code.

rcgldr
  • 27,407
  • 3
  • 36
  • 61
  • 1
    So if after a call, the value of "i" is on stack as trash, and if this address of the stack wont get used(initialized by something else) my next call of test in this case, will print 5. if i isn't initialized but is defined in the the same address as before and prints trash that are actually 5. Is this right?@rcgldr – Theredone Jun 02 '17 at 01:00
  • 2
    @Theredone - depending on optmization, `i` may not even be stored on the stack, and replaced with immediate values in the code. Assuming `i` is on the stack, then an interrupt or possibly a context switch (time slicing) could modify the location where `i` is stored. If `i` is stored on the stack, and nothing changes the value at that location, then the value of `i` will be retained. – rcgldr Jun 02 '17 at 01:23
1

In this example you are showing us, you never initialize i with a value, and it is never assigned a value unless iter is > 3.

Its value is undefined until you actually set it to something, which you only do in the "if" part of your "if then else" clause.

So it's perfectly normal that you might have a garbage value in i when iter is 3 or less.

Mozahler
  • 4,958
  • 6
  • 36
  • 56
  • 1
    I was getting somehow 5 each time after i was initialised once. Even if iter was less than 3. But each time. So i m trying to understand how is possible. Getting 5 once may happen but getting it each time is a bit weird. – Theredone Jun 02 '17 at 01:09
  • 1
    No it isn't really, though. It could be a different value running on a different device. It could be the same every time, it is "undefined", and you don't know how the compiler was written. – Mozahler Jun 02 '17 at 01:13
1

Declear your variable Global (out of function). for example:

int a=0;
void f(){
    a++; 
}

this way the value will not be lost after execution of function.

Ali Molaei
  • 396
  • 2
  • 13
  • 2
    first off not the point of the question. The OP wants to know an explanation of the behavior not a way to store variables. Second off if the variable is local to the function `static` is a better option then globals. – twain249 Jun 02 '17 at 00:52
1

Do this:

void test(int iter){    
  static int i;    
  if(iter>3){    
    i=5;    
    printf("%d",i);    
  }else{    
    printf("%d",i);    
  }    
  return;    
}
int main(){
  test(5);
  test(2);
  return 1;
}

Note the static keyword in the static int i; line.

  • 1
    Sorry, I have answered the wrong question. My bad. If you can change the title to something like: *I am having this odd behavior where a varible in a function hold an old value from old calls of the function. How can this be possible?*; It would avoid some mistaken answers. – Fábio Roberto Teodoro Jun 02 '17 at 01:35