c - Stream buffer size less than input stream, but no immediate segmentation fault -


Why does the following C code allow an input stream which is greater than the size of the buffer, before it is finally a Partition fault? With a character array of size 1 as the argument of too_bad , will it not allow 1 character of input only?

In addition, what is the effect of not starting c as a int and not char ? The statement will be read from getchar () 4 characters while ((c = getchar ()) = '\ N' & amp; c! = EOF) is? Or will the binary representation of the characters be in 4 bytes?

Note: This is a text from the class, but not the HW.

  #include & lt; Stdio.h & gt; Zero too_bad (four * buffer) {int c; / * NB: instead of char int because the system gives the integer value 0xffffffff on the I / O function getchar () end file * / char * dest = buffer; / * As long as the new line or file of end (Control-Z for standard input) * / read as ((c = getchar ()) = '\ n' & amp; c; = EOF * dest + + = C; Store next characters in buffer and pointer * / printf ("% s \ n", buffer); } Int main () {four fans [1]; / * Waiting for big trouble * / too_bad (fond); Return 0; }    

For the first part of your question, what to do with the page size in it OS And of course, this code causes "undefined behavior".

This answer really gives a good idea:

In addition, what are the effects of starting in C and not a four? When making the statement ((C = getchar ())! = '\ N' & amp; c! = EOF 4 characters will be read from getchar ()? Or will the binary representation of characters be in 4 bytes?

is the proto () of getchar:

  int singer (zero);   

, When you use c = getchar () , each call will be read from a getchar () stdin, convert variable Return the value to assign int and c to variable.

Comments

Popular posts from this blog

java - ImportError: No module named py4j.java_gateway -

python - Receiving "KeyError" after decoding json result from url -

.net - Creating a new Queue Manager and Queue in Websphere MQ (using C#) -