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

Pages: 1-4041-

Why doesn't this end

Name: Anonymous 2010-10-31 5:35

#include <stdio.h>

main()
{

    int a;

while(a != 256)
    {putchar(a);
    a++;}
}

But if I add  a = 0;  after int a;, it ends fine?

Something to do with a variable not being initialized properly, and therefore, not being able to be compared or something?

Compiled with Code::Blocks+GCC or mingw or something. The debugger also reports no relevant issues with the initial code.

Name: Anonymous 2010-10-31 5:44

No [code] tags.
Demented indentation.
Sepples-infected C.
Trivial problem that would not be there if you had read K&R.
Jesus Christ.

Name: Anonymous 2010-10-31 5:49

It should end, you just haven't ran it long enough. If you actually remove the putchar call, it should end reasonably fast, depending on the speed of your CPU.

Name: Anonymous 2010-10-31 5:52

What do you think the value of a is after int a;? Why?

Name: Anonymous 2010-10-31 6:22

>>2
>demented indentation
maybe I'll pretty it up next time faggot
>C++ infected
wat, there's nothing C++ here
>trivial problem explain in K&R
And here I am inspired by the K&R to write this, except no, they don't explain it.
>2.4
>All variables must be declared before use
>no explanation other than down the page "undefined values are bad"
>and the program still compiles and runs anyway

>>3
Indeed it does end, is it that it just takes so long to increment from -1/2 the size value to positive 256, and of course, that the system initializes the value at -1/2size instead of 0?

>>4
I guess I kind of explained my idea in 3.

Name: Anonymous 2010-10-31 6:29

>>5
>and the program still compiles and runs anyway
Turn on warnings.

Name: Anonymous 2010-10-31 6:37

>>6
>Line 4: Warning, return type defaults to int
>Line 11: control reaches end of non-void function
There's a ton more case-specific warning options in the settings.

Name: Anonymous 2010-10-31 6:48

