简体   繁体   English

为什么printf()在Windows上打印这个神秘的额外文本?

[英]Why printf() prints this mysterious extra text, on Windows?

I recently wrote a simple program to reverse a string, my program simply accepts an input string from user and then reverses it. 我最近编写了一个简单的程序来反转一个字符串,我的程序只接受来自用户的输入字符串,然后将其反转。 This is all done with 2 pointers. 这一切都是用2个指针完成的。 Concerning the pointer safety in my program I wrote a function to duplicate the give string, This function allocates memory for a new (same length) string, and then copy the character 1-by-1. 关于程序中的指针安全性,我编写了一个复制给定字符串的函数,该函数为新的(相同长度)字符串分配内存,然后逐个复制字符。

My problem is when I run this program, Although It does what I need, It prints some mysterious extra output. 我的问题是当我运行这个程序时,虽然它做了我需要的东西,它打印出一些神秘的额外输出。 It does this every time, before accepting input from the user. 在接受用户的输入之前,它每次都这样做。 This is what happens when I run the program. 这是我运行程序时发生的情况。

C:\Users\0xEDD1E\Desktop\revstr>revstr.exe
[C]
[.]
[revstr.exe]
hello
[hello]
olleh

here the last three lines are Input and Output, It's OK, The problem is in the first 3 lines 这里最后三行是输入和输出,没关系,问题出在前3行

[C]
[.]
[revstr]

What are those? 那些是什么? any way, here is my program 无论如何,这是我的计划

#include <stdio.h>
#include <stdlib.h>

#define swap(a, b) (((a) ^ (b)) && ((a) ^= (b), (b) ^= (a), (a) ^= (b)))

unsigned long strlen_(char *);
char *strdup(char *s);
char *reverseString(char *, int);

int main(void)
{
    //fflush(stdout);
    char *str = (char *) malloc(1024 * sizeof (char));
    scanf("%[^\n]s", str);
    int slen = strlen_(str);
    printf("%s\n", reverseString(str, slen));
    return 0;
}

unsigned long strlen_(char *s)
{
    char *p = s;
    while (*p) p++;
    return p - s;
}

char *strdup(char *s)
{
    char *p = (char *) malloc((size_t) (strlen_(s) + 1) * sizeof (char));

    if (p) {
        char *pp = p;
        while ((*pp++ = *s++)) ;
    }
    printf("[%s]\n", p);
    return p;
}


char* reverseString(char* s, int n) 
{

    char *str = strdup(s);
    char *p = str - 1;
    char *q = str + n;

    while (++p < --q) {
        swap(*p, *q);
    }

    return str;

}

you can see, looking at, strdup() function, those lines are generated by the printf() in the strdup() function (because of printf("[%s]\\n, str); ). 可以看到,在看, strdup()函数中,通过将所生成的那些线printf()strdup()函数(因为printf("[%s]\\n, str); )。

So I think I found the bug-point. 所以我想我找到了错误点。 But I can't figure out the reason for this error, and a way to fix it. 但我无法弄清楚这个错误的原因,以及解决它的方法。 Especially This happens only on Windows (mine is Windows 10 Pro). 特别是这只发生在Windows上(我的是Windows 10 Pro)。 I tested this on Ubuntu(64bit), there is no bug like this in Ubuntu. 我在Ubuntu(64位)上测试了这个,在Ubuntu中没有这样的bug。

My instinct says me this error has to do something with pointers used in the functions 我的直觉告诉我这个错误必须用函数中使用的指针做一些事情

UPDATE: I tried fflush(stdout) but that didn't help 更新:我试过fflush(stdout)但这没有帮助

What is the reason behind this Bug (or misbehave), could someone explain the reason? 这个Bug(或行为不端)背后的原因是什么,有人可以解释原因吗?

Probably, your system's C library also defines a function strdup , and some code in the program startup calls that function. 可能,您的系统的C库还定义了一个函数strdup ,程序启动中的一些代码调用该函数。 Then the linker links those calls to your strdup function instead of the system function. 然后链接器将这些调用链接到strdup函数而不是系统函数。

To avoid this, don't name your function strdup , or str anything for that matter: names starting str and a lowercase letter are reserved (C11 7.31.12, 7.31.13); 为了避免这种情况,不命名你的函数strdup ,或str为此事东西:开头的str和小写字母被保留 (C11 12年7月31日,13年7月31日); using them as function names in your own code causes undefined behaviour. 在自己的代码中使用它们作为函数名称会导致未定义的行为。

NB. NB。 char *p = str - 1; also causes undefined behaviour by doing pointer arithmetic outside the bounds of any object. 通过在任何对象的边界之外进行指针运算,也会导致未定义的行为。 (For example, imagine if str happens to be the first address in the address space). (例如,假设str恰好是地址空间中的第一个地址)。 It would be good to restructure your algorithm to not point out of bounds. 将算法重组为不指出界限是很好的。

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

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