3

I was just trying to animate some strings before terminating the application. I called attached piece of code to do so. but it keeps on printing garbage till core dumped.

Here is that code..

int terminate_msg(int quit_flag)
{
    char server_msg[] = "\nApplication Not Alive";
    char exit_msg[] = "\nTerminating Application...\n";
    char *ch;
    int i = 0;

    if(quit_flag == 1) {
        printf("%s%s", server_msg, exit_msg);
        while((ch = server_msg + i) != NULL) {
            printf("%c",*ch);
            fflush(stdout);
            usleep(100000);
            i++;
        }
        i = 0;
        sleep(2);
        while((ch = exit_msg + i) != NULL) {
            printf("%c",*ch);
            fflush(stdout);
            usleep(100000);
            i++;
        }
        sleep(2);
    }   
    return 0;
}

Output:

Application Not Alive
Terminating Application...

Application Not Alive
Terminating Application...
���І�JS�dX��lX���p�dX��X��$���P
l�UZ��D~�]P�up��IS�@q�P�q�M�dX��І@��!p�\X��^C

Though first printf() prints correctly, don't know why garbage prints are coming at the end. If there is any problem, garbage should come after first while loop also.

Note that, when comparing *ch != '\0', everything is working fine.

What is wrong with this piece of code? I'm using gcc in linux environment.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Aman
  • 37
  • 2
  • Use `while(*(ch = server_msg + i) != '\0') {` and `while(*(ch = exit_msg + i) != '\0') {`. BTW, you have an infinite loop as `quit_flag` is not modified. – Spikatrix Jun 26 '15 at 11:21
  • `NULL` is a macro describing a pointer to zero. It's used to have a value that is not valid as a pointer value. `NUL` is used in the ASCII table to name the character with the code 0. `'\0'` is the C coding of a character with the code 0. 0 is the 0-value itself. There are so many ways to define a character with the value 0. Why do you use this `NULL` macro? [http://stackoverflow.com/questions/1296843/what-is-the-difference-between-null-0-and-0] [https://en.wikipedia.org/wiki/Null_character] – harper Jun 26 '15 at 11:43

2 Answers2

5

This condition is wrong

while((ch = server_msg + i) != NULL)

you should try this

char ch;

while ((ch = server_msg[i]) != '\0')
    printf("%c", ch);

the condition is testing if the pointer is NULL, which very likely isn't going to happen, what you must test is if the '\0' terminating byte was reached, for that you need to dereference the pointer.

Since NULL is basically (void *) 0 in c, and if server_msg is a valid pointer it's value is certainly > 0, then the value of (void *) 0 is unreachable by just incrementing the pointer, so the loop never ends and your program continues to access the pointer after it's passed the valid memory it points to.

Of course, the same problem happens here

while((ch = exit_msg + i) != NULL) {
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
3

In your case, inside a loop, with anincrementing value of i (i++;)

 server_msg + i

points to the next memory location to server_msg in every iteration. The problem here is, there is no guarantee, that the immediate next address (after the string termination) will be NULL. It can very well be a valid memory area (have most probability for being so) to pass a NULL check.

So, your loop will not break (as you've expected), it will continue execution and (try to) access out-of-bound memory area, which in turn invokes undefined behaviour. So, the output and the behaviour is also, undefined.

Rather , you should reply on the *ch value as \0 for judging the string termination.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • Yes, i got it in a while..what i was doing wrong..I mentioned it also...*ch != '\0', everything is working fine. Garbage is coming with exit_msg (second while loop). Any information on that?? – Aman Jun 26 '15 at 11:35
  • @Aman did you change the check to ` *ch != '\0',` in `exit_msg` ? – Sourav Ghosh Jun 26 '15 at 11:37
  • With checking the "value of ch" in both the loop together, everything works fine. But checking `*ch != '\0'` in only `exit_msg` is still printing garbage. – Aman Jun 26 '15 at 11:48
  • @Aman Obviously, first while will give UB then. Once UB, UB forever. :-) – Sourav Ghosh Jun 26 '15 at 11:49