>>7
I find -W -Wall -Wextra to be sufficient. You should be told something like Warning: `a' may be used uninitialised here. See >>4.
Keep fixing until there are no warnings.

Name: Anonymous 2010-10-31 6:57

>>8
-W and -Wextra are the same thing.
-pedantic is also useful, though that wouldn't have helped >>1 with this particular bit of stupidity.

Name: Anonymous 2010-10-31 7:09

>>8
>>9
>call me an idiot
>neither can explain why an unitialized variable caused the program to run on repeatedly until the system feels it's not responding
dohoho you trolled me you experts, you sure did

Name: Anonymous 2010-10-31 7:14

Is /g/ down?

Name: Anonymous 2010-10-31 7:17

>>10
int a;
putchar (a);

>If that doesn't explain it, I will gladly call you an idiot.
>Now kindly fuck right off.

Name: Anonymous 2010-10-31 7:27

>>12
>implying if I use printf("%d", a) it makes a difference
>If that doesn't explain it
There is no explicit reason why it doesn't work. You're retarded.

If it's implicit that variables are initialized to -max, then that is a convention of the language.

Name: Anonymous 2010-10-31 7:30

>>12

>implying int and char aren't the exact same fucking thing in C, other than their sizes.

OP is stupid as fuck, but this is not why.

Name: Anonymous 2010-10-31 7:37

This thread is bad and you should feel bad.

Name: Anonymous 2010-10-31 7:39

>>13,14
Are you fucking serious?
implicit that variables are initialized to -max
In >>8 it is apparent that variables are not initialised unless you explicitly initialise them.
Meanwhile, printf("%d", a) prints the decimal value of a.
putchar prints the (ASCII) character a.
fputc() writes the character c, cast to an unsigned char, to stream.
putc() is equivalent to fputc() except that it may be implemented as a macro which evaluates stream more than once.
putchar(c); is equivalent to putc(c,stdout).


There are 4294967040 values of int outside of the range of a char. This is why it ``doesn't work''.
IHBMFT

Name: Anonymous 2010-10-31 7:45

>>1
mingw
Ah, I found the bug. You're on Windows. C only works on unix. On windows you need C++ or C#. My favourite programming language is dot net.

Name: Anonymous 2010-10-31 7:53

>>16
>variables are not initialised unless you explicitly initialise them
Except compilers don't give a fuck, and the program still increments a, though it's undefined.

And "something is not undefined" is not an explanation for it's behavior when it's not.

Name: Anonymous 2010-10-31 7:53

>>18
"something is not defined"*

Name: Anonymous 2010-10-31 7:57

>>16

This still does not mean he can't use an int.

The true error lies in the lack of initialization, not in the usage of int instead of char.

Hell, you can use:

time_t t;
for (t=0;t<256;++t) { putchar((char)t); }


if you wish.

Name: Anonymous 2010-10-31 8:06

>>20
The whole point of my thing though is where, or more important, what determines where unitialized variables start?

Yes, my original code seems to go on endlessly looping 255 characters, but where and why does it even start without being initialized?

Name: Anonymous 2010-10-31 8:07

>>18
Its value is defined, you just don't know what it is. This is what warnings are for. What if a happened to be 257?

Name: Anonymous 2010-10-31 8:09

>>22
Then the value that's in the position is determined by whether or not that part of memory has been wiped? That is the only effect of not initializing variables?

Name: Anonymous 2010-10-31 8:10

Apparently ``undefined behavior'' is a difficult concept now.

Name: Anonymous 2010-10-31 8:11

>>23
>Yes.
>He finally gets it.

Name: Anonymous 2010-10-31 8:19

>>25
>finally gets it
No, there is no possibly way it's clear for someone who hasn't learned the conventions of the language.

>>24
>undefined behavior
>implying the C standard leaves anything undefined
If it did, I would call shame on ANSI for ratifying it.

Name: >>3 2010-10-31 8:33

If you leave a variable unitialized, it will just take the value of whatever is there in the stack (assuming you're using a stack-based C implementation, which is the majority of them). Some (very few) implementations actually null the stack, but most won't, so the value of your variable depends on whatever leftover variables were left from other functions that ran before it, including your own code, or system's initialization code or whatever. In general, it's bad to rely on uninitiliazed variables for anything, you can't rely on them being constant or random, or anything (that is, unless you're in exact control of your compiler, implementation, OS, etc). >>24 said it right. This is a fairly direct form of "undefined behaviour", although in practice you can expect undefined variables to have a value, just none you can rely on in any way whatsoever, so you should always initialize your variables.

Name: Anonymous 2010-10-31 8:35

>>24
He's from the imageboards, cut him some slack. It's a wonder he managed to get from nothing to a text file that he somewhat knows how to compile.

Name: Anonymous 2010-10-31 8:40

What I don't understand is why OP's loop never ends.

He does increment a, even if it's not initialized. By declaring the variable, it gets some block of memory assigned and, by not initializing it, the allocated memory is not cleared and the old value there is kept. Assume the worst - that this value is 257, and assume that an int is signed 4 bytes. By incrementing the variable in this while loop, it should reach 2^15-1 relatively quickly, overflow, wrap around to -2^15 and keep counting until it reaches 256 and terminates.

It certainly would NEVER exit if it were a char. An unsigned char will never reach 256. Instead, it wraps around at 255 back to 0 and the test in the while loop would always be true.

Name: Anonymous 2010-10-31 8:47

>>29
It's 232-1. Also I/O takes a long time.

Name: Anonymous 2010-10-31 9:10

back to /g/, please

Name: Anonymous 2010-10-31 9:17

>>30

It'd be 2^31-1 if it were signed.

Name: Anonymous 2010-10-31 9:18

>>32
Yeah, I realised this. It's still a hell of a lot of numbers though.

Name: >>3 2010-10-31 9:29

>>33
It doesn't really take that long to run it, here's a quick benchmark I just wrote:

RDTSC
XOR ECX,ECX

LOOP:        
INC ECX
JNZ SHORT LOOP
MOV ECX,EAX
MOV EBX,EDX
RDTSC
SUB EAX,ECX
SUB EDX,EBX

This results is: eax = 019E6C7A, edx= 00000001 here, which is only some 1-2s on my x86 CPU.

For those that can't read x86 asm, that's the equivalent of:

unsigned int i = 0; while(++i);

With some timing code wrapped around at the start and end (well, actually it's a tick count, however given the processor and its frequency, you can convert it into a time unit).

Name: Anonymous 2010-10-31 9:31

>>34
There is a bottleneck, and it is putchar.

Name: Anonymous 2010-10-31 9:31

>>34,35
Oh, never mind I just read >>3

Name: Anonymous 2010-10-31 9:32

>>35
Yes, I know, however just looping through unsigned integer (on 32bit systems), barely takes more than a few seconds on most modern x86 CPUs.

Name: Anonymous 2010-10-31 9:44

Just pipe it to /dev/null

Name: Anonymous 2010-10-31 11:02

>>35
If you're piping the output to /dev/null, the compiler is going to optimize it out anyway.

Name: Anonymous 2010-10-31 11:03

>>39

You pipe it at runtime.

Name: Anonymous 2010-10-31 11:09

>>40
yhbt

Name: Anonymous 2010-10-31 13:38

>>41
Motherfucker.

Name: Anonymous 2010-10-31 15:45

Maybe a has a value greater than 256?
IT IS A MYSTERY

Name: Anonymous 2010-10-31 16:19

>>43
Maybe PAY ATTENTION TO THE THREAD GODDAMN

Name: Anonymous 2010-10-31 18:59

yhl yhbt hand

Name: Anonymous 2010-10-31 20:14

Relevant parts of the latest C99 standard (n1256.pdf) regarding uninitialized automatic storage duration objects:

6.7.8p10: If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate.

3.17.2 (definition of indeterminate value): Either an unspecified value or a trap representation.

3.17.3 (definition of unspecified value): Valid value of the relevant type where this International Standard imposes no requirements on which value is chosen in any instance. NOTE An unspecified value cannot be a trap representation.

6.2.6.1p5 (definition of trap representation): Certain object representations need not represent a value of the object type. If the stored value of an object has such a representation and is read by an lvalue expression that does not have character type, the behavior is undefined. If such a representation is produced by a side effect that modifies all or any part of the object by an lvalue expression that does not have character type, the behavior is undefined.41) Such a representation is called a trap representation.

Footnote 41: Thus, an automatic variable can be initialized to a trap representation without causing undefined behavior,but the value of the variable cannot be used until a proper value is stored in it.

Name: Anonymous 2010-11-01 0:22

>>46
Thanks, Zhivago.

Name: Anonymous 2010-11-14 23:31

Name: Anonymous 2011-02-04 19:10


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