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

Pages: 1-4041-8081-120121-

For you

Name: Anonymous 2012-01-12 14:20

What is the output of this program and why?

#include <stdio.h>

void f(void) {
  printf("hax my anus\n");
  fflush(stdout);
  f();
}

int main(void) {
  f();
  return 0;
}

Name: Anonymous 2012-01-12 14:21

It's undefined

Name: Anonymous 2012-01-12 14:22

It prints "hax my anus\n" forever.

Name: Anonymous 2012-01-12 14:23

>>2
No it's not. It's implementation defined.

Name: Anonymous 2012-01-12 14:25

>>4
printf without format string

Name: Anonymous 2012-01-12 14:27

>>5
So "hax my anus\n" isn't a format string?

Name: Anonymous 2012-01-12 14:27

>>5
Look again.

Name: Anonymous 2012-01-12 14:29

>>4-7
No sense of humor

Name: Anonymous 2012-01-12 14:30

>>8
Oh, you were joking? Get the fuck back to /g/.

Name: Anonymous 2012-01-12 14:30

>>9
Go back to /g/

Name: Anonymous 2012-01-12 14:32

>>9
No he does what they always do on /g/, they say some retarded shit, it gets pointed out and then they pretend they were just joking.

Name: Anonymous 2012-01-12 14:33

>>3
On my machine it segfaults after a while.

Name: Anonymous 2012-01-12 14:35

>>12
infinite recursion, much?

Name: Anonymous 2012-01-12 14:37

>>12
Exactly, as I said in >>2 it is undefined.

>>4-11
See above, jokes on you

Name: Anonymous 2012-01-12 14:38

It's not undefined, it runs forever.

Name: Anonymous 2012-01-12 14:38

>>15
No it doesn't, it crashes.

Name: Anonymous 2012-01-12 14:39

When I compile with -O3 it doesn't segfault but when I don't it does.

Name: Anonymous 2012-01-12 14:39

>>17
herf derf it's undefined

Name: Anonymous 2012-01-12 14:39

>>16
If that's the case then your implementation of C is non-conformant.

Name: Anonymous 2012-01-12 14:40

That program is well defined and will run forever on a C virtual machine.

Name: Anonymous 2012-01-12 14:40

>>19
But it is non-deterministic, therefore the behaviour is undefined.

Get back to /g/

Name: Anonymous 2012-01-12 14:41

>>21
No it's completely deterministic, it runs forever.

Name: Anonymous 2012-01-12 14:41

What exactly is undefined about it?

That you run out of stack space? Well there isn't any stack in C.

Name: Anonymous 2012-01-12 14:42

>>20
| forever

Protip: Forever is longer than you think.

Name: Anonymous 2012-01-12 14:42

>>1
It's undecidable.

Name: Anonymous 2012-01-12 14:42

>>23
It's non-deterministic, i.e. it doesn't have an entry point and an exit point.

Name: Anonymous 2012-01-12 14:45

It doesn't crash on my Turing machine, it runs forever.

Name: Anonymous 2012-01-12 14:47

>>22
Forever is an abstract term how can it be deterministic?

Name: Anonymous 2012-01-12 14:48

It runs forever, a valid optimization is

void f(void) {
  while (1) {
    printf("hax my anus\n");
    fflush(stdout);
  }
}


Which is why it won't crash on your machines when you compile it with GCC and use optimization (GCC does TCO).

Name: Anonymous 2012-01-12 14:48

>>22
It can't be deterministic.

Name: Anonymous 2012-01-12 14:49

>>29
It's still non-deterministic, therefore undefined. You cant prove that it will produce the same output on any machine that implements C correctly.

Name: Anonymous 2012-01-12 14:49

The C program won't stop executing and will continue to print "hax my anus\n", on our feeble machines it will probably crash due to resource exhaustion.

Name: Anonymous 2012-01-12 14:50

>>29
oh you,
inb4 mental midgets, toilet scrubing, no future in the 'real world'

Name: Anonymous 2012-01-12 14:50

>>31
It's well defined, please cite from the C standard what causes the undefined behavior.

Name: Anonymous 2012-01-12 14:51

>>22
prove that it runs forever.

Name: Anonymous 2012-01-12 14:51

>>34
| C standard
I'm talking about determinism v. non-determinism here

Go back to /g/ asshole

Name: Anonymous 2012-01-12 14:52

