为什么"while(*sea ++ =*river ++);" 不能正常工作?

Raf*_*han 3 c++

我的大学教授最近给了我们一个实现我们自己的智能指针类的任务.在他用于复制字符串的样板代码中,我发现这件作品很漂亮的语法糖:

while (*sea++ = *river++);// C Sting copy 
Run Code Online (Sandbox Code Playgroud)

我进一步研究了这段代码,发现它是strcpy.c中的确切代码,并在下面的stackoverflow问题中进一步解释它是如何工作的: "while(*s ++ =*t ++)"如何复制一个字符串?

当我尝试在我的下面的代码中使用这个语法糖时,它给出了垃圾作为结果并删除了存储在"river"中的字符串:

    #include<iostream>
    #include<cstring>

    using namespace std;
        void main()
        {
            const char *river = "water";// a 5 character string +  NULL terminator

            char *sea = new char[6];

            while (*sea++ = *river++);

            cout << "Sea contains: " << sea << endl;
            cout << "River contains: " << river << endl;
        }
Run Code Online (Sandbox Code Playgroud)

结果:
上述代码的结果

我知道我可以使用以下代码简单地实现所需的结果:

    int i = 0;
    while (i<6)
    {
        sea[i] = river[i];
        i++;
    }
Run Code Online (Sandbox Code Playgroud)

但这不是我想要的答案.我想知道我的while循环的实现或我的char指针的实例化有什么问题吗?

Rem*_*eau 7

你正在显示垃圾,因为当你去显示它们时你的指针指向垃圾.您在循环时推进指针,但在显示数据时需要使用原始指针.

此外,您有内存泄漏,因为您没有释放char[]缓冲区.

试试这个:

#include <iostream>
#include <cstring>

using namespace std;

int main()
{
    const char *river = "water";// a 5 character string +  NULL terminator

    char *sea = new char[6];

    const char *p_river = river;
    char *p_sea = sea;
    while (*p_sea++ = *p_river++);

    cout << "Sea contains: " << sea << endl;
    cout << "River contains: " << river << endl;

    delete [] sea;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)


Emi*_*lia 5

"错误"在技术上是一种未定义的行为,因为指针超出了对它们引用的内存的限制.

有趣的是,为什么会发生这种情况.

这是因为指针之间的混淆以及它们指向的内容.

seariver不是字符串.字符串是驻留在内存中某处的匿名变量,两个指针指示它们的开始.

你永远不应该触摸它们,否则你将无法再进一步访问这些字符串.

如果您需要移动指针,请使用其他指针.

一个更恰当的例子就是这个

 using namespace std;
 int main() //< note `void main` is a C++ dialcet
 {
     // note the use of `const` after the `*`: 
     // you cannot modify these pointers.
     const char * const river = "water";  // a 5 character string +  NULL terminator
     char * const sea = new char[6];

     {
          // in this inner scope, other non-const pointers are
          // initialized to point to the same memory
          const char* r = river;
          char* s = sea;
          while (*s++ = *r++); // the loop moves the mutable pointers
          // note how `river++` or `sea++` is an error, being them `*const`
     }

     // there are no more `r` and `s` here, but `sea` and `river` are still the same.
     cout << "Sea contains: " << sea << endl;
     cout << "River contains: " << river << endl;

     //now we have an array allocated with new to return to the system
     delete[] sea; //< the importance to not move the `sea` pointer
}
Run Code Online (Sandbox Code Playgroud)

注意如何delete删除数组,而不是指针.

要提前做更多事情,可以做两件事.

一个是使内部范围成为一个合适的函数:

 using namespace std;

 void copy(const char* r, char* s)
 {
     // in this function, other non-const pointer (parameters) are
     // initialized to point to the same memory upon call
     while (*s++ = *r++); // the loops moves the mutable pointers
     // note how `river++` or `sea++` is an error, being them not visible.
 }

 int main() //< note `void main` is a C++ dialect
 {
     const char * const river = "water";  // a 5 character string +  NULL terminator
     char * const sea = new char[6];

     copy(river, sea);         

     cout << "Sea contains: " << sea << endl;
     cout << "River contains: " << river << endl;

     //now we have an array allocated with new to return to the system
     delete[] sea; //< the importance to not move the `sea` pointer
}
Run Code Online (Sandbox Code Playgroud)

第二被摆脱了new/delete对在相同的上下文中,使用-用于例-一个std::unique_ptr<char[]>

但这太过分了!