简体   繁体   English

意外行为,字符串后打印垃圾

[英]Unexpected behaviour, Garbage prints after string

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. 虽然第一次printf()打印正确,但不知道为什么垃圾打印最后会出现。 If there is any problem, garbage should come after first while loop also. 如果有任何问题,垃圾应该先在循环后进行循环。

Note that, when comparing *ch != '\\0' , everything is working fine. 请注意,在比较*ch != '\\0' ,一切正常。

What is wrong with this piece of code? 这段代码有什么问题? I'm using gcc in linux environment. 我在linux环境中使用gcc

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. 条件是测试指针是否为NULL ,这很可能不会发生,你必须测试的是,如果到达'\\0'终止字节,你需要取消引用指针。

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. 由于NULL在c中基本上是(void *) 0 ,并且如果server_msg是一个有效的指针,它的值当然> 0 ,那么只需递增指针就无法访问(void *) 0的值,所以循环永远不会结束程序在传递指向的有效内存后继续访问指针。

Of course, the same problem happens here 当然,这里也会出现同样的问题

while((ch = exit_msg + i) != NULL) {

In your case, inside a loop, with anincrementing value of i ( i++; ) 在你的情况下,在一个循环中,增量值为ii++;

 server_msg + i

points to the next memory location to server_msg in every iteration. 指向每次迭代中server_msg的下一个内存位置。 The problem here is, there is no guarantee, that the immediate next address (after the string termination) will be NULL . 这里的问题是,无法保证立即的下一个地址(字符串终止后)将为NULL It can very well be a valid memory area (have most probability for being so) to pass a NULL check. 它很可能是一个有效的内存区域(最有可能)是通过NULL检查。

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. 相反,您应该将*ch值作为\\0回复以判断字符串终止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM