不同操作系统/编译器之间的 C 风格字符串输出不一致

Inconsistent C style string output between different operating systems / compilers

我有一个 C++ 程序:

#include <iostream>

char * foo (char * bar, const char * baz) {
    int i = -1;

    do {
        i++;    
        *(bar + i) = *(baz + i);
    } while (*(baz + i));

    return bar;
}

int main (int argc, char *argv[]) {
    char bar[] = "";
    char baz[] = "Hello";

    foo(bar, baz);

    std::cout << "bar: " << bar << std::endl;
    std::cout << "baz: " << baz << std::endl;
}

并不是说这是重要的部分,但该程序的要求是它使用指针将一个 C style string 复制到另一个。

当我在我的 Ubuntu 16.04 桌面上编译和执行我的二进制文件时,这是我看到的:

$ g++ -std=c++11 test.cpp -o test && ./test
bar: Hello
baz: ello

哎呀! baz 的初始 'H' 已被删除,但我根本看不出我的 foo 函数如何更改 baz。嗯...

我的 Ubuntu 桌面上的 g++ 版本是:

$ g++ --version
g++ (Ubuntu 5.4.0-6ubuntu1~16.04.12) 5.4.0 20160609
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

我认为这是我的代码的一个错误或错误(可能仍然是),但我发现当我在任何其他操作系统上编译和 运行 时,我会得到不同的行为。

这是 macOS 上的输出:

$ g++ -std=c++11 test.cpp -o test && ./test
bar: Hello
baz: Hello

这是那台 macOS 笔记本电脑上的 g++ 版本:

$ g++ --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 12.0.0 (clang-1200.0.32.2)
Target: x86_64-apple-darwin19.5.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

在其他 Linux 盒子上测试时,在 Windows 等上测试时,它具有正确的预期输出 barbaz 都是 Hello.

这是怎么回事!?

tl;dr C++ 程序在我的桌面上输出的 C 风格字符串与其他任何计算机都不同。为什么?

char bar[] = "";

这保证创建一个一个字节长的内存区域(基本上刚好足以容纳 '[=13=]')。一个实现可能给你更多,但你不能依赖它。

因此它不够大,无法存储需要六个字节的字符串 "Hello"。例如,C++20 [expr.add] 中对此进行了说明,并强调:

If the expression P points to element x[i] of an array object x with n elements, the expressions P + J and J + P (where J has the value j) point to the (possibly-hypothetical) element x[i + j] if 0 <= i + j <= n; otherwise, the behavior is undefined.

如果要确保 this 代码段中有足够的 space,只需将声明更改为:

char baz[] = "Hello";
char bar[sizeof(baz)];  // bar will be same size as baz

对于其他场景,有不同的方法来保证这个大小,但一般规则仍然相同:确保目标数组足够大,这样你就不会超出它的末尾。


虽然未定义的行为意味着任何事情都可能发生,但在您的错误案例中最可能发生的是堆栈上的以下内存布局。您将字符 one-by-one 从 baz 复制到 bar$ 代表 [=29=] 字符),导致以下前后快照:

     bar
      V
    +---+---+---+---+---+---+---+
    | $ | H | e | l | l | o | $ |  (before)
    +---+---+---+---+---+---+---+
    | H | e | l | l | o | $ | $ |  (after)
    +---+---+---+---+---+---+---+
          ^
         baz

因此您可以看到在 bar 末尾之外的写入如何影响堆栈中的其他内容,例如 baz。如果堆栈布局不同,效果很可能也会不同。

例如,如果 barbazother 顺序的堆栈中,那么 bar影响baz。它几乎肯定会影响堆栈上的某些 else,导致奇怪的行为,特别是如果其他东西恰好是调用函数的 return 地址:-)

底线是,未定义的行为恰恰意味着 - 你不能依赖任何按预期工作的东西。