Posted November 18, 2016

Cyraxpt
ZzZzZz
Registered: Feb 2011
From Portugal

immi101
User
Registered: May 2010
From Germany
Posted November 18, 2016

Alaric.us
Slava Ukraini
Registered: Feb 2010
From United States
Posted November 18, 2016
low rated


immi101
User
Registered: May 2010
From Germany
Posted November 18, 2016


Yeah, then I have no clue either.
//edit:
it might just optimize away the value assignment (x=x), but keep the declaration of int x.
Thus printf(x) doesn't throw an error, but just prints some random value of an uninitialized variable.
And using an uninitialized variable is apparently just "undefined behavior" according to the standard, but not an error.
still weird that gcc doesn't throw at least a warning.
if you compile that code with optimization turned on you get
int main()
{
print(0)
return 0;
}
gcc wants to be invoked with this to give out a warning here:
gcc -Wunitialized -Winit-self
probably futile to try to see the logic behind that :p
Post edited November 18, 2016 by immi101

phaolo
Durik - Half-Orc
Registered: Dec 2013
From Italy
Posted November 18, 2016
What was this thread about? :P

nipsen
New User
Registered: Aug 2012
From Norway
Posted November 18, 2016
edit: ^ right, so it's just set to 0. Likely because it's in a main method, and implied static.
dtgreene: is even legal in the first place? (It is; as I said, the code I posted surprisingly does compile.) Well, it's legal syntax. :) But if x wasn't defined outside the method somewhere, or.. sort of guessing here.. made static by context? Then you would init x locally to a variable that doesn't actually exist, and the compiler shouldn't allow it.. at least once you start doing something to it later.
Guess you could imagine that there's an include somewhere that defines x. So every time the method is called, the x value from the include is used. So now you have a static x from the include, and a local, new, completely different x in the method.
But if x didn't exist before main, it's the same as saying int x=y;, where y never existed. But that's why you would get whatever value in a stack somewhere this variable is assigned to on the init, because it's uninitiated.
..Then again, maybe it compiles because x is initialized this way in the main method, so it's sort of guaranteed to run at least once. And you'll probably get a zeroed value then? So what's really run is int x; then x=x; or x=0;. Or the same as int x;.
So if this code is used somewhere, you can sort of imagine that the purpose was to make sure that x is zeroed to that value on the outside context when the main method is called, after it was changed in some indeterminate way during an init phase on a lower nested function (like.. hey, let's run through a grid and count the grid for consistency!). And int x; was thrown out and detected as an undefined value, while int x=x; runs through since it assigns an undefined value. Who knows.
Maybe java spoils me or something. My compiler won't allow things like
public void meaning(int something){
while(something < 42){
walkroads(1, something);
}
}
Because walkroads could generate any amount of peculiar bytecode to be run, depending on what something is assigned to. Or the compiled code would never have walkroads in it, because something is assumed to be higher than 42 at compile time. But because java, it just stops with "uninitialized value" for something. ..I think?

Guess you could imagine that there's an include somewhere that defines x. So every time the method is called, the x value from the include is used. So now you have a static x from the include, and a local, new, completely different x in the method.
But if x didn't exist before main, it's the same as saying int x=y;, where y never existed. But that's why you would get whatever value in a stack somewhere this variable is assigned to on the init, because it's uninitiated.
..Then again, maybe it compiles because x is initialized this way in the main method, so it's sort of guaranteed to run at least once. And you'll probably get a zeroed value then? So what's really run is int x; then x=x; or x=0;. Or the same as int x;.
So if this code is used somewhere, you can sort of imagine that the purpose was to make sure that x is zeroed to that value on the outside context when the main method is called, after it was changed in some indeterminate way during an init phase on a lower nested function (like.. hey, let's run through a grid and count the grid for consistency!). And int x; was thrown out and detected as an undefined value, while int x=x; runs through since it assigns an undefined value. Who knows.
Maybe java spoils me or something. My compiler won't allow things like
public void meaning(int something){
while(something < 42){
walkroads(1, something);
}
}
Because walkroads could generate any amount of peculiar bytecode to be run, depending on what something is assigned to. Or the compiled code would never have walkroads in it, because something is assumed to be higher than 42 at compile time. But because java, it just stops with "uninitialized value" for something. ..I think?
Post edited November 18, 2016 by nipsen

zeogold
The Puzzlemaster
Registered: Dec 2012
From United States
Posted November 18, 2016
It was a stage/forum dramatization of "The Meaning of Life".
https://www.youtube.com/watch?v=7DA2MKuI6fs
https://www.youtube.com/watch?v=7DA2MKuI6fs

Bookwyrm627
ADD Jumping Bean
Registered: Nov 2013
From United States
Posted November 18, 2016

public void meaning(int something){
while(something < 42){
walkroads(1, something);
}
}
Because walkroads could generate any amount of peculiar bytecode to be run, depending on what something is assigned to. Or the compiled code would never have walkroads in it, because something is assumed to be higher than 42 at compile time. But because java, it just stops with "uninitialized value" for something. ..I think?
If you tried to call meaning with something besides an int, then the compiler still wouldn't trip over this method because the method signature would indicate some other method named "meaning" should be called.

Alaric.us
Slava Ukraini
Registered: Feb 2010
From United States
Posted November 18, 2016
low rated

public void meaning(int something){
while(something < 42){
walkroads(1, something);
}
}
Because walkroads could generate any amount of peculiar bytecode to be run, depending on what something is assigned to. Or the compiled code would never have walkroads in it, because something is assumed to be higher than 42 at compile time. But because java, it just stops with "uninitialized value" for something. ..I think?

If you tried to call meaning with something besides an int, then the compiler still wouldn't trip over this method because the method signature would indicate some other method named "meaning" should be called.
I guess all languages and all compilers are different in how they handle things.
Kind of like ... people on a forum. o_O
Post edited November 18, 2016 by Alaric.us

Kleetus
For Internal Use Only
Registered: Sep 2010
From Australia

toxicTom
Big Daddy
Registered: Feb 2009
From Germany
Posted November 18, 2016
We should change to thread topic to "An exercise in derailing".
And it wasn't even TinyE who did it 0_o
And it wasn't even TinyE who did it 0_o

zeogold
The Puzzlemaster
Registered: Dec 2012
From United States

toxicTom
Big Daddy
Registered: Feb 2009
From Germany
Posted November 18, 2016
I'll PM you when I'm not as drunk as I'm now. The forum has become to "toxic" even for me to discuss things like this out in the open. Used to be "intoxicating" to hang around here... what a shame :-/

zeogold
The Puzzlemaster
Registered: Dec 2012
From United States

Regals
New User
Registered: Mar 2015
From United States