It seems that you're using an outdated browser. Some things may not work as they should (or don't work at all).
We suggest you upgrade newer and better browser like: Chrome, Firefox, Internet Explorer or Opera

×
avatar
Alaric.us: "You're."
avatar
Cavenagh: Sarcasm :P
Lol.

http://replygif.net/i/715.gif
avatar
dtgreene: Anyway, does anyone know why the line of C code

int x = x;
avatar
dtgreene: is even legal in the first place? (It is; as I said, the code I posted surprisingly does compile.)
I don't think it is legal. But I guess the code gets optimized away before it even has a chance to throw an error.
low rated
avatar
immi101: I don't think it is legal. But I guess the code gets optimized away before it even has a chance to throw an error.
The weirdest part is not even int x = x; Since it does nothing at all, it could technically be ignored by the compiler altogether. What is weird, is this: printf(x); Even if the previous line is ignored somehow, this should definitely throw an error.
avatar
immi101: I don't think it is legal. But I guess the code gets optimized away before it even has a chance to throw an error.
avatar
Alaric.us: The weirdest part is not even int x = x; Since it does nothing at all, it could technically be ignored by the compiler altogether. What is weird, is this: printf(x); Even if the previous line is ignored somehow, this should definitely throw an error.
oh, i didn't even saw that part. Just saw the int x = x line.
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;
}
//edit2:
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
What was this thread about? :P
edit: ^ right, so it's just set to 0. Likely because it's in a main method, and implied static.
avatar
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?
Post edited November 18, 2016 by nipsen
avatar
phaolo: What was this thread about? :P
It was a stage/forum dramatization of "The Meaning of Life".
https://www.youtube.com/watch?v=7DA2MKuI6fs
avatar
nipsen: 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?
Why wouldn't your java compiler allow that to compile? "something" will be initialized by the incoming argument when the method is called, so it has an int value for the while comparison and walkroads (assuming it exists and takes two ints for arguments) should be called on each while interation.

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.
low rated
avatar
nipsen: 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?
avatar
Bookwyrm627: Why wouldn't your java compiler allow that to compile? "something" will be initialized by the incoming argument when the method is called, so it has an int value for the while comparison and walkroads (assuming it exists and takes two ints for arguments) should be called on each while interation.

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.
Yea, that code seems like it should be able to compile. If, when running it, you screw up and try executing meaning without giving it an int that it expects, only then should it error out.

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
avatar
Alaric.us: I guess all languages and all compilers are different in how they handle things.

Kind of like ... people on a forum.
I hope you don't mind, but I'm getting this as a forearm script tattoo in cursive bold.

Don't worry, I'll credit you, Duke.
We should change to thread topic to "An exercise in derailing".
And it wasn't even TinyE who did it 0_o
avatar
toxicTom: We should change to thread topic to "An exercise in derailing".
And it wasn't even TinyE who did it 0_o
It was derailed before it even began.
Also, good to see you back on the forum! How's life treatin' ya these days?
avatar
zeogold: It was derailed before it even began.
Also, good to see you back on the forum! How's life treatin' ya these days?
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 :-/
avatar
toxicTom: Used to be "intoxicating" to hang around here... what a shame :-/
There's still fun to be had, you just gotta find the right places.
We really could use another one of those "hangout" threads. Maybe I'll make one someday.
low rated
avatar
toxicTom: Used to be "intoxicating" to hang around here... what a shame :-/
avatar
zeogold: There's still fun to be had, you just gotta find the right places.
We really could use another one of those "hangout" threads. Maybe I'll make one someday.
Yup that is what it is all about having fun on forum....

This is not a store selling video games