Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

What will this C code do?

Name: Anonymous 2012-01-19 8:24

void main(){
int x=1;
printf("%d\n",x=2);
}

Name: Anonymous 2012-01-21 15:43

>>79
It does according to the standard. Point me to something that says otherwise.

Name: Anonymous 2012-01-21 15:44

>>80
I gave a response under a tripcode, however, you totally ignored it.

Name: Anonymous 2012-01-21 15:45

>>82
I clearly didn't, because I said "as mentioned" and was referring to what you said.

Name: Anonymous 2012-01-21 15:45

>>81
What about global variables? Unless it's changed, it used to be that uninitialized global variables would become zero. In fact, a lot of older Linux code relies on this feature.

Name: Anonymous 2012-01-21 15:47

>>84
A global variable is an object that has static duration.

Name: Anonymous 2012-01-21 15:50

>>85
No. The because "static", when used conjunction with a variable, impacts the duration of the variable. Not its scope.

Name: Anonymous 2012-01-21 15:52

>>85
A variable in C has three properties. Scope, duration, and linkage. Using something like 'static' impacts these three basic properties.

Name: Anonymous 2012-01-21 15:52

>>73
As a veteran assembly programmer, I expect this to work as expected:
float foo = xyzzy;
int bar = evil_bit_magic( *(int*)&foo );
foo = *(float*)&bar;


Oh and I expect compilers not to optimize out (x < x+1). If I'm doing the comparison, it's for a reason, I'm not just putting it there for fun. Sure, if you can prove that x will always be smaller than INT_MAX, go ahead and optimize it out, otherwise fuck off. But don't give me undefined behaviour.

Name: Anonymous 2012-01-21 15:55

>>88
I don't think that's undefined behavior because you're not changing the value, but instead, reinterpreting it.

Name: Anonymous 2012-01-21 16:15

>>86
How could something with file scope (i.e. a global variable) not have static duration?

>>88
Strange example. Just cast foo to type int. It's well defined as long as the int can represent the integral part of the float. I don't actually know whether int* to float* and vice-versa is actually undefined. I just know that if they're incompatible in a sense that they're not correctly aligned, the behaviour is undefined. Logic tells me it should be fine (refer to what I first said).

If x is signed, I believe it would be optimized out whether the compiler knows its value or not, because an overflow can occur (undefined behaviour), so either way you're fucked. If it was unsigned, on the other hand, the compiler shouldn't optimize it out.

Name: Anonymous 2012-01-21 16:22

>>90
I was thinking about having a set of uninitialized variables defined in one file, and then having a different C program access these variables. Also, what >>88 isn't that strange because he is reinpreting the value.

Name: Anonymous 2012-01-21 16:24

>>91
And such a move is generally used in your larger programming projects.

Name: Anonymous 2012-01-21 16:25

>>90
because an overflow can occur (undefined behaviour)
All the architectures I work with use 2's complement for signed integers, so normally I would expect it to be very well defined.

Name: Anonymous 2012-01-21 16:26

>>91
A different C program or a different translation unit? I don't know about a different C program (not sure how that would work out), but objects with static duration are initialized before program startup, so the behaviour would be defined.

I said it was strange because he could just cast the float to int.

Name: Anonymous 2012-01-21 16:28

>>93
As long as you use an implementation that specifies how it deals with integer overflow, you should be fine.

Name: Anonymous 2012-01-21 16:37

>>95
There's a big difference between implementation-defined and undefined behaviour.

Name: Anonymous 2012-01-21 16:39

>>93
Completely irrelevant from the language's point of view. Integer overflow causes undefined behaviour, so anything happening is equally valid.

Name: Anonymous 2012-01-21 16:41

>>95
I don't know.  I'm just getting this feeling that there's just too many little things like this, just waiting there to bite me in the allegorical ass.  I'm afraid for the security and correctness of my code.  Sigh.  I don't know, I might actually start working in assembly for my low-level work.  But then I'd have to completely rewrite everything for other architectures.  That's a lot of extra work.  I'm gonna go pop a beer and maybe I'll figure it out eventually.

Name: Anonymous 2012-01-21 16:43

check 'em

Name: Anonymous 2012-01-21 16:47

>>96
I know it would still be undefined behaviour at the end of the day, but in practice, you could get away with it. The best thing to do would be to check whether [code]x[code] is about to overflow.