>>33
I don't know what Kodak would say about this one to be honest, his two great loves, "real world programming" and "the C standard collides".

Name: Anonymous 2012-01-12 14:52

>>36
So you can't? I can tell you why, because it is well defined.

Name: Anonymous 2012-01-12 14:53

>>38
Prove that it runs forever then, asswipe

Name: Anonymous 2012-01-12 14:54

>36
| Please provide proof that something doesn't exist
That is not how things work, brah

Name: Anonymous 2012-01-12 14:56

>>1
Non-deterministic code, and also it will not produce the same output for all implementations of C, however "correct" or "incorrect" they are.

Name: Anonymous 2012-01-12 14:56

So proving or disproving this is like solving the halting problem for this program or something?

Name: Anonymous 2012-01-12 14:56

C99 6.5.2.2.11 Recursive function calls shall be permitted, both directly and indirectly through any chain of other functions.
Because C has no stack, it can theoretically run forever. In fact tail recursion optimizations are possible only because the C standard does not mention a stack.

Name: Anonymous 2012-01-12 14:56

>>41
That's wrong, if it crashes then your implementation of C is flawed and not really C.

Name: Anonymous 2012-01-12 14:56

Go back to /v/

Name: Anonymous 2012-01-12 14:57

using printf instead of puts for printing strings
IHBT

Name: Anonymous 2012-01-12 14:57

>>42
What a neat little troll.

Name: Anonymous 2012-01-12 14:58

>>43
See? It's well defined and does run forever.

Name: Anonymous 2012-01-12 14:58

>>44
I said so (ref. implementations of C, however "correct" or "incorrect" they [the implentations] are), but the code is still non-deterministic!

Name: Anonymous 2012-01-12 14:59

>>48
| does run forever.
Prove it

Name: Anonymous 2012-01-12 14:59

Okay I get it, /g/ doesn't know the standard so they now think it's undefined after you spammed the shit out of them about the undefined program. Very clever, go away now.

Name: Anonymous 2012-01-12 14:59

ITT people using  gnuc89 or gnuc99 and it seg faults

Name: Anonymous 2012-01-12 15:00

>>43
A side note: The stack isn't the limitation. Theoretically a stack could have infinitely space.

Name: Anonymous 2012-01-12 15:00

No. The standard assumes that the host environment has sufficient resources to store and execute the program code itself -- which means that things like stack overflows and static allocation failures cannot happen on the C abstract machine. The behavior of the program is thus well defined, it prints "hax my anus\n" ad infinitum.

You might argue that these assumptions aren't sound, but it would be absolutely silly in any other case. What if the environment has insufficient resources to load the program code, should we be able to catch and deal with a code allocation failure?

Name: Anonymous 2012-01-12 15:00

>>50
Okay, it doesn't call exit and it never returns, so it runs forever.

Name: Anonymous 2012-01-12 15:01

>>55
Nope, that is not proof. It is an indication, but not a formal proof.

Name: Anonymous 2012-01-12 15:02

>>56
Please cite from the C standard how it might exit then.

Name: Anonymous 2012-01-12 15:02

>>54
Defined, yes, but not deterministic. Also, unlimited resources is a false premise as you have just shown.

>>55
That is not a proof. What if the program is interrupted.

Name: Anonymous 2012-01-12 15:02

>>54
Damn what a neat fucking troll. I'm going to post this on /g/.

Name: Anonymous 2012-01-12 15:03

>>43
Damn, you must have the link to the standard bookmarked or something.

Name: Anonymous 2012-01-12 15:04

>>58
What do you mean deterministic? Do you even have a clue what you are talking about?

Name: Anonymous 2012-01-12 15:04

>>57
Signals

Name: Anonymous 2012-01-12 15:06

Name: Anonymous 2012-01-12 15:07

>>60
It costs money, he probably has it in a pdf on his computer.

Name: Anonymous 2012-01-12 15:07

>>63
This has nothing to do with a Turing machine. Do you know what the C abstract machine is?

Name: Anonymous 2012-01-12 15:07

HEY PROG RUN MY PROGRAM AND TELL ME THE OUTPUT


#MIPS
main:   li $v0,4
        la $a0,hax
        syscall
        j main
.data
        hax: .asciiz "hax my anus\n"

Name: Anonymous 2012-01-12 15:08

Where may I acquire one of these C abstract machines?

Name: Anonymous 2012-01-12 15:08

I run all my programs on the C abstract machine.

