Why is this happening (see image)?

Why the following matters: it prints a terminal full of random characters, and then leaves the command line, which creates garbage when you enter it. (I tried this because I thought it would cause a seg error).

http://oi38.tinypic.com/r9qxbt.jpg

#include <stdio.h> int main(){ char* s = "lololololololol"; while(1){ printf("%c", *s); s++; } } 

It was compiled with:

  gcc -std = c99 hello.c 
+6
c terminal reset console
source share
7 answers

Ultimately, seg will fail, but before that it will print all bytes on one page. This is why you see random characters on the screen.

These may include escape sequences to change (say) the character encoding of the console. That's why you end gibberish when you type the console after it comes out.

+20
source share

You simply print out what is in memory because your loop does not stop at the end of the line. Each random byte is interpreted as a character. This will crash when you reach the end of the memory page (and fall into unreadable territory).

+7
source share

Since you have an infinite loop ( while(1) ), and you continue to get the current value of the pointer ( *s ), and then move the pointer one char forward ( s++ ). This leads to the fact that the march passes through the end of the line to the "garbage" (uninitialized memory), which as a result is displayed on the console.

+7
source share

In addition to what everyone else said due to the fact that you ignore the string terminal character and just willy-nilly print that in memory after the line, the reason your command line is also β€œgarbage” is because by printing a certain "unprintable", the terminal session remained in strange character mode. (I don’t know what symbol it is or what mode it changed, but maybe someone else can talk about it, who knows better than me.)

+7
source share

Multiplying so little on the answers given here (they are all excellent) ... I came across this more than once when I just started with C, and it is easy to do.

Quick tuning of your while fix it. Everyone else gave you why; I will suspend you of how:

 #include <stdio.h> int main() { char *s = "lolololololololol"; while (*s != '\0') { printf("%c", *s); s++; } } 

Note that instead of an infinite loop ( while(1) ), we do a loop check to ensure that the pointer we pull out is not the null terminator for the string, thus avoiding overruns when meeting.

If you absolutely need while(1) (for example, if it's homework and the instructor wants you to use it), use the break keyword to exit the loop. The following code smells, at least to me, but it works:

 #include <stdio.h> int main() { char *s = "lolololololololol"; while (1) { if (*s == '\0') break; printf("%c", *s); s++; } } 

Both produce the same console output without breaking a line at the end:

lolololololololol

+2
source share

Your loop does not end, so println prints everything that is in memory after the text you write; ultimately, it will access memory that is not allowed to be read, which leads to its segfault.

You can change the loop, as others suggested, or you can take advantage of the fact that in c zero is false and null (which ends all lines) is also zero, so you can build a loop like:

 while (*s) { 

Instead

 while (*s != '\0') 

The first may be harder to understand, but it has the advantage of brevity, so it is often used to save a small typing.

+1
source share

In addition, you can return to the command line using the "reset" command, typing, of course, blindly. (type Enter, reset, Enter)

0
source share

All Articles