>>98
Correct me if I'm wrong, but even with assembly you'd still have the same problems. Again, the best thing to do would be to check the value.

Name: Anonymous 2012-01-21 17:14

>>98
You could spend your time sub-optimally unrolling loops and shuffling registers around like a retard or you could let the compiler do a better job for you.

Name: Anonymous 2012-01-21 17:32

>>101
Slow and secure VS Fast and flawed.
I know what I'd pick.

Name: Anonymous 2012-01-21 17:33

alternately you could make a better compiler

Name: Anonymous 2012-01-21 17:35

As a noobish newcomer, I would do this:
#include <stdint.h>
union word {
    float foo;
    int32_t bar;
};
int main() {
    union word a = {1.0};
    a.bar = evil_bit_magic(a.bar);
}


Why programmers have this hacker syndrome, denying stuff like if (x == INT_MAX) {...}? It's standard and much more readable.

Name: Anonymous 2012-01-21 17:38

>>104
union

finish reading K&R

Name: Anonymous 2012-01-21 17:39

>>104
Writing into one member of a union and reading from another invokes undefined behaviour. I know at least GCC's optimizers take advantage of this fact.

Name: Anonymous 2012-01-21 18:06

>>94
I said it was strange because he could just cast the float to int.

You don't understand that the code isn't changing the value. Instead, it is reinterpreting it.

Name: Anonymous 2012-01-21 18:24

>>106
No, I couldn't find evidence. Standard only cites that an union is a structure where the storage of all its members overlap. The other way around may produce undefined behavior, if the value assigned to a.bar isn't a valid float. Also, if a.bar overflows, it's better to write:
union word {
    float foo;
    uint32_t bar;
};

Name: Anonymous 2012-01-21 20:14

>>108
ISO/IEC 9899:1999
Annex J
J.1 Unspecified behaviour
The following are unspecified:
- The value of a union member other than the last one stored into (6.2.6.1).


§6.2.6.1/7:
When a value is stored in a member of an object of union type, the bytes of the object representation that do not correspond to that member but do correspond to other members take unspecified values, but the value of the union object shall not thereby become a trap representation.

Name: Anonymous 2012-01-21 20:58

>>108
>>109
Whelp, ISO/IEC 9899:1999:TC3 seems to have legalized type punning via unions:
§6.5.2.3/3 fn 82:
If the member used to access the contents of a union object is not the same as the member last used to store a value in the object, the appropriate part of the object representation of the value is reinterpreted as an object representation in the new type as described in 6.2.6 (a process sometimes called "type punning"). This might be a trap representation.

Name: Anonymous 2012-01-21 21:54

epic triples xD

Name: Anonymous 2012-01-21 23:59

>>111
If it ain't trips, it's crap.

Name: Anonymous 2012-01-22 0:32

Trips' shit.

Name: Anonymous 2012-01-22 12:17

>>94
A different C program or a different translation unit? I don't know about a different C program (not sure how that would work out), but objects with static duration are initialized before program startup, so the behaviour would be defined.

Informally, in C, variables are named objects. These variables have scope, duration, and linkage. Also, an object is a runtime thing. Ie, it doesn't exist at compile time. Eer....why am I arguing with some faggot that thinks global variables are static?

Why don't you just fucking read the standard and write some code you moron.

Name: Anonymous 2012-01-22 12:21

I declare all my global variables in the data segment because i'm a boss nigger.

check my (postcount % 15) dubs niggas

Name: Anonymous 2012-01-22 12:52

>>114
Are you that same person (kodak_gallery_programmer)? You realize global variables have file scope by definition, right? Since they have file scope, they can't have automatic duration. The object could be a pointer to an object that has allocated duration, but the variable itself would still have static duration.

Name: Anonymous 2012-01-22 13:03

>>116
Again, for the second time, a variable and an object in C aren't the same thing. Which part of this doesn't your monkey ass get?

Name: Anonymous 2012-01-22 13:04

>>116
And again, an object in C is a runtime thing. Now why don't you just shut the fuckup and read the standard instead of giving your own creative interpretation of the language.

Name: Anonymous 2012-01-22 13:25

>>117,118
They refer to the same thing in this context... I don't understand what you're getting at.

Name: Anonymous 2012-01-22 13:29

>>119
No they don't. Why are you trying to argue about something that you clearly don't understand? Anyways, at least in C, it's possible for a variable to not have an associated object with it. Likewise, it's possible to have an unnamed variable.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List