Name: Anonymous 2012-01-12 15:09

>>65
The point wasn't the turing machine, but that you may have different results for every run, there isn't a well-defined entry and exit point, which is a requirement for determinism, therefore the program is ultimately indeterministic.

Name: Anonymous 2012-01-12 15:10

>>67
abstract
You sure you know what that word means?

Name: kodak_gallery_programmer !!kCq+A64Losi56ze 2012-01-12 15:11

I think this would be undefined behavior because you're flushing stdout twice. There could be crash because you're attempting to free() more memory that what you allocate.

Name: Anonymous 2012-01-12 15:11

Gentlemen, please, if you are from /g/ and have arrived to /prog/ this month, please leave, there is no sense in arguing about things like these. The answers to questions like these are defined by standards so there is an actual answer to the questions, there is little use arguing about them without a copy of the standard so if you don't have one, please leave this thread.

Thank you for listening.

Name: Anonymous 2012-01-12 15:11

>>69
You don't have any idea what you are talking about. Please go away, I'm not going to explain things to a child.

Name: Anonymous 2012-01-12 15:12

http://www.gowrikumar.com/c/
have some real what does this prog output riddles,
don't come back to /prog/ until you clear them

Name: Anonymous 2012-01-12 15:14

>>72
standard is li3k 550 pages
(least the copy I just downloaded from the interwebz)
Seriously you call yourself professional and you can't even memorize a measly 550 pages.
Toilet scrubbing boo-hoo.

Name: Anonymous 2012-01-12 15:14

>>71
Does flushing stdout twice cause undefined behavior? That I didn't know. About free I'm guessing that fflush somehow calls free?

Name: Anonymous 2012-01-12 15:15

>>73
| blabla I failed formal logic 101
Okay then

Name: Anonymous 2012-01-12 15:16

>>71
Doesn't say so in the standard!!

Go back to /g/

Name: Anonymous 2012-01-12 15:17

So this ten line program have you all wondering?
Which of you are actually programmers?

Name: Anonymous 2012-01-12 15:17

>>79
Kodak

Name: Anonymous 2012-01-12 15:19

>>79
Implying that we can't argue about the obvious.

Name: Anonymous 2012-01-12 15:20

>>79
The correct solution was already given in >>3.

Name: Anonymous 2012-01-12 15:20

HERP DERP THE PROGRAM IS UNDEFINED SINCE IT RELIES ON UNDEFINED BEHAVIOR AND IT ONLY RUNS THAT WAY IF COMPILED WITH GCC

I am of course referring to the Segmentation fault output the program ultimately produces on a x86

Name: Anonymous 2012-01-12 15:21

>>82
It doesn't run "forever".

Name: Anonymous 2012-01-12 15:21

>>83
Actually this is a well defined program that doesn't rely on any compiler.

Name: Anonymous 2012-01-12 15:21

>>84
Yes it does, if it doesn't then your implementation of C is flawed.

Name: Anonymous 2012-01-12 15:22

It depends on OP's premise: Is he referring to the output he gets on his machine, or from a "correct" implementation of C? In the first case it relies on undefined behavior that ultimately results in a Segmentation fault. Clever troll. Now go back to /g/

Name: Anonymous 2012-01-12 15:22

>>85-86
It depends on OP's take

Name: Anonymous 2012-01-12 15:22

>>83
On my x86 computer it doesn't stop executing.

Name: Anonymous 2012-01-12 15:23

>>86
Even with a non-flawed implementation, the program might be interrupted by a signal correct?

Name: Anonymous 2012-01-12 15:24

>>89
It does on mine

Name: kodak_gallery_programmer !!kCq+A64Losi56ze 2012-01-12 15:24

>>76
I'm actually a Java Programmer, but anyways.....

Unless things have changes, fflush() itself is a (circular) linked list. When this function gets called, it will check against the "base". If the "base" is NULL, it will call malloc(). Otherwise it will do some kind of write().

Name: Anonymous 2012-01-12 15:24

>>91
Then your implementation of C is flawed get a conforming compiler.

Name: Anonymous 2012-01-12 15:24

>>89
inb4 IM USING A C VIRTUAL MACHINE NOT UR SHITTY COMPLIER

Name: Anonymous 2012-01-12 15:26

Now this is the first time I'm curious as to what Zhivago would say, I'm guessing it would something in line with >>54.

Name: Anonymous 2012-01-12 15:26

