Author Topic: Variable's scope lifetime [LPC]  (Read 4260 times)

Offline nfa

  • Acquaintance
  • *
  • Posts: 28
    • View Profile
Variable's scope lifetime [LPC]
« on: December 21, 2011, 04:14:17 am »
Ok, I usually don't get stumped anymore after 10+ years of coding in LPC. But today it happend. I assumed one behaviour and it turned out to be completely wrong. Note this is only tested in FluffOS 2.23.

Code: [Select]
void create()
{
    for(int i=0;i<=10;i++) {
        int x;

        printf("X = %d\n", x);
        x = 1;
    }
}


Feature?/Oversight? Or perhaps I'm just the only one that never realized this  :-\


Volothamp@Final Realms

Offline wodan

  • BFF
  • ***
  • Posts: 434
  • Drink and code, you know you want to!
    • View Profile
Re: Variable's scope lifetime [LPC]
« Reply #1 on: December 21, 2011, 04:17:33 pm »
Consider it undefined behaviour, you didn't give it a value after all :)

They're all set to 0 only once, when the function starts, I wouldn't depend on this behaviour staying the same forever!

Offline reallove

  • Acquaintance
  • *
  • Posts: 8
    • View Profile
Re: Variable's scope lifetime [LPC]
« Reply #2 on: December 21, 2011, 10:13:21 pm »
Code: [Select]
/u/test.c X = 0
X = 1
X = 1
X = 1
X = 1
X = 1
X = 1
X = 1
X = 1
X = 1
X = 1

wawawawawa

Offline nfa

  • Acquaintance
  • *
  • Posts: 28
    • View Profile
Re: Variable's scope lifetime [LPC]
« Reply #3 on: December 22, 2011, 03:15:41 am »
I wouldn't depend on this behaviour staying the same forever!
Oh, I won't - it caused an error for me when an integer I used for bitflags wasn't reinitialized on second iteration and forward.

Offline hamlet

  • Acquaintance
  • *
  • Posts: 46
    • View Profile
Re: Variable's scope lifetime [LPC]
« Reply #4 on: December 22, 2011, 09:44:13 am »
from a "security" standpoint, it's important to zero variables at the start to ensure you don't get access to the previous memory occupant's data.  There's no need for this security when it's -you- (this particular bit of code) reusing the same space, though.

So consider the "variables are zeroed" thing not a feature but just a side-effect of something else.  In other words, never assume it will be true.  Always zero your variables if it's relevant.

Offline Jimorie

  • Friend
  • **
  • Posts: 58
    • View Profile
Re: Variable's scope lifetime [LPC]
« Reply #5 on: December 24, 2011, 03:56:29 am »
This behaviour differs between drivers. DGD, for example, re-initializes the variable x to 0 each iteration -- which also is the common behaviour in C and Java, for reference.

Offline wodan

  • BFF
  • ***
  • Posts: 434
  • Drink and code, you know you want to!
    • View Profile
Re: Variable's scope lifetime [LPC]
« Reply #6 on: December 24, 2011, 03:48:53 pm »
Ehm, nope, C will not put anything in there for you at all.

Offline Jimorie

  • Friend
  • **
  • Posts: 58
    • View Profile
Re: Variable's scope lifetime [LPC]
« Reply #7 on: December 26, 2011, 04:47:30 pm »
Ehm, nope, C will not put anything in there for you at all.

Okay. I'm no expert so I won't argue that. My gcc compiled it without complaint and gave the same behaviour as DGD and Java.

Offline Raudhrskal

  • BFF
  • ***
  • Posts: 214
  • The MUD community needs YOUR help!
    • View Profile
Re: Variable's scope lifetime [LPC]
« Reply #8 on: December 27, 2011, 09:34:01 am »
In C, "int x;" just reserves space for the variable on the stack.
If you'll read it before writing anything to it, you get whatever else was in this memory location before, a leftover from some other function call or such.
I think, therefore i may be wrong.
Please note that if you met a Raudhrskal in a place that's not related to muds, it wasn't me. *sigh*... back when I started there was zero hits on google for that name...

Offline Jimorie

  • Friend
  • **
  • Posts: 58
    • View Profile
Re: Variable's scope lifetime [LPC]
« Reply #9 on: December 27, 2011, 01:20:00 pm »
Thanks, Raudhrskal. Now I understand. I retract my statements about Java and C, as they are irrelevant.

I tried running the LPC code on a few more different drivers, just for reference.

Code: [Select]
int i;
for(i = 0; i <= 10; i++)
{
    int x;
    write(x + "\n");
    x = 1;
}

Here are the results.

MudOS (recent version):
Code: [Select]
0
1
1
1
1
1
1
1
1
1
1

Amylaar (real old one):
Code: [Select]
0
1
1
1
1
1
1
1
1
1
1

LDMUD (recent version):
Code: [Select]
0
0
0
0
0
0
0
0
0
0
0

DGD (recent version):
Code: [Select]
0
0
0
0
0
0
0
0
0
0
0