>>92
Java Programmer,
inb4 shitstorm fagstorm India

Name: Anonymous 2012-01-12 15:26

Now this is the first time I'm curious as to what Zhivago would say, I'm guessing it would be something in line with >>54.

Name: Anonymous 2012-01-12 15:27

>>92
What the fuck Kodak? What the very fuck.

Name: Anonymous 2012-01-12 15:27

Almost a 100 replies in an hour, /g/, seriously, please leave.

Name: Anonymous 2012-01-12 15:28

Check 'em?

Name: Anonymous 2012-01-12 15:28

>>96
At least I have enough of an idea on how fflush() actually works to make a reasonable guess on the source of the crash you fucking toilet scrubber. Now shut up and go google something.

Again, you're stupid. And again, you have no possible future as a computer programmer.

Name: Anonymous 2012-01-12 15:28

>>101
Ahhhh yeahhhhhh.

I love you Kodak, even though you're a Java programmer.

Name: Anonymous 2012-01-12 15:29

WTF is wrong with you?

Name: kodak_gallery_programmer !!kCq+A64Losi56ze 2012-01-12 15:30

>>98
I know I'm on the right track.

Name: Anonymous 2012-01-12 15:30

>>101
You have an 'idea'
Now provide some evidance.
But I will abandon my 'computer programmer' aspirations and move onto World Domination as soon as I 1CC some lunatic touhou

Name: Anonymous 2012-01-12 15:32

>>102
Listen you little stupid fucker. Go read "The C Programming Language" by K & R. At the end of chapter 8, there is a programming excercise that asks you to implement fflush().

Now shut up and go scrub another toilet you fucking dumbass.

Name: Anonymous 2012-01-12 15:33

>>105
Read response >>106 you fucking moron.

Name: Anonymous 2012-01-12 15:33

>>106
>K&R
>C standard

Name: kodak_gallery_programmer !!kCq+A64Losi56ze 2012-01-12 15:34

>>108
No, but the exercise does provide some insight into how fflush() works.

Name: Anonymous 2012-01-12 15:35

>>104
n1124 §7.19.5.3
If stream points to an output stream [...] the fflush function causes any unwritten data for that stream to be delivered to the host environment to be written to the file

Name: Anonymous 2012-01-12 15:36

All the autism because of OP's troll.

Name: Anonymous 2012-01-12 15:36

Again, it's undecidable.

Name: Anonymous 2012-01-12 15:36

>>110
And how much different is that fromt he outline of the code I gave you fucking halfwit?

Name: Anonymous 2012-01-12 15:38

>>109
'Some insight'
I tought precise terms were important to you?

Name: Anonymous 2012-01-12 15:38

>>112
Now go run off and play some internet video game. You clearly don't have what it takes yet.

Name: Anonymous 2012-01-12 15:38

>>114
Go back to /g/ jew

Name: Anonymous 2012-01-12 15:39

>>115
Go back to /g/

Name: Anonymous 2012-01-12 15:41

>>114
Well, you seem to get your panties in a bundle when I call you on your stupidity. Why don't just give your lame a ego a rest, accept that fact that your entire schooling was a waste, and accept that fact you will more than likely not land any kind of programming job when you get out of school.

Name: Anonymous 2012-01-12 15:41

>>95,97
I'm done with this stupid thread and the idiots in it now but Zhivago has a very similar view, see, for example,

http://www.reddit.com/r/programming/comments/nubf4/c11_has_been_published/c3ce1i3

Name: Anonymous 2012-01-12 15:44

Name: Anonymous 2012-01-12 15:48

>>119
Go back to reddit while you're at it.

Name: Anonymous 2012-01-12 15:51

>>118
Kodak is so tsundere, concerned about my future~

Name: Anonymous 2012-01-12 15:55

>>122
You've consistently demonstrated that you're clueless. On top of it, you seem to have a "fragile ego". I don't know about any other programmers here, but I would be reluctant to forward your resume to any of the hiring managers at my job.

Name: Anonymous 2012-01-12 16:11

Kodak, are your co-workers competent programmers or are some of them also mental midgets?

Name: Anonymous 2012-01-12 16:15

>>124
Once in a while we get a few mental midgets. The bright ones end up getting reassigned to marketing or tech support. The rest get laid off.

Name: Anonymous 2012-01-12 16:20

anal sexual intercourse without any lubricant

Name: Anonymous 2012-01-12 16:28

5.1.2.3 states that

The least requirements on a conforming implementation are:
...
- At program termination, all data written to files shall be identical to the result that execution of the program according to the abstract semantics would have produced.
...


Since stdout is a FILE *, that means that any conforming C implementation must allow this program to loop until you interrupt it with a signal or something to that effect.

Name: Anonymous 2012-01-12 16:29

f()->
  io:format("hax my anus~n"),
  f().


WAT DOSE IT DEW?

Name: Anonymous 2012-01-12 16:30

>>127
But the loop doesn't terminate.

Name: Anonymous 2012-01-12 16:31

>>128
That depends on the Erlang(?) standard.

Name: Anonymous 2012-01-12 16:32

>>129
That is correct.

Name: Anonymous 2012-01-12 16:34

>>127
| At program termination, all data written to files shall be identical to the result that execution of the program according to the abstract semantics would have produced.
| At program termination
Fucking this. TOLD

Name: Anonymous 2012-01-12 16:36

>>129
But you are allowed to interrupt it with a signal, which is why I think implementations that send a SIGSEGV get away with still being conforming implementations, as far as the implementation is concerned it's not running out of stack space, it's just sending the program a signal.

The program is well defined according to the standard, you are allowed to flush an output stream as many times as you like.

Name: Anonymous 2012-01-12 16:42

>>132
I don't know who is "told" but I am happy you found the standard agreeable.

>>127
5.1.2.3 also states that

The least requirements on a conforming implementation are:
...
- The input and output dynamics of interactive devices shall take place as specified in 7.21.3. The intent of these requirements is that unbuffered and line-buffered output appear as soon as possible, to ensure that prompting messages actually appear prior to a program waiting for input.
...

So it must adhere to line-buffering, which is interesting, as printf in this case will send a new-line character in every iteration of the loop.

Name: Anonymous 2012-01-12 16:43

Every C program can be interrupted by a signal. Are all C programs inherently nondeterministic, since a given state can terminate in different ways?

Name: Anonymous 2012-01-12 16:46

>>135
I don't know to be honest, I'm still reading the standard, I'll see if I find some guarantees either way.

Name: Anonymous 2012-01-12 17:14

>>136
I would really like to discuss this.

Name: Anonymous 2012-01-12 17:18

>>137
You should try getting your hands on a copy of the standard and start reading or perhaps you should get the latest draft for free, it's supposed to be 99.9% compatible with the C11 standard, either way I'm having a lot of fun reading how C works (or is supposed to work). I'll get back to you if I find anything interesting.

Name: Anonymous 2012-01-12 17:19

>>138
| You should try getting your hands on a copy of the standard


Will do

Name: Anonymous 2012-01-12 17:25

>>130
Due to recursion optimization, it loops indefinately.

Name: Anonymous 2012-01-12 17:25

>>140
Good to know.

Name: Anonymous 2012-01-12 22:37

There has been egregious misuse of the term deterministic in this shitty /g/ infiltrated thread, I'm going to leave a definition here to clarify for posterity.

Deterministic algorithms can be defined in terms of a state machine, if the machine is deterministic, its current state determines what its next state will be; its course through the set of states is predetermined.

Sufficient for non-determinism:
Never Terminates: NO (States are still correctly determined)
Potentially interruptable: NO (The signal is considered input to the state machine)
Accepts user input: NO (as above)
Invokes undefined behavior: YES (Machine may draw numbers out of a hat to determine next state)
Invokes unspecified behavior: YES (The same machine may produce different output on identical runs)
Invokes implementation defined behavior: YES (In terms of the abstract machine, conforming implementations may produce different results on the same input)

Name: Anonymous 2012-01-13 5:02

>>142
So the program in >>1 is both well defined and deterministic, good to know.

Name: Anonymous 2012-01-13 7:37

>>142
Take a logic class or go back to /g/

Name: Anonymous 2012-01-13 12:44

>>142
Invokes implementation defined behavior: YES (In terms of the abstract machine, conforming implementations may produce different results on the same input)
Fuck off, faggot. GNU C is the true C.

Name: Anonymous 2012-01-13 12:51

If it was deterministic, then it wouldn't need the preprocessor. However, C does need a preprocessor, so like... Oh geeze, never mind. Go scrub another toilet you mental midget.

Name: Anonymous 2012-01-13 13:06

>>146
This

It is non-deterministic

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